File size: 1,686 Bytes
8a8c5bb
 
 
 
 
 
 
 
 
 
06e51c6
8a8c5bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { validateTemplate } from "./validation.js";

export interface Template {
  title: string;
  body: string;
}

const INVISIBLE_TEMPLATE_CHARS = /[\u200B-\u200D\u2060-\u206F\uFEFF]/g;

/**
 * Filter and normalize an array of templates.
 *
 * The function validates each template using `validateTemplate` and returns
 * only those that pass validation. Invalid items are ignored. A TypeError is
 * thrown if the input is not an array.
 */
export function integrateTemplates(templates: unknown[]): Template[] {
  if (!Array.isArray(templates)) {
    throw new TypeError("templates must be an array");
  }

  const result: Template[] = [];
  for (const tpl of templates) {
    try {
      // Reject non-objects, null, and arrays before touching any fields
      if (
        typeof tpl !== "object" ||
        tpl === null ||
        Array.isArray(tpl) ||
        !Object.prototype.hasOwnProperty.call(tpl, "title") ||
        !Object.prototype.hasOwnProperty.call(tpl, "body")
      ) {
        continue;
      }

      const rec = tpl as Record<string, unknown>;
      const title = rec.title;
      const body = rec.body;
      // Validate using the already-read values so getters are not invoked twice
      if (validateTemplate({ title, body })) {
        const normalizedTitle = String(title)
          .replace(INVISIBLE_TEMPLATE_CHARS, "")
          .trim();
        const normalizedBody = String(body)
          .replace(INVISIBLE_TEMPLATE_CHARS, "")
          .trim();
        result.push({
          title: normalizedTitle,
          body: normalizedBody,
        });
      }
    } catch {
      // Ignore entries where property access throws
    }
  }
  return result;
}