options.js 3.0 KB

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