utils.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.assertNever = exports.removeWithPredicate = exports.remove = exports.ellipsis = exports.defaults = exports.isBoolean = exports.isUndefined = void 0;
  4. /**
  5. * Simpler helper method to check for undefined simply for the benefit of
  6. * gaining better compression when minified by not needing to have multiple
  7. * comparisons to the `undefined` keyword in the codebase.
  8. */
  9. function isUndefined(value) {
  10. return value === undefined;
  11. }
  12. exports.isUndefined = isUndefined;
  13. /**
  14. * Simpler helper method to check for a boolean type simply for the benefit of
  15. * gaining better compression when minified by not needing to have multiple
  16. * `typeof` comparisons in the codebase.
  17. */
  18. function isBoolean(value) {
  19. return typeof value === 'boolean';
  20. }
  21. exports.isBoolean = isBoolean;
  22. /**
  23. * Assigns (shallow copies) the properties of `src` onto `dest`, if the
  24. * corresponding property on `dest` === `undefined`.
  25. *
  26. * @param {Object} dest The destination object.
  27. * @param {Object} src The source object.
  28. * @return {Object} The destination object (`dest`)
  29. */
  30. function defaults(dest, src) {
  31. for (var prop in src) {
  32. if (src.hasOwnProperty(prop) && isUndefined(dest[prop])) {
  33. dest[prop] = src[prop];
  34. }
  35. }
  36. return dest;
  37. }
  38. exports.defaults = defaults;
  39. /**
  40. * Truncates the `str` at `len - ellipsisChars.length`, and adds the `ellipsisChars` to the
  41. * end of the string (by default, two periods: '..'). If the `str` length does not exceed
  42. * `len`, the string will be returned unchanged.
  43. *
  44. * @param {String} str The string to truncate and add an ellipsis to.
  45. * @param {Number} truncateLen The length to truncate the string at.
  46. * @param {String} [ellipsisChars=...] The ellipsis character(s) to add to the end of `str`
  47. * when truncated. Defaults to '...'
  48. */
  49. function ellipsis(str, truncateLen, ellipsisChars) {
  50. var ellipsisLength;
  51. if (str.length > truncateLen) {
  52. if (ellipsisChars == null) {
  53. ellipsisChars = '…';
  54. ellipsisLength = 3;
  55. }
  56. else {
  57. ellipsisLength = ellipsisChars.length;
  58. }
  59. str = str.substring(0, truncateLen - ellipsisLength) + ellipsisChars;
  60. }
  61. return str;
  62. }
  63. exports.ellipsis = ellipsis;
  64. /**
  65. * Removes array elements by value. Mutates the input array.
  66. *
  67. * Using this instead of the ES5 Array.prototype.filter() function to prevent
  68. * creating many new arrays in memory for removing an element.
  69. *
  70. * @param arr The array to remove elements from. This array is mutated.
  71. * @param fn The element to remove.
  72. */
  73. function remove(arr, item) {
  74. for (var i = arr.length - 1; i >= 0; i--) {
  75. if (arr[i] === item) {
  76. arr.splice(i, 1);
  77. }
  78. }
  79. }
  80. exports.remove = remove;
  81. /**
  82. * Removes array elements based on a filtering function. Mutates the input
  83. * array.
  84. *
  85. * Using this instead of the ES5 Array.prototype.filter() function to prevent
  86. * creating many new arrays in memory for filtering.
  87. *
  88. * @param arr The array to remove elements from. This array is mutated.
  89. * @param fn The predicate function which should return `true` to remove an
  90. * element.
  91. */
  92. function removeWithPredicate(arr, fn) {
  93. for (var i = arr.length - 1; i >= 0; i--) {
  94. if (fn(arr[i]) === true) {
  95. arr.splice(i, 1);
  96. }
  97. }
  98. }
  99. exports.removeWithPredicate = removeWithPredicate;
  100. /**
  101. * Function that should never be called but is used to check that every
  102. * enum value is handled using TypeScript's 'never' type.
  103. */
  104. function assertNever(theValue) {
  105. throw new Error("Unhandled case for value: '".concat(theValue, "'"));
  106. }
  107. exports.assertNever = assertNever;
  108. //# sourceMappingURL=utils.js.map