index.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var lower_case_1 = require("lower-case");
  4. // Support camel case ("camelCase" -> "camel Case" and "CAMELCase" -> "CAMEL Case").
  5. var DEFAULT_SPLIT_REGEXP = [/([a-z0-9])([A-Z])/g, /([A-Z])([A-Z][a-z])/g];
  6. // Remove all non-word characters.
  7. var DEFAULT_STRIP_REGEXP = /[^A-Z0-9]+/gi;
  8. /**
  9. * Normalize the string into something other libraries can manipulate easier.
  10. */
  11. function noCase(input, options) {
  12. if (options === void 0) { options = {}; }
  13. var _a = options.splitRegexp, splitRegexp = _a === void 0 ? DEFAULT_SPLIT_REGEXP : _a, _b = options.stripRegexp, stripRegexp = _b === void 0 ? DEFAULT_STRIP_REGEXP : _b, _c = options.transform, transform = _c === void 0 ? lower_case_1.lowerCase : _c, _d = options.delimiter, delimiter = _d === void 0 ? " " : _d;
  14. var result = replace(replace(input, splitRegexp, "$1\0$2"), stripRegexp, "\0");
  15. var start = 0;
  16. var end = result.length;
  17. // Trim the delimiter from around the output string.
  18. while (result.charAt(start) === "\0")
  19. start++;
  20. while (result.charAt(end - 1) === "\0")
  21. end--;
  22. // Transform each token independently.
  23. return result
  24. .slice(start, end)
  25. .split("\0")
  26. .map(transform)
  27. .join(delimiter);
  28. }
  29. exports.noCase = noCase;
  30. /**
  31. * Replace `re` in the input string with the replacement value.
  32. */
  33. function replace(input, re, value) {
  34. if (re instanceof RegExp)
  35. return input.replace(re, value);
  36. return re.reduce(function (input, re) { return input.replace(re, value); }, input);
  37. }
  38. //# sourceMappingURL=index.js.map