Debian Codegraph @

src/Util.js

raw symbols SCIP
1 /*global Autolinker */
2 /*jshint eqnull:true, boss:true */
3 /**
4 * @class Autolinker.Util
5 * @singleton
6 *
7 * A few utility methods for Autolinker.
8 */
9 Autolinker.Util = {
10
11 /**
12 * @property {Function} abstractMethod
13 *
14 * A function object which represents an abstract method.
15 */
16 abstractMethod : function() { throw "abstract"; },
17
18
19 /**
20 * @private
21 * @property {RegExp} trimRegex
22 *
23 * The regular expression used to trim the leading and trailing whitespace
24 * from a string.
25 */
26 trimRegex : /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,
27
28
29 /**
30 * Assigns (shallow copies) the properties of `src` onto `dest`.
31 *
32 * @param {Object} dest The destination object.
33 * @param {Object} src The source object.
34 * @return {Object} The destination object (`dest`)
35 */
36 assign : function( dest, src ) {
37 for( var prop in src ) {
38 if( src.hasOwnProperty( prop ) ) {
39 dest[ prop ] = src[ prop ];
40 }
41 }
42
43 return dest;
44 },
45
46
47 /**
48 * Assigns (shallow copies) the properties of `src` onto `dest`, if the
49 * corresponding property on `dest` === `undefined`.
50 *
51 * @param {Object} dest The destination object.
52 * @param {Object} src The source object.
53 * @return {Object} The destination object (`dest`)
54 */
55 defaults : function( dest, src ) {
56 for( var prop in src ) {
57 if( src.hasOwnProperty( prop ) && dest[ prop ] === undefined ) {
58 dest[ prop ] = src[ prop ];
59 }
60 }
61
62 return dest;
63 },
64
65
66 /**
67 * Extends `superclass` to create a new subclass, adding the `protoProps` to the new subclass's prototype.
68 *
69 * @param {Function} superclass The constructor function for the superclass.
70 * @param {Object} protoProps The methods/properties to add to the subclass's prototype. This may contain the
71 * special property `constructor`, which will be used as the new subclass's constructor function.
72 * @return {Function} The new subclass function.
73 */
74 extend : function( superclass, protoProps ) {
75 var superclassProto = superclass.prototype;
76
77 var F = function() {};
78 F.prototype = superclassProto;
79
80 var subclass;
81 if( protoProps.hasOwnProperty( 'constructor' ) ) {
82 subclass = protoProps.constructor;
83 } else {
84 subclass = function() { superclassProto.constructor.apply( this, arguments ); };
85 }
86
87 var subclassProto = subclass.prototype = new F(); // set up prototype chain
88 subclassProto.constructor = subclass; // fix constructor property
89 subclassProto.superclass = superclassProto;
90
91 delete protoProps.constructor; // don't re-assign constructor property to the prototype, since a new function may have been created (`subclass`), which is now already there
92 Autolinker.Util.assign( subclassProto, protoProps );
93
94 return subclass;
95 },
96
97
98 /**
99 * Truncates the `str` at `len - ellipsisChars.length`, and adds the `ellipsisChars` to the
100 * end of the string (by default, two periods: '..'). If the `str` length does not exceed
101 * `len`, the string will be returned unchanged.
102 *
103 * @param {String} str The string to truncate and add an ellipsis to.
104 * @param {Number} truncateLen The length to truncate the string at.
105 * @param {String} [ellipsisChars=...] The ellipsis character(s) to add to the end of `str`
106 * when truncated. Defaults to '...'
107 */
108 ellipsis : function( str, truncateLen, ellipsisChars ) {
109 var ellipsisLength;
110
111 if( str.length > truncateLen ) {
112 if(ellipsisChars == null) {
113 ellipsisChars = '…';
114 ellipsisLength = 3;
115 } else {
116 ellipsisLength = ellipsisChars.length;
117 }
118
119 str = str.substring( 0, truncateLen - ellipsisLength ) + ellipsisChars;
120 }
121 return str;
122 },
123
124
125 /**
126 * Supports `Array.prototype.indexOf()` functionality for old IE (IE8 and below).
127 *
128 * @param {Array} arr The array to find an element of.
129 * @param {*} element The element to find in the array, and return the index of.
130 * @return {Number} The index of the `element`, or -1 if it was not found.
131 */
132 indexOf : function( arr, element ) {
133 if( Array.prototype.indexOf ) {
134 return arr.indexOf( element );
135
136 } else {
137 for( var i = 0, len = arr.length; i < len; i++ ) {
138 if( arr[ i ] === element ) return i;
139 }
140 return -1;
141 }
142 },
143
144
145 /**
146 * Removes array elements based on a filtering function. Mutates the input
147 * array.
148 *
149 * Using this instead of the ES5 Array.prototype.filter() function, to allow
150 * Autolinker compatibility with IE8, and also to prevent creating many new
151 * arrays in memory for filtering.
152 *
153 * @param {Array} arr The array to remove elements from. This array is
154 * mutated.
155 * @param {Function} fn A function which should return `true` to
156 * remove an element.
157 * @return {Array} The mutated input `arr`.
158 */
159 remove : function( arr, fn ) {
160 for( var i = arr.length - 1; i >= 0; i-- ) {
161 if( fn( arr[ i ] ) === true ) {
162 arr.splice( i, 1 );
163 }
164 }
165 },
166
167
168 /**
169 * Performs the functionality of what modern browsers do when `String.prototype.split()` is called
170 * with a regular expression that contains capturing parenthesis.
171 *
172 * For example:
173 *
174 * // Modern browsers:
175 * "a,b,c".split( /(,)/ ); // --> [ 'a', ',', 'b', ',', 'c' ]
176 *
177 * // Old IE (including IE8):
178 * "a,b,c".split( /(,)/ ); // --> [ 'a', 'b', 'c' ]
179 *
180 * This method emulates the functionality of modern browsers for the old IE case.
181 *
182 * @param {String} str The string to split.
183 * @param {RegExp} splitRegex The regular expression to split the input `str` on. The splitting
184 * character(s) will be spliced into the array, as in the "modern browsers" example in the
185 * description of this method.
186 * Note #1: the supplied regular expression **must** have the 'g' flag specified.
187 * Note #2: for simplicity's sake, the regular expression does not need
188 * to contain capturing parenthesis - it will be assumed that any match has them.
189 * @return {String[]} The split array of strings, with the splitting character(s) included.
190 */
191 splitAndCapture : function( str, splitRegex ) {
192 // @if DEBUG
193 if( !splitRegex.global ) throw new Error( "`splitRegex` must have the 'g' flag set" );
194 // @endif
195
196 var result = [],
197 lastIdx = 0,
198 match;
199
200 while( match = splitRegex.exec( str ) ) {
201 result.push( str.substring( lastIdx, match.index ) );
202 result.push( match[ 0 ] ); // push the splitting char(s)
203
204 lastIdx = match.index + match[ 0 ].length;
205 }
206 result.push( str.substring( lastIdx ) );
207
208 return result;
209 },
210
211
212 /**
213 * Trims the leading and trailing whitespace from a string.
214 *
215 * @param {String} str The string to trim.
216 * @return {String}
217 */
218 trim : function( str ) {
219 return str.replace( this.trimRegex, '' );
220 }
221
222 };