util.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.isNumeric = isNumeric;
  4. exports.hyphenToCamelCase = hyphenToCamelCase;
  5. exports.trimEnd = trimEnd;
  6. exports.kebabCase = kebabCase;
  7. exports.replaceSpaces = replaceSpaces;
  8. /**
  9. * Determines if the specified string consists entirely of numeric characters.
  10. *
  11. * @param {*} [value]
  12. * @returns {boolean}
  13. */
  14. function isNumeric(value) {
  15. return !Number.isNaN(value - parseFloat(value));
  16. }
  17. /**
  18. * Convert a hyphenated string to camelCase.
  19. *
  20. * @param {string} string
  21. * @returns {string}
  22. */
  23. function hyphenToCamelCase(string) {
  24. return string.replace(/-(.)/g, (match, chr) => chr.toUpperCase());
  25. }
  26. /**
  27. * Trim the specified substring off the string. If the string does not end
  28. * with the specified substring, this is a no-op.
  29. *
  30. * @param {string} haystack String to search in
  31. * @param {string} needle String to search for
  32. * @return {string}
  33. */
  34. function trimEnd(haystack, needle) {
  35. return haystack.endsWith(needle) ? haystack.slice(0, -needle.length) : haystack;
  36. }
  37. const KEBAB_REGEX = /[A-Z\u00C0-\u00D6\u00D8-\u00DE]/g;
  38. function kebabCase(str) {
  39. return str.replace(KEBAB_REGEX, match => `-${match.toLowerCase()}`);
  40. }
  41. const SPACES_REGEXP = /[\t\r\n\u0085\u2028\u2029]+/g;
  42. function replaceSpaces(str) {
  43. return str.replace(SPACES_REGEXP, ' ');
  44. }