[d24f17c] | 1 | /**
|
---|
| 2 | * Assigns (shallow copies) the properties of `src` onto `dest`, if the
|
---|
| 3 | * corresponding property on `dest` === `undefined`.
|
---|
| 4 | *
|
---|
| 5 | * @param {Object} dest The destination object.
|
---|
| 6 | * @param {Object} src The source object.
|
---|
| 7 | * @return {Object} The destination object (`dest`)
|
---|
| 8 | */
|
---|
| 9 | export function defaults(dest, src) {
|
---|
| 10 | for (var prop in src) {
|
---|
| 11 | if (src.hasOwnProperty(prop) && dest[prop] === undefined) {
|
---|
| 12 | dest[prop] = src[prop];
|
---|
| 13 | }
|
---|
| 14 | }
|
---|
| 15 | return dest;
|
---|
| 16 | }
|
---|
| 17 | /**
|
---|
| 18 | * Truncates the `str` at `len - ellipsisChars.length`, and adds the `ellipsisChars` to the
|
---|
| 19 | * end of the string (by default, two periods: '..'). If the `str` length does not exceed
|
---|
| 20 | * `len`, the string will be returned unchanged.
|
---|
| 21 | *
|
---|
| 22 | * @param {String} str The string to truncate and add an ellipsis to.
|
---|
| 23 | * @param {Number} truncateLen The length to truncate the string at.
|
---|
| 24 | * @param {String} [ellipsisChars=...] The ellipsis character(s) to add to the end of `str`
|
---|
| 25 | * when truncated. Defaults to '...'
|
---|
| 26 | */
|
---|
| 27 | export function ellipsis(str, truncateLen, ellipsisChars) {
|
---|
| 28 | var ellipsisLength;
|
---|
| 29 | if (str.length > truncateLen) {
|
---|
| 30 | if (ellipsisChars == null) {
|
---|
| 31 | ellipsisChars = '…';
|
---|
| 32 | ellipsisLength = 3;
|
---|
| 33 | }
|
---|
| 34 | else {
|
---|
| 35 | ellipsisLength = ellipsisChars.length;
|
---|
| 36 | }
|
---|
| 37 | str = str.substring(0, truncateLen - ellipsisLength) + ellipsisChars;
|
---|
| 38 | }
|
---|
| 39 | return str;
|
---|
| 40 | }
|
---|
| 41 | /**
|
---|
| 42 | * Supports `Array.prototype.indexOf()` functionality for old IE (IE8 and below).
|
---|
| 43 | *
|
---|
| 44 | * @param {Array} arr The array to find an element of.
|
---|
| 45 | * @param {*} element The element to find in the array, and return the index of.
|
---|
| 46 | * @return {Number} The index of the `element`, or -1 if it was not found.
|
---|
| 47 | */
|
---|
| 48 | export function indexOf(arr, element) {
|
---|
| 49 | // @ts-ignore - As far as TypeScript is concerned, this method will always
|
---|
| 50 | // exist (lowest "lib" in TS is "ES5"). Hence we need to ignore this error
|
---|
| 51 | // to support IE8 which only implements ES3 and doesn't have this method
|
---|
| 52 | if (Array.prototype.indexOf) {
|
---|
| 53 | return arr.indexOf(element);
|
---|
| 54 | }
|
---|
| 55 | else {
|
---|
| 56 | for (var i = 0, len = arr.length; i < len; i++) {
|
---|
| 57 | if (arr[i] === element)
|
---|
| 58 | return i;
|
---|
| 59 | }
|
---|
| 60 | return -1;
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
| 63 | /**
|
---|
| 64 | * Removes array elements based on a filtering function. Mutates the input
|
---|
| 65 | * array.
|
---|
| 66 | *
|
---|
| 67 | * Using this instead of the ES5 Array.prototype.filter() function, to allow
|
---|
| 68 | * Autolinker compatibility with IE8, and also to prevent creating many new
|
---|
| 69 | * arrays in memory for filtering.
|
---|
| 70 | *
|
---|
| 71 | * @param {Array} arr The array to remove elements from. This array is
|
---|
| 72 | * mutated.
|
---|
| 73 | * @param {Function} fn A function which should return `true` to
|
---|
| 74 | * remove an element.
|
---|
| 75 | * @return {Array} The mutated input `arr`.
|
---|
| 76 | */
|
---|
| 77 | export function remove(arr, fn) {
|
---|
| 78 | for (var i = arr.length - 1; i >= 0; i--) {
|
---|
| 79 | if (fn(arr[i]) === true) {
|
---|
| 80 | arr.splice(i, 1);
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 | /**
|
---|
| 85 | * Performs the functionality of what modern browsers do when `String.prototype.split()` is called
|
---|
| 86 | * with a regular expression that contains capturing parenthesis.
|
---|
| 87 | *
|
---|
| 88 | * For example:
|
---|
| 89 | *
|
---|
| 90 | * // Modern browsers:
|
---|
| 91 | * "a,b,c".split( /(,)/ ); // --> [ 'a', ',', 'b', ',', 'c' ]
|
---|
| 92 | *
|
---|
| 93 | * // Old IE (including IE8):
|
---|
| 94 | * "a,b,c".split( /(,)/ ); // --> [ 'a', 'b', 'c' ]
|
---|
| 95 | *
|
---|
| 96 | * This method emulates the functionality of modern browsers for the old IE case.
|
---|
| 97 | *
|
---|
| 98 | * @param {String} str The string to split.
|
---|
| 99 | * @param {RegExp} splitRegex The regular expression to split the input `str` on. The splitting
|
---|
| 100 | * character(s) will be spliced into the array, as in the "modern browsers" example in the
|
---|
| 101 | * description of this method.
|
---|
| 102 | * Note #1: the supplied regular expression **must** have the 'g' flag specified.
|
---|
| 103 | * Note #2: for simplicity's sake, the regular expression does not need
|
---|
| 104 | * to contain capturing parenthesis - it will be assumed that any match has them.
|
---|
| 105 | * @return {String[]} The split array of strings, with the splitting character(s) included.
|
---|
| 106 | */
|
---|
| 107 | export function splitAndCapture(str, splitRegex) {
|
---|
| 108 | if (!splitRegex.global)
|
---|
| 109 | throw new Error("`splitRegex` must have the 'g' flag set");
|
---|
| 110 | var result = [], lastIdx = 0, match;
|
---|
| 111 | while ((match = splitRegex.exec(str))) {
|
---|
| 112 | result.push(str.substring(lastIdx, match.index));
|
---|
| 113 | result.push(match[0]); // push the splitting char(s)
|
---|
| 114 | lastIdx = match.index + match[0].length;
|
---|
| 115 | }
|
---|
| 116 | result.push(str.substring(lastIdx));
|
---|
| 117 | return result;
|
---|
| 118 | }
|
---|
| 119 | /**
|
---|
| 120 | * Function that should never be called but is used to check that every
|
---|
| 121 | * enum value is handled using TypeScript's 'never' type.
|
---|
| 122 | */
|
---|
| 123 | export function throwUnhandledCaseError(theValue) {
|
---|
| 124 | throw new Error("Unhandled case for value: '".concat(theValue, "'"));
|
---|
| 125 | }
|
---|
| 126 | //# sourceMappingURL=utils.js.map |
---|