No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

string-utils.js 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /**
  2. * @license
  3. * Copyright (c) 2016, Contributors
  4. * SPDX-License-Identifier: ISC
  5. */
  6. export function camelCase(str) {
  7. // Handle the case where an argument is provided as camel case, e.g., fooBar.
  8. // by ensuring that the string isn't already mixed case:
  9. const isCamelCase = str !== str.toLowerCase() && str !== str.toUpperCase();
  10. if (!isCamelCase) {
  11. str = str.toLowerCase();
  12. }
  13. if (str.indexOf('-') === -1 && str.indexOf('_') === -1) {
  14. return str;
  15. }
  16. else {
  17. let camelcase = '';
  18. let nextChrUpper = false;
  19. const leadingHyphens = str.match(/^-+/);
  20. for (let i = leadingHyphens ? leadingHyphens[0].length : 0; i < str.length; i++) {
  21. let chr = str.charAt(i);
  22. if (nextChrUpper) {
  23. nextChrUpper = false;
  24. chr = chr.toUpperCase();
  25. }
  26. if (i !== 0 && (chr === '-' || chr === '_')) {
  27. nextChrUpper = true;
  28. }
  29. else if (chr !== '-' && chr !== '_') {
  30. camelcase += chr;
  31. }
  32. }
  33. return camelcase;
  34. }
  35. }
  36. export function decamelize(str, joinString) {
  37. const lowercase = str.toLowerCase();
  38. joinString = joinString || '-';
  39. let notCamelcase = '';
  40. for (let i = 0; i < str.length; i++) {
  41. const chrLower = lowercase.charAt(i);
  42. const chrString = str.charAt(i);
  43. if (chrLower !== chrString && i > 0) {
  44. notCamelcase += `${joinString}${lowercase.charAt(i)}`;
  45. }
  46. else {
  47. notCamelcase += chrString;
  48. }
  49. }
  50. return notCamelcase;
  51. }
  52. export function looksLikeNumber(x) {
  53. if (x === null || x === undefined)
  54. return false;
  55. // if loaded from config, may already be a number.
  56. if (typeof x === 'number')
  57. return true;
  58. // hexadecimal.
  59. if (/^0x[0-9a-f]+$/i.test(x))
  60. return true;
  61. // don't treat 0123 as a number; as it drops the leading '0'.
  62. if (/^0[^.]/.test(x))
  63. return false;
  64. return /^[-]?(?:\d+(?:\.\d*)?|\.\d+)(e[-+]?\d+)?$/.test(x);
  65. }