options.js 3.0 KB

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