-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsequenz.js
1974 lines (1810 loc) · 59 KB
/
sequenz.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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define('sequenz', ['exports'], factory) :
(factory((global.sequenz = global.sequenz || {})));
}(this, (function (exports) { 'use strict';
var compose = function compose() {
for (var _len = arguments.length, transforms = Array(_len), _key = 0; _key < _len; _key++) {
transforms[_key] = arguments[_key];
}
return function (input) {
var ret = input;
for (var i = 0; i < transforms.length; i += 1) {
ret = transforms[i](ret);
}
return ret;
};
};
/**
* convert iterable to sequence. Iterable should contain `length` and should be able to get each
* element via `[i]`.
*
* @param {array|string} input - Iterable. Normally should be either string or array.
* @return {function} function to receive subscriber
*/
var fromIterable = (function (input) {
return (
/**
* function for subscribe.
*
* @param {function} onNext - function to receive each element.
*/
function (onNext) {
var length = input.length;
for (var i = 0; i < length; i += 1) {
if (onNext(input[i], i) === false) {
return false;
}
}
return true;
}
);
});
var toList = (function (subscribe) {
var ret = [];
subscribe(function (element) {
ret.push(element);
});
return ret;
});
var list = (function () {
for (var _len = arguments.length, transforms = Array(_len), _key = 0; _key < _len; _key++) {
transforms[_key] = arguments[_key];
}
return function (input) {
var result = compose.apply(undefined, [fromIterable].concat(transforms))(input);
if (typeof result === 'function') return toList(result);
return result;
};
});
var fromObject = (function (input) {
return function (onNext) {
for (var key in input) {
// eslint-disable-line no-restricted-syntax
/* istanbul ignore if */
if (!Object.prototype.hasOwnProperty.call(input, key)) continue;
if (onNext(input[key], key) === false) return false;
}
return true;
};
});
var toObject = (function (subscribe) {
var ret = {};
subscribe(function (element, key) {
ret[key] = element;
});
return ret;
});
var object = (function () {
for (var _len = arguments.length, transforms = Array(_len), _key = 0; _key < _len; _key++) {
transforms[_key] = arguments[_key];
}
return function (input) {
var result = compose.apply(undefined, [fromObject].concat(transforms))(input);
if (typeof result === 'function') return toObject(result);
return result;
};
});
var toString = (function (subscribe) {
var ret = '';
subscribe(function (char) {
ret += char;
});
return ret;
});
var string = (function () {
for (var _len = arguments.length, transforms = Array(_len), _key = 0; _key < _len; _key++) {
transforms[_key] = arguments[_key];
}
return function (input) {
var result = compose.apply(undefined, [fromIterable].concat(transforms))(input);
if (typeof result === 'function') return toString(result);
return result;
};
});
var empty = function empty() {};
/**
* Identity function that accepts one argument and returns the exact same argument.
*
* @param {T} x - Any value.
* @return {T} The exact same result as `x`.
*/
var identity = function identity(x) {
return x;
};
var truthy = function truthy() {
return true;
};
var equal = function equal(x, y) {
return x === y ? 0 : 1;
};
var not = function not(f, numOfParams) {
switch (numOfParams) {
case 0:
return function () {
return !f();
};
case 1:
return function (a) {
return !f(a);
};
case 2:
return function (a, b) {
return !f(a, b);
};
case 3:
return function (a, b, c) {
return !f(a, b, c);
};
default:
return function () {
return !f.apply(undefined, arguments);
};
}
};
var isArray = Array.isArray ||
/* istanbul ignore next */function (x) {
return Object.prototype.toString.call(x) === '[object Array]';
};
var isString = function isString(x) {
return typeof x === 'string' || x instanceof String;
};
var isNumber = function isNumber(x) {
return x === +x;
};
var isFunction = function isFunction(x) {
return typeof x === 'function';
};
var isLength = function isLength(value) {
return isNumber(value) && value >= 0 && value % 1 === 0 && value < Infinity;
};
var isArrayLike = function isArrayLike(x) {
return x != null && !isFunction(x) && isLength(x.length);
};
/**
* Split elements into groups, where each group is an array with specified given `length`.
* If elements are not able to be splitted evenly, the last group will contain the rest elements,
* having `length` smaller or equal to specified size.
*
* @param {number} [size=1] size - length of each group (default value is 1)
* Falsey or negative values will be treated as 0, and number will be coerced to integer.
*/
var chunk = function chunk() {
var size = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
if (!size || size < 1) return function () {
return truthy;
};
var sizeNum = Math.floor(size);
return function (subscribe) {
return (
/**
* @param {function(Array<any>,number):boolean} onNext - callback for receiving next element.
* First param will be the group of elements, second param will be the current index of group.
* Callback should return `false` if no longer interested in further elements.
* @return {boolean} whether the iteration has terminated by accident
*/
function (onNext) {
var cache = [];
var count = 0;
var result = subscribe(function (element) {
cache.push(element);
if (cache.length >= sizeNum) {
if (onNext(cache, count) === false) {
return false;
}
count += 1;
cache = [];
}
return true;
});
if (cache.length !== 0) return onNext(cache, count + 1);
return result;
}
);
};
};
/**
* Keep elements that `predicate` returns truthy for.
*
* **[NOTICE]**: Elements passed `predicate` will be provided with a new `key` that starts from `0`.
* To prevent the original `key`, `sequenz.pickBy` should be used instead.
*
* @param {function(any,any):boolean} predicate - The function invoked per iteration.
*/
var filter = function filter(predicate) {
return function (subscribe) {
return (
/**
* @param {function(any,any):boolean} onNext - Consumer that handles each element.
* @return {boolean} Whether the iteration is terminated in middle.
*/
function (onNext) {
var count = -1;
return subscribe(function (element, key) {
if (predicate(element, key)) {
count += 1;
return onNext(element, count);
}
return true;
});
}
);
};
};
/**
* Remove all falsey values.
* In JavaScript, `false`, `null`, `undefined`, `0`, `''` and `NaN` are considered falsey values.
*/
var compact = function compact() {
return filter(function (x) {
return !!x;
});
};
var just = (function (input) {
return function (onNext) {
return onNext(input, 0);
};
});
// import fromPromise from './fromPromise';
var from = (function (input) {
if (isArray(input) || isString(input)) return fromIterable(input);
// else if (isPromise(input)) return fromPromise(input);
else if (typeof input === 'function') return input; // assume it's sequenz already
return just(input);
});
/**
* Creates a new `sequenz` of values by running each element of given `sequenz` with `iteratee`,
* while keeping the `key` unmodified. The `iteratee` is invoked with two arguments: `element` and
* `key`.
*
* @param {function(any,any):any} iteratee - Function to create new `element`.
*/
var map = function map(iteratee) {
return function (subscribe) {
return function (onNext) {
return subscribe(function (element, key) {
return onNext(iteratee(element, key), key);
});
};
};
};
/**
* @param {...any} sequenzs - A list of elements, each will be iterated over and append to
* original `sequenz`. If the given element is a function, it will be considered as a `sequenz`;
* otherwise it will be converted to `sequenz` using `sequenz.from` method.
*/
var concat = function concat() {
for (var _len = arguments.length, sequenzs = Array(_len), _key = 0; _key < _len; _key++) {
sequenzs[_key] = arguments[_key];
}
return function (subscribe) {
return function (onNext) {
var subscriptions = [subscribe].concat(list(map(function (seq) {
return typeof seq === 'function' ? seq : from(seq);
}))(sequenzs));
var count = -1;
var subscriber = function subscriber(element) {
count += 1;return onNext(element, count);
};
for (var i = 0; i < subscriptions.length; i += 1) {
if (subscriptions[i](subscriber) === false) return false;
}
return true;
};
};
};
var findOrigin = (function () {
var predicate = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : identity;
var fromIndex = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
return function (subscribe) {
var startIndex = fromIndex ? Math.floor(+fromIndex) : 0;
var result = { value: undefined, index: -1 };
subscribe(function (element, i) {
if (i >= startIndex && predicate(element, i)) {
result = { value: element, index: i };
return false;
}
return true;
});
return result;
};
});
/**
* Find the first index in sequenz where `predicate` returns true.
*
* @param {function(any,any):boolean} predicate - Predicate function to determine if value has been
* found.
* @param {number} fromIndex - Index to start searching.
*/
var findIndex = function findIndex(predicate, fromIndex) {
return function (subscribe) {
return findOrigin(predicate, fromIndex)(subscribe).index;
};
};
var findLastOrigin = (function () {
var predicate = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : identity;
var fromIndex = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
return function (subscribe) {
var startIndex = fromIndex ? Math.floor(+fromIndex) : 0;
var result = { value: undefined, index: -1 };
subscribe(function (element, i) {
if (i >= startIndex && predicate(element, i)) {
result = { value: element, index: i };
}
return true;
});
return result;
};
});
/**
* Find the last index in sequenz where `predicate` returns true.
*
* @param {function(any,any):boolean} predicate - Predicate function to determine if value has been
* found.
* @param {number} fromIndex - Index to start searching.
*/
var findLastIndex = function findLastIndex(predicate, fromIndex) {
return function (subscribe) {
return findLastOrigin(predicate, fromIndex)(subscribe).index;
};
};
/**
* Gets the index at which the first occurrence of `value` is found in `sequenz`.
*
* @param {any} value - Value to search for.
* @param {number} [fromIndex=0] - The index to search from. If index is negative number, it will be
* used as offset from the end of `sequenz`.
*/
var indexOf = function indexOf(value) {
var fromIndex = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
return fromIndex < 0 ? findLastIndex(function (x) {
return x === value;
}, -1 * fromIndex) : findIndex(function (x) {
return x === value;
}, fromIndex);
};
/**
* Check whether the given value is contained in sequenz. Comparasion will be made using strict
* equal `===`.
*
* @param {any} value - Value that should be used in search.
* @param {number} fromIndex - Starting index for seaching.
*/
var contains = function contains(value) {
var fromIndex = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
return function (subscribe) {
return indexOf(value, fromIndex)(subscribe) >= 0;
};
};
/**
* Creates an object containing the key-value pairs, where keys are generated using given `iteratee`
* and values are the total times each key is generated from elements in sequenz.
*
* By default, `identity` function is used as `iteratee`.
*
* @param {function(any):string} iteratee - Iteratee function to generate keys.
*/
var countBy = function countBy() {
var iteratee = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : identity;
return function (subscribe) {
var result = {};
var mapFn = isFunction(iteratee) ? iteratee : function (element) {
return element[iteratee.toString()];
};
subscribe(function (element) {
var key = mapFn(element);
result[key] = result[key] ? result[key] + 1 : 1;
});
return result;
};
};
/**
* Iterate over each element in `sequenz`
*
* @param {function(any,any):boolean} f - Callback that will be triggered for every element in
* `sequenz`. Returning `false` will terminate the iteration.
*/
var each = function each(f) {
return function (subscribe) {
return subscribe(f);
};
};
/* istanbul ignore next */function MapPolyfill() {
this.str = {};
this.num = {};
}
MapPolyfill.prototype.set /* istanbul ignore next */ = function add(key, value) {
if (isString(key)) {
this.str[key] = value;
return;
} else if (isNumber(key)) {
this.num[key] = value;
return;
}
if (!this.restKey) {
this.restKey = [];this.restValue = [];
}
for (var i = 0; i < this.restKey.length; i += 1) {
if (this.restKey[i] === key) {
this.restValue[i] = value;
return;
}
}
this.restKey.push(key);
this.restValue.push(value);
};
MapPolyfill.prototype.has /* istanbul ignore next */ = function has(key) {
if (isString(key)) return this.str.hasOwnProperty(key);else if (isNumber(key)) return this.num.hasOwnProperty(key);else if (!this.restKey) return false;
for (var i = 0; i < this.restKey.length; i += 1) {
if (this.restKey[i] === key) return true;
}
return false;
};
MapPolyfill.prototype.get /* istanbul ignore next */ = function get(key) {
if (isString(key)) return this.str[key];else if (isNumber(key)) return this.num[key];else if (!this.restKey) return undefined;
for (var i = 0; i < this.restKey.length; i += 1) {
if (this.restKey[i] === key) return this.restValue[i];
}
return undefined;
};
var Map$1 = typeof May === 'undefined' ? MapPolyfill : Map;
/* istanbul ignore next */function SetPolyfill() {
this.map = new Map$1();
}
SetPolyfill.prototype.add /* istanbul ignore next */ = function add(key) {
this.map.set(key, key);
};
SetPolyfill.prototype.has /* istanbul ignore next */ = function has(key) {
return this.map.has(key);
};
var Set$1 = typeof Set === 'undefined' ? SetPolyfill : Set;
/**
* High oder function that acts similarly as `sequenz.difference`, except that it first accepts an
* iteratee function that will be used to map result values, before they get compared. The result
* element will still be the original value.
*
* @param {function(any):any} [iteratee=identity] Iteratee function that will be used to calculate
* value for comparation.
*/
var differenceBy = function differenceBy() {
var iteratee = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : identity;
return function () {
for (var _len = arguments.length, inputs = Array(_len), _key = 0; _key < _len; _key++) {
inputs[_key] = arguments[_key];
}
var set = new Set$1();
compose(from, each(function (input) {
compose(from, each(function (element) {
set.add(iteratee(element));
}))(input);
}))(inputs);
return filter(function (x) {
var element = iteratee(x);
return !set.has(element);
});
};
};
/**
* Create a sequenz of values that do not appear in other given arrays.
*
* @param {...Array} inputs - The values to exclude. If given is not array, it will be converted to
* based on implementation of `sequenz.from`.
*/
var difference = differenceBy(identity);
/**
* Check if `predicate` returns truthy for **all** elements of given `sequenz`. Iteration will stops
* once `predicate` returns falsey. The `predicate` is invoked with two arguments: (value, key).
*
* @param {function(any,any):boolean} [predicate=identity] The function invoked per iteration.
*/
var every = function every() {
var predicate = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : identity;
return function (subscribe) {
var result = true;
subscribe(function (element, key) {
if (!predicate(element, key)) {
result = false;
}
return result;
});
return result;
};
};
/**
* Reduce a `sequenz` to one final value by applying the `iteratee` against an accumulator and each
* element in `sequenz`.
*
* @param {function(any,any,any):any} iteratee - Function to execute on each value in the `sequenz`,
* taking three arguments: **accumulator**, **current value** and **current index**
* @param {?any} initial - Value to use as the first accumulator. If not provided, the first element
* will be used as initial value instead (`iteratee` will not be called for first element in this
* case then).
*/
var reduce = function reduce(iteratee, initial) {
if (arguments.length === 1) {
// eslint-disable-line prefer-rest-params
return function (subscribe) {
var result = void 0;
var hasInitial = false;
subscribe(function (element, key) {
if (!hasInitial) {
hasInitial = true;
result = element;
} else {
result = iteratee(result, element, key);
}
});
return result;
};
}
return function (subscribe) {
var result = initial;
subscribe(function (element, key) {
result = iteratee(result, element, key);
});
return result;
};
};
/**
* High oder function that acts similarly as `sequenz.difference`, except that it first accepts a
* customized comparator function that will be used to compara values.
*
* @param {function(any,any):number} [comparator=equal] Comparator function that will be used to
* compara alues. Comparator should return `0` when two values are equal.
*/
var differenceWith = function differenceWith(comparator) {
if (comparator === undefined) return difference;
return function () {
for (var _len = arguments.length, inputs = Array(_len), _key = 0; _key < _len; _key++) {
inputs[_key] = arguments[_key];
}
var values = list(map(function (input) {
if (isArray(input)) return input;
return compose(from, toList)(input);
}), reduce(function (ret, input) {
return ret.concat(input);
}))(inputs);
return filter(function (x) {
return list(every(function (y) {
return comparator(x, y) !== 0;
}))(values);
});
};
};
/**
* Continously ignore elements at front, if `predicate` returns thruthy.
*
* @param {function(any,any):boolean} predicate - Whether the element at front should be ignored.
* This function will not be called when first element results in falsey.
*/
var skipWhile = function skipWhile(predicate) {
return function (subscribe) {
return function (onNext) {
var shouldSkip = true;
var count = -1;
return subscribe(function (element, key) {
if (shouldSkip && predicate(element, key)) return true;
shouldSkip = false;
count += 1;
return onNext(element, count);
});
};
};
};
/**
* Skip first given number of elements.
*
* @param {number} num - Number of elements should be ignored.
*/
var skip$1 = function skip$1() {
var num = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
if (!num) return identity; // do not drop any element
var n = Math.floor(num);
return skipWhile(function (_, i) {
return i < n;
});
};
/**
* Skip given number of elements at the end of sequenz.
*
* @param {number} num - Number of elements that should be skipped at the end.
*/
var skipRight$1 = function skipRight$1() {
var num = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
if (num < 1) return identity;
if (num === Infinity) return function () {
return truthy;
};
var n = Math.floor(num);
var cache = new Array(n);
var count = -1;
var result = true;
var idx = -1;
return function (subscribe) {
return function (onNext) {
return subscribe(function (element) {
count += 1;
idx = count % n;
if (count >= n) {
result = onNext(cache[idx], count);
}
cache[idx] = element;
return result;
});
};
};
};
/**
* Skip some elements at the end, where each results in truthy value from `predicate` function.
* These elements should be continuous one after another.
*
* @param {function(any,any):boolean} predicate - Whether element should be ignored.
*/
var skipRightWhile$1 = function skipRightWhile$1() {
var predicate = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : identity;
var cache = [];
var count = -1;
return function (subscribe) {
return function (onNext) {
var doNext = function doNext(element) {
count += 1;return onNext(element, count);
};
return subscribe(function (element, key) {
if (predicate(element, key)) {
cache.push(element);
return true;
}
if (cache.length > 0) {
fromIterable(cache)(doNext);
cache = [];
}
return doNext(element);
});
};
};
};
/**
* Fills elements in a `sequenz` with given `value` from `start` up to, but not including, `end`.
*
* **[NOTICE]**: The `sequenz` should have keys that are `number`s, as key will be used to compare
* if given element should be replaced by `value`.
*
* @param {any} value - Value to fill
* @param {number} [start=0] - Start index
* @param {number} [end=Infinity] - End index
*/
var fill = function fill(value, start, end) {
if (start === undefined && end === undefined) {
return function (subscribe) {
return function (onNext) {
return subscribe(function (_, key) {
return onNext(value, key);
});
};
};
}
var startIdx = !start ? 0 : Math.floor(+start);
var endIdx = void 0;
if (end === undefined) endIdx = Infinity;else if (!end) endIdx = 0;else endIdx = Math.floor(+end);
return function (subscribe) {
return function (onNext) {
return subscribe(function (_, key) {
if (startIdx <= key && endIdx > key) return onNext(value, key);
return onNext(_, key);
});
};
};
};
/**
* Find the first value where `predicate` returns true.
*
* @param {function(any,any):boolean} predicate - Predicate function to determine if value has been
* found.
* @param {number} fromIndex - Index to start searching.
*/
var find = function find(predicate, fromIndex) {
return function (subscribe) {
return findOrigin(predicate, fromIndex)(subscribe).value;
};
};
/**
* Find the last value where `predicate` returns true.
*
* @param {function(any,any):boolean} predicate - Predicate function to determine if value has been
* found.
* @param {number} fromIndex - Index to start searching.
*/
var findLast = function findLast(predicate, fromIndex) {
return function (subscribe) {
return findLastOrigin(predicate, fromIndex)(subscribe).value;
};
};
/**
* Return the first element or `defaultValue` if sequenz is empty.
*
* @param {any} defaultValue - Value that will be returned if sequenz is empty.
*/
var firstOrDefault = function firstOrDefault(defaultValue) {
return function (subscribe) {
var ret = defaultValue;
subscribe(function (element) {
ret = element;return false;
});
return ret;
};
};
/**
* Find the first value in sequenz. `undefined` will be returned if sequenz is empty.
*/
var first = function first() {
return firstOrDefault(undefined);
};
var isMatch = function isMatch(properties) {
return function (obj) {
for (var key in properties) {
// eslint-disable-line no-restricted-syntax
if (!Object.prototype.hasOwnProperty.call(obj, key) || properties[key] !== obj[key]) {
return false;
}
}
return true;
};
};
/**
* Looks through each element in `sequenz` and returns all elements that contains the given
* key-value pairs specified in `properties`.
*
* @param {object} properties - Key-value pairs.
*/
var where = function where(properties) {
return filter(isMatch(properties));
};
/**
* Find the first value in sequenz where it matches the given `properties`. Internally, this API
* uses `where` to look for matched value.
*
* @param {object} properties - Key-pair values that should be used for searching.
*/
var findWhere = function findWhere(properties) {
return compose(where(properties), first());
};
/**
* Recursively flatten elements in `sequenz` up to `depth` times.
*
* @param {number} [depth=1] the maximum recursion depth.
*/
var flattenDepth = function flattenDepth() {
var depth = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
var depthNum = Math.floor(depth);
return function (subscribe) {
return function (onNext) {
var count = -1;
var doNext = function doNext(element) {
count += 1;return onNext(element, count);
};
var recursiveSubscribe = function recursiveSubscribe(sub, currentDepth) {
return sub(function (element) {
if (isArrayLike(element) && !isString(element) && currentDepth < depthNum) {
return recursiveSubscribe(fromIterable(element), currentDepth + 1);
}
return doNext(element);
});
};
return recursiveSubscribe(subscribe, 0);
};
};
};
/**
* Flatten elements in `sequenz` one level deep.
*/
var flatten = function flatten() {
return flattenDepth(1);
};
/**
* Flatten elements in `sequenz` as much as possible.
*/
var flattenDeep = function flattenDeep() {
return flattenDepth(Infinity);
};
var get$1 = function get$1(object, property, receiver) {
if (object === null) object = Function.prototype;
var desc = Object.getOwnPropertyDescriptor(object, property);
if (desc === undefined) {
var parent = Object.getPrototypeOf(object);
if (parent === null) {
return undefined;
} else {
return get$1(parent, property, receiver);
}
} else if ("value" in desc) {
return desc.value;
} else {
var getter = desc.get;
if (getter === undefined) {
return undefined;
}
return getter.call(receiver);
}
};
var set = function set(object, property, value, receiver) {
var desc = Object.getOwnPropertyDescriptor(object, property);
if (desc === undefined) {
var parent = Object.getPrototypeOf(object);
if (parent !== null) {
set(parent, property, value, receiver);
}
} else if ("value" in desc && desc.writable) {
desc.value = value;
} else {
var setter = desc.set;
if (setter !== undefined) {
setter.call(receiver, value);
}
}
return value;
};
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");
}
};
}();
var fromPairs = (function () {
return function (subscribe) {
var result = {};
subscribe(function (_ref) {
var _ref2 = slicedToArray(_ref, 2),
key = _ref2[0],
value = _ref2[1];
result[key] = value;
});
return result;
};
});
/**
* Keep only the elements that `predicate` returns true. Unlike `filter` API, this API will keep the
* origin `key` value unchanged.
*
* @param {function(any,any):boolean} predicate - Check if value should be kept or ignored.
*/