1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | function flatten(arr) {
|
11 | return Array.prototype.concat.apply([], arr);
|
12 | }
|
13 |
|
14 | const nativeToString = Object.prototype.toString;
|
15 | const nativeHasOwnProperty = Object.prototype.hasOwnProperty;
|
16 |
|
17 | function isUndefined(obj) {
|
18 | return obj === undefined;
|
19 | }
|
20 |
|
21 | function isDefined(obj) {
|
22 | return obj !== undefined;
|
23 | }
|
24 |
|
25 | function isNil(obj) {
|
26 | return obj == null;
|
27 | }
|
28 |
|
29 | function isArray(obj) {
|
30 | return nativeToString.call(obj) === '[object Array]';
|
31 | }
|
32 |
|
33 | function isObject(obj) {
|
34 | return nativeToString.call(obj) === '[object Object]';
|
35 | }
|
36 |
|
37 | function isNumber(obj) {
|
38 | return nativeToString.call(obj) === '[object Number]';
|
39 | }
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | function isFunction(obj) {
|
47 | const tag = nativeToString.call(obj);
|
48 |
|
49 | return (
|
50 | tag === '[object Function]' ||
|
51 | tag === '[object AsyncFunction]' ||
|
52 | tag === '[object GeneratorFunction]' ||
|
53 | tag === '[object AsyncGeneratorFunction]' ||
|
54 | tag === '[object Proxy]'
|
55 | );
|
56 | }
|
57 |
|
58 | function isString(obj) {
|
59 | return nativeToString.call(obj) === '[object String]';
|
60 | }
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | function ensureArray(obj) {
|
69 |
|
70 | if (isArray(obj)) {
|
71 | return;
|
72 | }
|
73 |
|
74 | throw new Error('must supply array');
|
75 | }
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | function has(target, key) {
|
86 | return nativeHasOwnProperty.call(target, key);
|
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 | function find(collection, matcher) {
|
150 |
|
151 | const matchFn = toMatcher(matcher);
|
152 |
|
153 | let match;
|
154 |
|
155 | forEach(collection, function(val, key) {
|
156 | if (matchFn(val, key)) {
|
157 | match = val;
|
158 |
|
159 | return false;
|
160 | }
|
161 | });
|
162 |
|
163 | return match;
|
164 |
|
165 | }
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 | function findIndex(collection, matcher) {
|
178 |
|
179 | const matchFn = toMatcher(matcher);
|
180 |
|
181 | let idx = isArray(collection) ? -1 : undefined;
|
182 |
|
183 | forEach(collection, function(val, key) {
|
184 | if (matchFn(val, key)) {
|
185 | idx = key;
|
186 |
|
187 | return false;
|
188 | }
|
189 | });
|
190 |
|
191 | return idx;
|
192 | }
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 | function filter(collection, matcher) {
|
205 |
|
206 | const matchFn = toMatcher(matcher);
|
207 |
|
208 | let result = [];
|
209 |
|
210 | forEach(collection, function(val, key) {
|
211 | if (matchFn(val, key)) {
|
212 | result.push(val);
|
213 | }
|
214 | });
|
215 |
|
216 | return result;
|
217 | }
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 | function forEach(collection, iterator) {
|
231 |
|
232 | let val,
|
233 | result;
|
234 |
|
235 | if (isUndefined(collection)) {
|
236 | return;
|
237 | }
|
238 |
|
239 | const convertKey = isArray(collection) ? toNum : identity;
|
240 |
|
241 | for (let key in collection) {
|
242 |
|
243 | if (has(collection, key)) {
|
244 | val = collection[key];
|
245 |
|
246 | result = iterator(val, convertKey(key));
|
247 |
|
248 | if (result === false) {
|
249 | return val;
|
250 | }
|
251 | }
|
252 | }
|
253 | }
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 | function without(arr, matcher) {
|
265 |
|
266 | if (isUndefined(arr)) {
|
267 | return [];
|
268 | }
|
269 |
|
270 | ensureArray(arr);
|
271 |
|
272 | const matchFn = toMatcher(matcher);
|
273 |
|
274 | return arr.filter(function(el, idx) {
|
275 | return !matchFn(el, idx);
|
276 | });
|
277 |
|
278 | }
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 | function reduce(collection, iterator, result) {
|
294 |
|
295 | forEach(collection, function(value, idx) {
|
296 | result = iterator(result, value, idx);
|
297 | });
|
298 |
|
299 | return result;
|
300 | }
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 | function every(collection, matcher) {
|
313 |
|
314 | return !!reduce(collection, function(matches, val, key) {
|
315 | return matches && matcher(val, key);
|
316 | }, true);
|
317 | }
|
318 |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 | function some(collection, matcher) {
|
330 |
|
331 | return !!find(collection, matcher);
|
332 | }
|
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 |
|
339 |
|
340 |
|
341 |
|
342 |
|
343 |
|
344 | function map(collection, fn) {
|
345 |
|
346 | let result = [];
|
347 |
|
348 | forEach(collection, function(val, key) {
|
349 | result.push(fn(val, key));
|
350 | });
|
351 |
|
352 | return result;
|
353 | }
|
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 |
|
362 |
|
363 | function keys(collection) {
|
364 | return collection && Object.keys(collection) || [];
|
365 | }
|
366 |
|
367 |
|
368 |
|
369 |
|
370 |
|
371 |
|
372 |
|
373 |
|
374 |
|
375 | function size(collection) {
|
376 | return keys(collection).length;
|
377 | }
|
378 |
|
379 |
|
380 |
|
381 |
|
382 |
|
383 |
|
384 |
|
385 |
|
386 |
|
387 | function values(collection) {
|
388 | return map(collection, (val) => val);
|
389 | }
|
390 |
|
391 |
|
392 |
|
393 |
|
394 |
|
395 |
|
396 |
|
397 |
|
398 |
|
399 |
|
400 | function groupBy(collection, extractor, grouped = {}) {
|
401 |
|
402 | extractor = toExtractor(extractor);
|
403 |
|
404 | forEach(collection, function(val) {
|
405 | let discriminator = extractor(val) || '_';
|
406 |
|
407 | let group = grouped[discriminator];
|
408 |
|
409 | if (!group) {
|
410 | group = grouped[discriminator] = [];
|
411 | }
|
412 |
|
413 | group.push(val);
|
414 | });
|
415 |
|
416 | return grouped;
|
417 | }
|
418 |
|
419 |
|
420 | function uniqueBy(extractor, ...collections) {
|
421 |
|
422 | extractor = toExtractor(extractor);
|
423 |
|
424 | let grouped = {};
|
425 |
|
426 | forEach(collections, (c) => groupBy(c, extractor, grouped));
|
427 |
|
428 | let result = map(grouped, function(val, key) {
|
429 | return val[0];
|
430 | });
|
431 |
|
432 | return result;
|
433 | }
|
434 |
|
435 |
|
436 | const unionBy = uniqueBy;
|
437 |
|
438 |
|
439 |
|
440 |
|
441 |
|
442 |
|
443 |
|
444 |
|
445 |
|
446 |
|
447 |
|
448 |
|
449 |
|
450 | function sortBy(collection, extractor) {
|
451 |
|
452 | extractor = toExtractor(extractor);
|
453 |
|
454 | let sorted = [];
|
455 |
|
456 | forEach(collection, function(value, key) {
|
457 | let disc = extractor(value, key);
|
458 |
|
459 | let entry = {
|
460 | d: disc,
|
461 | v: value
|
462 | };
|
463 |
|
464 | for (var idx = 0; idx < sorted.length; idx++) {
|
465 | let { d } = sorted[idx];
|
466 |
|
467 | if (disc < d) {
|
468 | sorted.splice(idx, 0, entry);
|
469 | return;
|
470 | }
|
471 | }
|
472 |
|
473 |
|
474 | sorted.push(entry);
|
475 | });
|
476 |
|
477 | return map(sorted, (e) => e.v);
|
478 | }
|
479 |
|
480 |
|
481 |
|
482 |
|
483 |
|
484 |
|
485 |
|
486 |
|
487 |
|
488 |
|
489 |
|
490 |
|
491 |
|
492 |
|
493 |
|
494 |
|
495 |
|
496 |
|
497 |
|
498 | function matchPattern(pattern) {
|
499 |
|
500 | return function(el) {
|
501 |
|
502 | return every(pattern, function(val, key) {
|
503 | return el[key] === val;
|
504 | });
|
505 |
|
506 | };
|
507 | }
|
508 |
|
509 |
|
510 |
|
511 |
|
512 |
|
513 |
|
514 |
|
515 | function toExtractor(extractor) {
|
516 |
|
517 | |
518 |
|
519 |
|
520 | return isFunction(extractor) ? extractor : (e) => {
|
521 |
|
522 |
|
523 | return e[extractor];
|
524 | };
|
525 | }
|
526 |
|
527 |
|
528 |
|
529 |
|
530 |
|
531 |
|
532 |
|
533 |
|
534 | function toMatcher(matcher) {
|
535 | return isFunction(matcher) ? matcher : (e) => {
|
536 | return e === matcher;
|
537 | };
|
538 | }
|
539 |
|
540 |
|
541 | function identity(arg) {
|
542 | return arg;
|
543 | }
|
544 |
|
545 | function toNum(arg) {
|
546 | return Number(arg);
|
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 | function debounce(fn, timeout) {
|
572 |
|
573 | let timer;
|
574 |
|
575 | let lastArgs;
|
576 | let lastThis;
|
577 |
|
578 | let lastNow;
|
579 |
|
580 | function fire(force) {
|
581 |
|
582 | let now = Date.now();
|
583 |
|
584 | let scheduledDiff = force ? 0 : (lastNow + timeout) - now;
|
585 |
|
586 | if (scheduledDiff > 0) {
|
587 | return schedule(scheduledDiff);
|
588 | }
|
589 |
|
590 | fn.apply(lastThis, lastArgs);
|
591 |
|
592 | clear();
|
593 | }
|
594 |
|
595 | function schedule(timeout) {
|
596 | timer = setTimeout(fire, timeout);
|
597 | }
|
598 |
|
599 | function clear() {
|
600 | if (timer) {
|
601 | clearTimeout(timer);
|
602 | }
|
603 |
|
604 | timer = lastNow = lastArgs = lastThis = undefined;
|
605 | }
|
606 |
|
607 | function flush() {
|
608 | if (timer) {
|
609 | fire(true);
|
610 | }
|
611 |
|
612 | clear();
|
613 | }
|
614 |
|
615 | |
616 |
|
617 |
|
618 | function callback(...args) {
|
619 | lastNow = Date.now();
|
620 |
|
621 | lastArgs = args;
|
622 | lastThis = this;
|
623 |
|
624 |
|
625 | if (!timer) {
|
626 | schedule(timeout);
|
627 | }
|
628 | }
|
629 |
|
630 | callback.flush = flush;
|
631 | callback.cancel = clear;
|
632 |
|
633 | return callback;
|
634 | }
|
635 |
|
636 |
|
637 |
|
638 |
|
639 |
|
640 |
|
641 |
|
642 |
|
643 |
|
644 |
|
645 | function throttle(fn, interval) {
|
646 | let throttling = false;
|
647 |
|
648 | return function(...args) {
|
649 |
|
650 | if (throttling) {
|
651 | return;
|
652 | }
|
653 |
|
654 | fn(...args);
|
655 | throttling = true;
|
656 |
|
657 | setTimeout(() => {
|
658 | throttling = false;
|
659 | }, interval);
|
660 | };
|
661 | }
|
662 |
|
663 |
|
664 |
|
665 |
|
666 |
|
667 |
|
668 |
|
669 |
|
670 |
|
671 | function bind(fn, target) {
|
672 | return fn.bind(target);
|
673 | }
|
674 |
|
675 |
|
676 |
|
677 |
|
678 |
|
679 |
|
680 |
|
681 |
|
682 |
|
683 | function assign(target, ...others) {
|
684 | return Object.assign(target, ...others);
|
685 | }
|
686 |
|
687 |
|
688 |
|
689 |
|
690 |
|
691 |
|
692 |
|
693 |
|
694 |
|
695 |
|
696 |
|
697 |
|
698 |
|
699 |
|
700 | function set(target, path, value) {
|
701 |
|
702 | let currentTarget = target;
|
703 |
|
704 | forEach(path, function(key, idx) {
|
705 |
|
706 | if (typeof key !== 'number' && typeof key !== 'string') {
|
707 | throw new Error('illegal key type: ' + typeof key + '. Key should be of type number or string.');
|
708 | }
|
709 |
|
710 | if (key === 'constructor') {
|
711 | throw new Error('illegal key: constructor');
|
712 | }
|
713 |
|
714 | if (key === '__proto__') {
|
715 | throw new Error('illegal key: __proto__');
|
716 | }
|
717 |
|
718 | let nextKey = path[idx + 1];
|
719 | let nextTarget = currentTarget[key];
|
720 |
|
721 | if (isDefined(nextKey) && isNil(nextTarget)) {
|
722 | nextTarget = currentTarget[key] = isNaN(+nextKey) ? {} : [];
|
723 | }
|
724 |
|
725 | if (isUndefined(nextKey)) {
|
726 | if (isUndefined(value)) {
|
727 | delete currentTarget[key];
|
728 | } else {
|
729 | currentTarget[key] = value;
|
730 | }
|
731 | } else {
|
732 | currentTarget = nextTarget;
|
733 | }
|
734 | });
|
735 |
|
736 | return target;
|
737 | }
|
738 |
|
739 |
|
740 |
|
741 |
|
742 |
|
743 |
|
744 |
|
745 |
|
746 |
|
747 |
|
748 |
|
749 | function get(target, path, defaultValue) {
|
750 |
|
751 | let currentTarget = target;
|
752 |
|
753 | forEach(path, function(key) {
|
754 |
|
755 |
|
756 | if (isNil(currentTarget)) {
|
757 | currentTarget = undefined;
|
758 |
|
759 | return false;
|
760 | }
|
761 |
|
762 | currentTarget = currentTarget[key];
|
763 | });
|
764 |
|
765 | return isUndefined(currentTarget) ? defaultValue : currentTarget;
|
766 | }
|
767 |
|
768 |
|
769 |
|
770 |
|
771 |
|
772 |
|
773 |
|
774 |
|
775 |
|
776 |
|
777 |
|
778 |
|
779 | function pick(target, properties) {
|
780 |
|
781 | let result = {};
|
782 |
|
783 | let obj = Object(target);
|
784 |
|
785 | forEach(properties, function(prop) {
|
786 |
|
787 | if (prop in obj) {
|
788 | result[prop] = target[prop];
|
789 | }
|
790 | });
|
791 |
|
792 | return result;
|
793 | }
|
794 |
|
795 |
|
796 |
|
797 |
|
798 |
|
799 |
|
800 |
|
801 |
|
802 |
|
803 |
|
804 |
|
805 |
|
806 | function omit(target, properties) {
|
807 |
|
808 | let result = {};
|
809 |
|
810 | let obj = Object(target);
|
811 |
|
812 | forEach(obj, function(prop, key) {
|
813 |
|
814 | if (properties.indexOf(key) === -1) {
|
815 | result[key] = prop;
|
816 | }
|
817 | });
|
818 |
|
819 | return result;
|
820 | }
|
821 |
|
822 |
|
823 |
|
824 |
|
825 |
|
826 |
|
827 |
|
828 |
|
829 |
|
830 |
|
831 |
|
832 | function merge(target, ...sources) {
|
833 |
|
834 | if (!sources.length) {
|
835 | return target;
|
836 | }
|
837 |
|
838 | forEach(sources, function(source) {
|
839 |
|
840 |
|
841 | if (!source || !isObject(source)) {
|
842 | return;
|
843 | }
|
844 |
|
845 | forEach(source, function(sourceVal, key) {
|
846 |
|
847 | if (key === '__proto__') {
|
848 | return;
|
849 | }
|
850 |
|
851 | let targetVal = target[key];
|
852 |
|
853 | if (isObject(sourceVal)) {
|
854 |
|
855 | if (!isObject(targetVal)) {
|
856 |
|
857 |
|
858 | targetVal = {};
|
859 | }
|
860 |
|
861 | target[key] = merge(targetVal, sourceVal);
|
862 | } else {
|
863 | target[key] = sourceVal;
|
864 | }
|
865 |
|
866 | });
|
867 | });
|
868 |
|
869 | return target;
|
870 | }
|
871 |
|
872 | export { assign, bind, debounce, ensureArray, every, filter, find, findIndex, flatten, forEach, get, groupBy, has, isArray, isDefined, isFunction, isNil, isNumber, isObject, isString, isUndefined, keys, map, matchPattern, merge, omit, pick, reduce, set, size, some, sortBy, throttle, unionBy, uniqueBy, values, without };
|