index.js 1.5 KB

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