File size: 2,058 Bytes
3bdacaf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9542199
 
 
 
3bdacaf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const SOCIAL_REQUIREMENT_PREFIX = "User requirement from social community:";
const GENERIC_PROBLEM_STATEMENT =
  /^Build a\s+.+?\s+product for\s+.+?\s+based on the submitted idea\.$/i;

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

export function stripSocialRequirementPrefix(value: string | null | undefined): string {
  if (!value) return "";

  const trimmed = value.trim();
  if (!trimmed) return "";

  if (trimmed.startsWith(SOCIAL_REQUIREMENT_PREFIX)) {
    return trimmed.slice(SOCIAL_REQUIREMENT_PREFIX.length).trim();
  }

  return trimmed;
}

export function cleanProblemStatement(
  problemStatement: string | null | undefined,
  rawInputText: string | null | undefined,
): string {
  const cleanedRawInput = stripSocialRequirementPrefix(rawInputText);
  const cleanedProblem = stripSocialRequirementPrefix(problemStatement);

  if (!cleanedProblem) {
    return cleanedRawInput;
  }

  if (GENERIC_PROBLEM_STATEMENT.test(cleanedProblem)) {
    return cleanedRawInput || cleanedProblem;
  }

  return cleanedProblem;
}

export function cleanIdeaTitle(title: string | null | undefined): string {
  return stripSocialRequirementPrefix(title);
}

export function normalizeIdeaCopy(input: {
  rawInputText: string | null | undefined;
  problemStatement: string | null | undefined;
}) {
  const rawInputText = stripSocialRequirementPrefix(input.rawInputText);
  const problemStatement = cleanProblemStatement(input.problemStatement, rawInputText);

  return {
    rawInputText,
    problemStatement,
  };
}

export function looksLikeDirtyIdeaCopy(input: {
  rawInputText: string | null | undefined;
  problemStatement: string | null | undefined;
}) {
  const currentRaw = collapseWhitespace(input.rawInputText || "");
  const currentProblem = collapseWhitespace(input.problemStatement || "");
  const normalized = normalizeIdeaCopy(input);

  return (
    currentRaw !== collapseWhitespace(normalized.rawInputText) ||
    currentProblem !== collapseWhitespace(normalized.problemStatement)
  );
}