1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.merge = merge;
|
---|
7 | exports.validate = validate;
|
---|
8 | exports.normalizeReplacements = normalizeReplacements;
|
---|
9 |
|
---|
10 | function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
---|
11 |
|
---|
12 | function merge(a, b) {
|
---|
13 | const {
|
---|
14 | placeholderWhitelist = a.placeholderWhitelist,
|
---|
15 | placeholderPattern = a.placeholderPattern,
|
---|
16 | preserveComments = a.preserveComments,
|
---|
17 | syntacticPlaceholders = a.syntacticPlaceholders
|
---|
18 | } = b;
|
---|
19 | return {
|
---|
20 | parser: Object.assign({}, a.parser, b.parser),
|
---|
21 | placeholderWhitelist,
|
---|
22 | placeholderPattern,
|
---|
23 | preserveComments,
|
---|
24 | syntacticPlaceholders
|
---|
25 | };
|
---|
26 | }
|
---|
27 |
|
---|
28 | function validate(opts) {
|
---|
29 | if (opts != null && typeof opts !== "object") {
|
---|
30 | throw new Error("Unknown template options.");
|
---|
31 | }
|
---|
32 |
|
---|
33 | const _ref = opts || {},
|
---|
34 | {
|
---|
35 | placeholderWhitelist,
|
---|
36 | placeholderPattern,
|
---|
37 | preserveComments,
|
---|
38 | syntacticPlaceholders
|
---|
39 | } = _ref,
|
---|
40 | parser = _objectWithoutPropertiesLoose(_ref, ["placeholderWhitelist", "placeholderPattern", "preserveComments", "syntacticPlaceholders"]);
|
---|
41 |
|
---|
42 | if (placeholderWhitelist != null && !(placeholderWhitelist instanceof Set)) {
|
---|
43 | throw new Error("'.placeholderWhitelist' must be a Set, null, or undefined");
|
---|
44 | }
|
---|
45 |
|
---|
46 | if (placeholderPattern != null && !(placeholderPattern instanceof RegExp) && placeholderPattern !== false) {
|
---|
47 | throw new Error("'.placeholderPattern' must be a RegExp, false, null, or undefined");
|
---|
48 | }
|
---|
49 |
|
---|
50 | if (preserveComments != null && typeof preserveComments !== "boolean") {
|
---|
51 | throw new Error("'.preserveComments' must be a boolean, null, or undefined");
|
---|
52 | }
|
---|
53 |
|
---|
54 | if (syntacticPlaceholders != null && typeof syntacticPlaceholders !== "boolean") {
|
---|
55 | throw new Error("'.syntacticPlaceholders' must be a boolean, null, or undefined");
|
---|
56 | }
|
---|
57 |
|
---|
58 | if (syntacticPlaceholders === true && (placeholderWhitelist != null || placeholderPattern != null)) {
|
---|
59 | throw new Error("'.placeholderWhitelist' and '.placeholderPattern' aren't compatible" + " with '.syntacticPlaceholders: true'");
|
---|
60 | }
|
---|
61 |
|
---|
62 | return {
|
---|
63 | parser,
|
---|
64 | placeholderWhitelist: placeholderWhitelist || undefined,
|
---|
65 | placeholderPattern: placeholderPattern == null ? undefined : placeholderPattern,
|
---|
66 | preserveComments: preserveComments == null ? undefined : preserveComments,
|
---|
67 | syntacticPlaceholders: syntacticPlaceholders == null ? undefined : syntacticPlaceholders
|
---|
68 | };
|
---|
69 | }
|
---|
70 |
|
---|
71 | function normalizeReplacements(replacements) {
|
---|
72 | if (Array.isArray(replacements)) {
|
---|
73 | return replacements.reduce((acc, replacement, i) => {
|
---|
74 | acc["$" + i] = replacement;
|
---|
75 | return acc;
|
---|
76 | }, {});
|
---|
77 | } else if (typeof replacements === "object" || replacements == null) {
|
---|
78 | return replacements || undefined;
|
---|
79 | }
|
---|
80 |
|
---|
81 | throw new Error("Template replacements must be an array, object, null, or undefined");
|
---|
82 | } |
---|