UNPKG

10.5 kBJavaScriptView Raw
1/**
2 * lodash 4.0.1 (Custom Build) <https://lodash.com/>
3 * Build: `lodash modularize exports="npm" -o ./`
4 * Copyright 2012-2016 The Dojo Foundation <http://dojofoundation.org/>
5 * Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
6 * Copyright 2009-2016 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
7 * Available under MIT license <https://lodash.com/license>
8 */
9var repeat = require('lodash.repeat');
10
11/** Used as references for various `Number` constants. */
12var INFINITY = 1 / 0,
13 MAX_INTEGER = 1.7976931348623157e+308,
14 NAN = 0 / 0;
15
16/** `Object#toString` result references. */
17var funcTag = '[object Function]',
18 genTag = '[object GeneratorFunction]',
19 symbolTag = '[object Symbol]';
20
21/** Used to match leading and trailing whitespace. */
22var reTrim = /^\s+|\s+$/g;
23
24/** Used to detect bad signed hexadecimal string values. */
25var reIsBadHex = /^[-+]0x[0-9a-f]+$/i;
26
27/** Used to detect binary string values. */
28var reIsBinary = /^0b[01]+$/i;
29
30/** Used to detect octal string values. */
31var reIsOctal = /^0o[0-7]+$/i;
32
33/** Used to compose unicode character classes. */
34var rsAstralRange = '\\ud800-\\udfff',
35 rsComboMarksRange = '\\u0300-\\u036f\\ufe20-\\ufe23',
36 rsComboSymbolsRange = '\\u20d0-\\u20f0',
37 rsVarRange = '\\ufe0e\\ufe0f';
38
39/** Used to compose unicode capture groups. */
40var rsAstral = '[' + rsAstralRange + ']',
41 rsCombo = '[' + rsComboMarksRange + rsComboSymbolsRange + ']',
42 rsFitz = '\\ud83c[\\udffb-\\udfff]',
43 rsModifier = '(?:' + rsCombo + '|' + rsFitz + ')',
44 rsNonAstral = '[^' + rsAstralRange + ']',
45 rsRegional = '(?:\\ud83c[\\udde6-\\uddff]){2}',
46 rsSurrPair = '[\\ud800-\\udbff][\\udc00-\\udfff]',
47 rsZWJ = '\\u200d';
48
49/** Used to compose unicode regexes. */
50var reOptMod = rsModifier + '?',
51 rsOptVar = '[' + rsVarRange + ']?',
52 rsOptJoin = '(?:' + rsZWJ + '(?:' + [rsNonAstral, rsRegional, rsSurrPair].join('|') + ')' + rsOptVar + reOptMod + ')*',
53 rsSeq = rsOptVar + reOptMod + rsOptJoin,
54 rsSymbol = '(?:' + [rsNonAstral + rsCombo + '?', rsCombo, rsRegional, rsSurrPair, rsAstral].join('|') + ')';
55
56/** Used to match [string symbols](https://mathiasbynens.be/notes/javascript-unicode). */
57var reComplexSymbol = RegExp(rsFitz + '(?=' + rsFitz + ')|' + rsSymbol + rsSeq, 'g');
58
59/** Used to detect strings with [zero-width joiners or code points from the astral planes](http://eev.ee/blog/2015/09/12/dark-corners-of-unicode/). */
60var reHasComplexSymbol = RegExp('[' + rsZWJ + rsAstralRange + rsComboMarksRange + rsComboSymbolsRange + rsVarRange + ']');
61
62/** Built-in method references without a dependency on `global`. */
63var freeParseInt = parseInt;
64
65/**
66 * Gets the number of symbols in `string`.
67 *
68 * @private
69 * @param {string} string The string to inspect.
70 * @returns {number} Returns the string size.
71 */
72function stringSize(string) {
73 if (!(string && reHasComplexSymbol.test(string))) {
74 return string.length;
75 }
76 var result = reComplexSymbol.lastIndex = 0;
77 while (reComplexSymbol.test(string)) {
78 result++;
79 }
80 return result;
81}
82
83/**
84 * Converts `string` to an array.
85 *
86 * @private
87 * @param {string} string The string to convert.
88 * @returns {Array} Returns the converted array.
89 */
90function stringToArray(string) {
91 return string.match(reComplexSymbol);
92}
93
94/** Used for built-in method references. */
95var objectProto = global.Object.prototype;
96
97/**
98 * Used to resolve the [`toStringTag`](http://ecma-international.org/ecma-262/6.0/#sec-object.prototype.tostring)
99 * of values.
100 */
101var objectToString = objectProto.toString;
102
103/** Built-in value references. */
104var Symbol = global.Symbol;
105
106/* Built-in method references for those with the same name as other `lodash` methods. */
107var nativeCeil = Math.ceil;
108
109/** Used to convert symbols to primitives and strings. */
110var symbolProto = Symbol ? Symbol.prototype : undefined,
111 symbolToString = Symbol ? symbolProto.toString : undefined;
112
113/**
114 * Creates the padding for `string` based on `length`. The `chars` string
115 * is truncated if the number of characters exceeds `length`.
116 *
117 * @private
118 * @param {string} string The string to create padding for.
119 * @param {number} [length=0] The padding length.
120 * @param {string} [chars=' '] The string used as padding.
121 * @returns {string} Returns the padding for `string`.
122 */
123function createPadding(string, length, chars) {
124 length = toInteger(length);
125
126 var strLength = stringSize(string);
127 if (!length || strLength >= length) {
128 return '';
129 }
130 var padLength = length - strLength;
131 chars = chars === undefined ? ' ' : (chars + '');
132
133 var result = repeat(chars, nativeCeil(padLength / stringSize(chars)));
134 return reHasComplexSymbol.test(chars)
135 ? stringToArray(result).slice(0, padLength).join('')
136 : result.slice(0, padLength);
137}
138
139/**
140 * Checks if `value` is classified as a `Function` object.
141 *
142 * @static
143 * @memberOf _
144 * @category Lang
145 * @param {*} value The value to check.
146 * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`.
147 * @example
148 *
149 * _.isFunction(_);
150 * // => true
151 *
152 * _.isFunction(/abc/);
153 * // => false
154 */
155function isFunction(value) {
156 // The use of `Object#toString` avoids issues with the `typeof` operator
157 // in Safari 8 which returns 'object' for typed array constructors, and
158 // PhantomJS 1.9 which returns 'function' for `NodeList` instances.
159 var tag = isObject(value) ? objectToString.call(value) : '';
160 return tag == funcTag || tag == genTag;
161}
162
163/**
164 * Checks if `value` is the [language type](https://es5.github.io/#x8) of `Object`.
165 * (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
166 *
167 * @static
168 * @memberOf _
169 * @category Lang
170 * @param {*} value The value to check.
171 * @returns {boolean} Returns `true` if `value` is an object, else `false`.
172 * @example
173 *
174 * _.isObject({});
175 * // => true
176 *
177 * _.isObject([1, 2, 3]);
178 * // => true
179 *
180 * _.isObject(_.noop);
181 * // => true
182 *
183 * _.isObject(null);
184 * // => false
185 */
186function isObject(value) {
187 // Avoid a V8 JIT bug in Chrome 19-20.
188 // See https://code.google.com/p/v8/issues/detail?id=2291 for more details.
189 var type = typeof value;
190 return !!value && (type == 'object' || type == 'function');
191}
192
193/**
194 * Checks if `value` is object-like. A value is object-like if it's not `null`
195 * and has a `typeof` result of "object".
196 *
197 * @static
198 * @memberOf _
199 * @category Lang
200 * @param {*} value The value to check.
201 * @returns {boolean} Returns `true` if `value` is object-like, else `false`.
202 * @example
203 *
204 * _.isObjectLike({});
205 * // => true
206 *
207 * _.isObjectLike([1, 2, 3]);
208 * // => true
209 *
210 * _.isObjectLike(_.noop);
211 * // => false
212 *
213 * _.isObjectLike(null);
214 * // => false
215 */
216function isObjectLike(value) {
217 return !!value && typeof value == 'object';
218}
219
220/**
221 * Checks if `value` is classified as a `Symbol` primitive or object.
222 *
223 * @static
224 * @memberOf _
225 * @category Lang
226 * @param {*} value The value to check.
227 * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`.
228 * @example
229 *
230 * _.isSymbol(Symbol.iterator);
231 * // => true
232 *
233 * _.isSymbol('abc');
234 * // => false
235 */
236function isSymbol(value) {
237 return typeof value == 'symbol' ||
238 (isObjectLike(value) && objectToString.call(value) == symbolTag);
239}
240
241/**
242 * Converts `value` to an integer.
243 *
244 * **Note:** This function is loosely based on [`ToInteger`](http://www.ecma-international.org/ecma-262/6.0/#sec-tointeger).
245 *
246 * @static
247 * @memberOf _
248 * @category Lang
249 * @param {*} value The value to convert.
250 * @returns {number} Returns the converted integer.
251 * @example
252 *
253 * _.toInteger(3);
254 * // => 3
255 *
256 * _.toInteger(Number.MIN_VALUE);
257 * // => 0
258 *
259 * _.toInteger(Infinity);
260 * // => 1.7976931348623157e+308
261 *
262 * _.toInteger('3');
263 * // => 3
264 */
265function toInteger(value) {
266 if (!value) {
267 return value === 0 ? value : 0;
268 }
269 value = toNumber(value);
270 if (value === INFINITY || value === -INFINITY) {
271 var sign = (value < 0 ? -1 : 1);
272 return sign * MAX_INTEGER;
273 }
274 var remainder = value % 1;
275 return value === value ? (remainder ? value - remainder : value) : 0;
276}
277
278/**
279 * Converts `value` to a number.
280 *
281 * @static
282 * @memberOf _
283 * @category Lang
284 * @param {*} value The value to process.
285 * @returns {number} Returns the number.
286 * @example
287 *
288 * _.toNumber(3);
289 * // => 3
290 *
291 * _.toNumber(Number.MIN_VALUE);
292 * // => 5e-324
293 *
294 * _.toNumber(Infinity);
295 * // => Infinity
296 *
297 * _.toNumber('3');
298 * // => 3
299 */
300function toNumber(value) {
301 if (isObject(value)) {
302 var other = isFunction(value.valueOf) ? value.valueOf() : value;
303 value = isObject(other) ? (other + '') : other;
304 }
305 if (typeof value != 'string') {
306 return value === 0 ? value : +value;
307 }
308 value = value.replace(reTrim, '');
309 var isBinary = reIsBinary.test(value);
310 return (isBinary || reIsOctal.test(value))
311 ? freeParseInt(value.slice(2), isBinary ? 2 : 8)
312 : (reIsBadHex.test(value) ? NAN : +value);
313}
314
315/**
316 * Converts `value` to a string if it's not one. An empty string is returned
317 * for `null` and `undefined` values. The sign of `-0` is preserved.
318 *
319 * @static
320 * @memberOf _
321 * @category Lang
322 * @param {*} value The value to process.
323 * @returns {string} Returns the string.
324 * @example
325 *
326 * _.toString(null);
327 * // => ''
328 *
329 * _.toString(-0);
330 * // => '-0'
331 *
332 * _.toString([1, 2, 3]);
333 * // => '1,2,3'
334 */
335function toString(value) {
336 // Exit early for strings to avoid a performance hit in some environments.
337 if (typeof value == 'string') {
338 return value;
339 }
340 if (value == null) {
341 return '';
342 }
343 if (isSymbol(value)) {
344 return Symbol ? symbolToString.call(value) : '';
345 }
346 var result = (value + '');
347 return (result == '0' && (1 / value) == -INFINITY) ? '-0' : result;
348}
349
350/**
351 * Pads `string` on the left side if it's shorter than `length`. Padding
352 * characters are truncated if they exceed `length`.
353 *
354 * @static
355 * @memberOf _
356 * @category String
357 * @param {string} [string=''] The string to pad.
358 * @param {number} [length=0] The padding length.
359 * @param {string} [chars=' '] The string used as padding.
360 * @returns {string} Returns the padded string.
361 * @example
362 *
363 * _.padStart('abc', 6);
364 * // => ' abc'
365 *
366 * _.padStart('abc', 6, '_-');
367 * // => '_-_abc'
368 *
369 * _.padStart('abc', 3);
370 * // => 'abc'
371 */
372function padStart(string, length, chars) {
373 string = toString(string);
374 return createPadding(string, length, chars) + string;
375}
376
377module.exports = padStart;