utils.js 3.2 KB

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