source: node_modules/autolinker/dist/commonjs/utils.js@ d24f17c

main
Last change on this file since d24f17c was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

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