File size: 1,909 Bytes
773ca59
3bdacaf
773ca59
e8dc740
 
 
 
 
773ca59
 
 
 
 
 
e8dc740
773ca59
 
 
 
 
e8dc740
773ca59
 
e8dc740
773ca59
 
 
e8dc740
773ca59
e8dc740
 
773ca59
 
 
 
e8dc740
773ca59
 
 
 
e8dc740
773ca59
 
 
 
e8dc740
773ca59
 
 
e8dc740
 
773ca59
 
 
 
 
 
 
 
 
 
e8dc740
773ca59
e8dc740
 
 
 
 
 
 
9542199
773ca59
 
e8dc740
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { cleanIdeaTitle, cleanProblemStatement, stripSocialRequirementPrefix } from "@/lib/idea-copy";

const MAX_TITLE_LENGTH = 80;

function collapseWhitespace(value: string): string {
  return value.replace(/\s+/g, " ").trim();
}

function trimLeadIn(value: string): string {
  return value
    .replace(/^i\s+(want|need|would\s+like|wish|hope)\s+(to\s+)?/i, "")
    .replace(/^please\s+/i, "")
    .replace(/^can\s+you\s+/i, "");
}

function truncateSummary(value: string, maxLength = MAX_TITLE_LENGTH): string {
  const text = collapseWhitespace(value);
  if (text.length <= maxLength) {
    return text;
  }

  let clipped = text.slice(0, maxLength);
  const lastSpace = clipped.lastIndexOf(" ");

  if (lastSpace >= Math.floor(maxLength / 2)) {
    clipped = clipped.slice(0, lastSpace);
  }

  return clipped.replace(/[,:;.!?\-–—]+$/g, "").trim();
}

function deriveTitleFromInput(rawInputText: string, problemStatement?: string | null): string {
  const source =
    cleanProblemStatement(problemStatement, rawInputText) ||
    stripSocialRequirementPrefix(rawInputText);

  let text = collapseWhitespace(source);
  if (!text) {
    return "";
  }

  const contextSplit = text.split(/\bContext:\b/i)[0];
  if (contextSplit) {
    text = contextSplit;
  }

  const sentenceSplit = text.split(/[.!?]\s+/)[0];
  if (sentenceSplit) {
    text = sentenceSplit;
  }

  text = text
    .split("\n")[0]
    .trim();

  text = trimLeadIn(text);
  text = collapseWhitespace(text);

  if (!text) {
    return "";
  }

  return truncateSummary(text);
}

export function buildMeaningfulTitle(params: {
  rawInputText: string;
  title: string;
  problemStatement?: string | null;
}): string {
  const candidate = collapseWhitespace(cleanIdeaTitle(params.title || ""));
  const derived = deriveTitleFromInput(params.rawInputText, params.problemStatement);
  return derived || candidate || "New Product Idea";
}