index.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435
  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
  22. .slice(start, end)
  23. .split("\0")
  24. .map(transform)
  25. .join(delimiter);
  26. }
  27. /**
  28. * Replace `re` in the input string with the replacement value.
  29. */
  30. function replace(input, re, value) {
  31. if (re instanceof RegExp)
  32. return input.replace(re, value);
  33. return re.reduce(function (input, re) { return input.replace(re, value); }, input);
  34. }
  35. //# sourceMappingURL=index.js.map