a3991e8aae83b82e72723955ca2bcd29e091de68c03a32ae3ee8ccdaa1118f6411f26438719e009486ee22fdb6c3f6ad1973da1d171185a490339f7be5f5cc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import {stringify} from './mixed';
  2. import {rangeEach} from './number';
  3. /**
  4. * Convert string to upper case first letter.
  5. *
  6. * @param {String} string String to convert.
  7. * @returns {String}
  8. */
  9. export function toUpperCaseFirst(string) {
  10. return string[0].toUpperCase() + string.substr(1);
  11. }
  12. /**
  13. * Compare strings case insensitively.
  14. *
  15. * @param {...String} strings Strings to compare.
  16. * @returns {Boolean}
  17. */
  18. export function equalsIgnoreCase(...strings) {
  19. let unique = [];
  20. let length = strings.length;
  21. while (length--) {
  22. let string = stringify(strings[length]).toLowerCase();
  23. if (unique.indexOf(string) === -1) {
  24. unique.push(string);
  25. }
  26. }
  27. return unique.length === 1;
  28. }
  29. /**
  30. * Generates a random hex string. Used as namespace for Handsontable instance events.
  31. *
  32. * @return {String} Returns 16-long character random string (eq. `'92b1bfc74ec4'`).
  33. */
  34. export function randomString() {
  35. function s4() {
  36. return Math.floor((1 + Math.random()) * 0x10000)
  37. .toString(16)
  38. .substring(1);
  39. }
  40. return s4() + s4() + s4() + s4();
  41. }
  42. /**
  43. * Checks if value is valid percent.
  44. *
  45. * @param {String} value
  46. * @returns {Boolean}
  47. */
  48. export function isPercentValue(value) {
  49. return /^([0-9][0-9]?%$)|(^100%$)/.test(value);
  50. }
  51. /**
  52. * Substitute strings placed beetwen square brackets into value defined in `variables` object. String names defined in
  53. * square brackets must be the same as property name of `variables` object.
  54. *
  55. * @param {String} template Template string.
  56. * @param {Object} variables Object which contains all available values which can be injected into template.
  57. * @returns {String}
  58. */
  59. export function substitute(template, variables = {}) {
  60. return (`${template}`).replace(/(?:\\)?\[([^[\]]+)]/g, (match, name) => {
  61. if (match.charAt(0) === '\\') {
  62. return match.substr(1, match.length - 1);
  63. }
  64. return variables[name] === void 0 ? '' : variables[name];
  65. });
  66. }
  67. const STRIP_TAGS_REGEX = /<\/?\w+\/?>|<\w+[\s|/][^>]*>/gi;
  68. /**
  69. * Strip any HTML tag from the string.
  70. *
  71. * @param {String} string String to cut HTML from.
  72. * @return {String}
  73. */
  74. export function stripTags(string) {
  75. string += '';
  76. return string.replace(STRIP_TAGS_REGEX, '');
  77. }