-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlodash-addons.js
715 lines (660 loc) · 16.2 KB
/
lodash-addons.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
/* Lodash Addons
* @version 2.0.1
* https://github.com/helion3/lodash-addons
* @copyright Copyright 2015 Helion3, and other contributors
* @license Licensed under MIT
* see https://github.com/helion3/lodash-addons/blob/master/LICENSE
*/
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('lodash')) :
typeof define === 'function' && define.amd ? define(['lodash'], factory) :
(global.LodashAddons = factory(global._));
}(this, (function (_) { 'use strict';
/**
* Throw a TypeError if value doesn't match one of any provided validation methods.
*
* @static
* @memberOf _
* @category Preconditions
* @param {mixed} value Value
* @return {void}
*/
function check(value) {
var valid = false;
for (var _len = arguments.length, validators = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
validators[_key - 1] = arguments[_key];
}
_.each(validators, function (validator) {
return !(valid = validator(value));
});
if (!valid) {
throw new TypeError('Argument is not any of the accepted types.');
}
}
/**
* Iterates over keys of a collection, returning an array of all keys predicate returns truthy for.
* The predicate is invoked with three arguments: (value, index|key, collection).
*
* @static
* @memberOf _
* @category Collection
* @param {object} collection The object to iterate over.
* @param {function} iteratee The function invoked per iteration.
* @return {array} Resulting keys
*/
function filterKeys(collection, iteratee) {
return _.transform(collection, function (results, val, key) {
if (iteratee(val, key, collection)) {
results.push(key);
}
}, []);
}
/**
* Gets indices for which elements differ between two arrays.
*
* @static
* @memberOf _
* @category Collection
* @param {array} first First array
* @param {array} second Second array
* @return {array} Array of indices of differing elements
* @example
*
* _.differenceKeys([false, true], [false, false]);
* // => [1]
*/
function differenceKeys(first, second) {
return filterKeys(first, function (val, key) {
return val !== second[key];
});
}
/**
* Base function for returning a default when the given value fails validation.
*
* @private
* @param {function} validator Validation function.
* @param {*} baseDefault Base default value.
* @param {*} value Given value.
* @param {*} replacement Custom replacement.
* @return {*} Final value.
*/
function baseGetType(validator, baseDefault, value, replacement) {
var result = void 0;
if (validator(value)) {
result = value;
} else if (validator(replacement)) {
result = replacement;
} else {
result = baseDefault;
}
return result;
}
/**
* Returns value if a number, otherwise a default number.
*
* @static
* @memberOf _
* @category Lang
* @param {mixed} value Source value
* @param {number} replacement Custom default if value is invalid type.
* @return {number} Final number.
* @example
*
* _.getNumber('');
* // => 0
*
* _.getNumber('', 100);
* // => 100
*/
function getNumber(value, replacement) {
return baseGetType(_.isNumber, 0, value, replacement);
}
/**
* Generates a random alphanumeric string with length n.
*
* @static
* @memberOf _
* @category String
* @param {int} length Desired length.
* @return {string} String of random characters.
* @example
*
* _.generateKey(5);
* // => 'L7IpD'
*/
function generateKey(length) {
var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var possibleLength = possible.length - 1;
var text = '';
_.times(getNumber(length, 16), function () {
text += possible.charAt(_.random(possibleLength));
});
return text;
}
/**
* Returns value if an array, otherwise a default.
*
* @static
* @memberOf _
* @category Lang
* @param {mixed} value Source value
* @param {number} replacement Custom default if value is invalid type.
* @return {number} Final array.
* @example
*
* _.getArray(null);
* // => []
*
* _.getArray(null, ['test']);
* // => ['test']
*/
function getArray(value, replacement) {
return baseGetType(_.isArray, [], value, replacement);
}
/**
* Returns value if a boolean, otherwise a default boolean.
*
* @static
* @memberOf _
* @category Lang
* @param {mixed} value Source value
* @param {number} replacement Custom default if value is invalid type.
* @return {number} Final boolean.
* @example
*
* _.getBoolean(null);
* // => false
*
* _.getBoolean(null, true);
* // => true
*/
function getBoolean(value, replacement) {
return baseGetType(_.isBoolean, false, value, replacement);
}
/**
* Returns value if a finite number, otherwise a default number.
*
* @static
* @memberOf _
* @category Lang
* @param {mixed} value Source value
* @param {number} replacement Custom default if value is invalid type.
* @return {number} Final number.
* @example
*
* _.getFinite('');
* // => 0
*
* _.getFinite('', 100);
* // => 100
*
* _.getFinite(NaN, 25);
* // => 25
*/
function getFinite(value, replacement) {
return baseGetType(_.isFinite, 0, value, replacement);
}
/**
* Returns value if a function, otherwise a default function.
*
* @static
* @memberOf _
* @category Lang
* @param {mixed} value Source value
* @param {number} replacement Custom default if value is invalid type.
* @return {number} Final function.
* @example
*
* _.getFunction(null);
* // => function () {}
*/
function getFunction(value, replacement) {
return baseGetType(_.isFunction, _.noop, value, replacement);
}
/**
* Returns value if a Map, otherwise a default map.
*
* @static
* @memberOf _
* @category Lang
* @param {mixed} value Source value
* @param {number} replacement Custom default if value is invalid type.
* @return {number} Final number.
*/
function getMap(value, replacement) {
return baseGetType(_.isMap, new Map(), value, replacement);
}
/**
* Returns value if a object, otherwise a default object.
*
* @static
* @memberOf _
* @category Lang
* @param {mixed} value Source value
* @param {number} replacement Custom default if value is invalid type.
* @return {number} Final object.
* @example
*
* _.getObject('');
* // => {}
*/
function getObject(value, replacement) {
return baseGetType(_.isObject, {}, value, replacement);
}
/**
* Returns value if a plain object, otherwise a default object.
*
* @static
* @memberOf _
* @category Lang
* @param {mixed} value Source value
* @param {number} replacement Custom default if value is invalid type.
* @return {number} Final object.
* @example
*
* _.getPlainObject('');
* // => {}
*/
function getPlainObject(value, replacement) {
return baseGetType(_.isPlainObject, {}, value, replacement);
}
/**
* Gets the prototype for the given value.
*
* @static
* @memberOf _
* @category Util
* @param {*} value Source value
* @return {object} Found prototype or undefined.
* @example
*
* _.getPrototype(5);
* // => { toFixed: func(), ... }
*/
function getPrototype(value) {
var prototype = void 0;
if (!_.isUndefined(value) && !_.isNull(value)) {
if (!_.isObject(value)) {
prototype = value.constructor.prototype;
} else if (_.isFunction(value)) {
prototype = value.prototype;
} else {
prototype = Object.getPrototypeOf(value);
}
}
return prototype;
}
/**
* Returns value if a Set, otherwise a default set.
*
* @static
* @memberOf _
* @category Lang
* @param {mixed} value Source value
* @param {set} replacement Custom default if value is invalid type.
* @return {set} Final Set.
* @example
*
* _.getSet('');
* // => Set()
*/
function getSet(value, replacement) {
return baseGetType(_.isSet, new Set(), value, replacement);
}
/**
* Returns value if a string, otherwise a default string.
*
* @static
* @memberOf _
* @category Lang
* @param {mixed} value Source value
* @param {number} replacement Custom default if value is invalid type.
* @return {number} Final string.
* @example
*
* _.getString(false);
* // => ''
*/
function getString(value, replacement) {
return baseGetType(_.isString, '', value, replacement);
}
/**
* Returns value if a WeakMap, otherwise a default WeakMap.
*
* @static
* @memberOf _
* @category Lang
* @param {mixed} value Source value
* @param {weakmap} replacement Custom default if value is invalid type.
* @return {weakmap} Final map.
* @example
*
* _.getWeakMap(false);
* // => ''
*/
function getWeakMap(value, replacement) {
return baseGetType(_.isWeakMap, new WeakMap(), value, replacement);
}
/**
* Returns value if a WeakSet, otherwise a default WeakSet.
*
* @static
* @memberOf _
* @category Lang
* @param {mixed} value Source value
* @param {weakset} replacement Custom default if value is invalid type.
* @return {weakset} Final set.
* @example
*
* _.getWeakSet(false);
* // => ''
*/
function getWeakSet(value, replacement) {
return baseGetType(_.isWeakSet, new WeakSet(), value, replacement);
}
/**
* If _.hasIn returns true, run a validator on value.
*
* @static
* @memberOf _
* @category Object
* @param {mixed} value Collection for _.hasIn
* @param {string|number} path Path.
* @param {function} validator Function to validate value.
* @return {boolean} Whether collection has the path and it passes validation
*/
function hasInOfType(value, path, validator) {
return _.hasIn(value, path) ? validator(_.get(value, path)) : false;
}
/**
* If _.has returns true, run a validator on value.
*
* @static
* @memberOf _
* @category Object
* @param {mixed} value Collection for _.has
* @param {string} path Path
* @param {function} validator Function to validate value.
* @return {boolean} Whether collection has prop, and it passes validation
* @example
*
* _.hasOfType({ test: '' }, 'test', _.isString);
* // => true
*/
function hasOfType(value, path, validator) {
return _.has(value, path) ? validator(_.get(value, path)) : false;
}
/**
* Converts a value to a boolean.
*
* @static
* @memberOf _
* @category Lang
* @param {*} value Source value
* @return {boolean} Resulting boolean
* @example
*
* _.toBool(1);
* // => true
*/
function toBool(value) {
return Boolean(value);
}
/**
* Checks if value is iterable.
*
* @static
* @memberOf _
* @category Lang
* @param {object} object An object
* @return {boolean} True if iterable
* @example
*
* _.isIterable([]);
* // => true
*/
function isIterable(object) {
return toBool(object) && _.isFunction(object[Symbol.iterator]);
}
/**
* Checks if value is a non-empty string.
*
* @static
* @memberOf _
* @category Lang
* @param {object} string String
* @return {boolean} True if a non-empty string.
* @example
*
* _.isNonEmptyString('');
* // => false
*/
function isNonEmptyString(string) {
return _.isString(string) && string.trim() !== '';
}
var slicedToArray = function () {
function sliceIterator(arr, i) {
var _arr = [];
var _n = true;
var _d = false;
var _e = undefined;
try {
for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
_arr.push(_s.value);
if (i && _arr.length === i) break;
}
} catch (err) {
_d = true;
_e = err;
} finally {
try {
if (!_n && _i["return"]) _i["return"]();
} finally {
if (_d) throw _e;
}
}
return _arr;
}
return function (arr, i) {
if (Array.isArray(arr)) {
return arr;
} else if (Symbol.iterator in Object(arr)) {
return sliceIterator(arr, i);
} else {
throw new TypeError("Invalid attempt to destructure non-iterable instance");
}
};
}();
/**
* Shorthand object creation when sole property is a variable, or a path.
*
* @static
* @memberOf _
* @category Object
* @param {[object]} object Existing object (optional)
* @param {string|number} path Property
* @param {mixed} value Value
* @return {object} Resulting object
* @example
*
* // To create a new object:
*
* _.objectWith('key', 'value');
* // => { key: 'value' }
*
* _.objectWith('a.deep.path', 'value');
* // => {
* a: {
* deep: {
* path: 'value'
* }
* }
* }
*
* // Using existing:
* _.objectWith({ a: 1 }, 'b', 2);
* // => { a: 1, b: 2 }
*/
function objectWith() {
var args = _.reverse(arguments);
var _$take = _.take(args, 2),
_$take2 = slicedToArray(_$take, 2),
value = _$take2[0],
path = _$take2[1];
var object = _.nth(args, 2) || {};
return _.set(object, path, value);
}
/**
* Parses query string into key/value object.
*
* @static
* @memberOf _
* @category Object
* @param {string} string Query string.
* @return {object} Key/value map.
* @example
*
* _.parseQueryString('key=value');
* // => { key: 'value' }
*/
function parseQueryString(string) {
return _.transform(_.toString(string).split('&'), function (result, segment) {
var split = segment.split('=');
result[decodeURIComponent(split[0])] = decodeURIComponent(split[1]);
}, {});
}
/**
* Returns a number representing the sign of `value`.
*
* If `value` is a positive number, negative number, positive zero or negative zero,
* the function will return 1, -1, 0 or -0 respectively. Otherwise, NaN is returned.
*
* @static
* @memberOf _
* @category Math
* @param {number} value A number
* @returns {number} A number representing the sign
* @example
*
* _.sign(10);
* // => 1
*
* _.sign(-10);
* // => -1
*/
function sign(value) {
var sign = NaN;
if (_.isNumber(value)) {
if (value === 0) {
sign = value;
} else if (value >= 1) {
sign = 1;
} else if (value <= -1) {
sign = -1;
}
}
return sign;
}
/**
* Generates a url-safe "slug" form of a string.
*
* @static
* @memberOf _
* @category String
* @param {string} string String value.
* @return {string} URL-safe form of a string.
* @example
*
* _.slugify('A Test');
* // => a-test
*/
function slugify(string) {
return _.deburr(string).trim().toLowerCase().replace(/ /g, '-').replace(/([^a-zA-Z0-9\._-]+)/, '');
}
/**
* Parses a value by passing it to new Date().
*
* @static
* @memberOf _
* @category Date
* @param {string} val Value to be parsed
* @return {Date} Resulting date
*/
function parseDate(val) {
return new Date(val);
}
/**
* Converts an object's key/values to a query string.
*
* @static
* @memberOf _
* @category Object
* @param {object} object Source key/value collection
* @return {string} Query string
* @example
*
* _.toQueryString({ a: 1, b: 2 });
* // => a=1&b=2
*/
function toQueryString(object) {
return _.transform(object, function (results, value, key) {
results.push(encodeURIComponent(key) + '=' + encodeURIComponent(value));
}, []).join('&');
}
/**
* Transforms a value in each element of collection if the path is not undefined.
*
* @static
* @memberOf _
* @category Array
* @param {Array} collection Array of objects
* @param {string} path The path of the value to transform
* @param {function} transformer Callback which returns the transformed value
* @return {Array} Returns the array of results.
*/
function transformValueMap(collection, path, transformer) {
_.each(collection, function (element) {
var val = _.get(element, path);
if (!_.isUndefined(val)) {
_.set(element, path, transformer(val));
}
});
return collection;
}
_.mixin({
check: check,
differenceKeys: differenceKeys,
filterKeys: filterKeys,
generateKey: generateKey,
getArray: getArray,
getBoolean: getBoolean,
getFinite: getFinite,
getFunction: getFunction,
getMap: getMap,
getNumber: getNumber,
getObject: getObject,
getPlainObject: getPlainObject,
getPrototype: getPrototype,
getSet: getSet,
getString: getString,
getWeakMap: getWeakMap,
getWeakSet: getWeakSet,
hasInOfType: hasInOfType,
hasOfType: hasOfType,
isIterable: isIterable,
isNonEmptyString: isNonEmptyString,
objectWith: objectWith,
parseQueryString: parseQueryString,
sign: sign,
slugify: slugify,
toBool: toBool,
toDate: parseDate,
toQueryString: toQueryString,
transformValueMap: transformValueMap,
// Aliases
fromQueryString: parseQueryString,
parseDate: parseDate,
with: objectWith
});
return _;
})));