forked from helpers/handlebars-helpers
-
Notifications
You must be signed in to change notification settings - Fork 5
/
array.js
883 lines (791 loc) · 20.3 KB
/
array.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
const _ = require('lodash');
const utils = require('./utils');
const mathHelpers = require('./math');
/**
* @exports array
*/
const helpers = module.exports;
/**
* Returns all of the items in an array after the specified index.
* Opposite of [before](#before).
*
* ```handlebars
* <!-- array: ['a', 'b', 'c'] -->
* {{after array 1}}
* <!-- results in: '["c"]' -->
* ```
* @param {Array} `array` Collection
* @param {Number} `n` Starting index (number of items to exclude)
* @return {Array} Array exluding `n` items.
* @api public
*/
helpers.after = function(array, n) {
if (utils.isUndefined(array)) return '';
if (!_.isFunction(array.slice)) return '';
return array.slice(n);
};
/**
* Cast the given `value` to an array.
*
* ```handlebars
* {{arrayify "foo"}}
* <!-- results in: [ "foo" ] -->
* ```
* @param {any} `value`
* @return {Array}
* @api public
*/
helpers.arrayify = utils.arrayify;
/**
* Return all of the items in the collection before the specified
* count. Opposite of [after](#after).
*
* ```handlebars
* <!-- array: ['a', 'b', 'c'] -->
* {{before array 2}}
* <!-- results in: '["a", "b"]' -->
* ```
* @param {Array} `array`
* @param {Number} `n`
* @return {Array} Array excluding items after the given number.
* @api public
*/
helpers.before = function(array, n) {
if (utils.isUndefined(array)) return '';
if (!_.isFunction(array.slice)) return '';
return array.slice(0, n);
};
/**
* Iterates over each item in an array and exposes the current item
* in the array as context to the inner block. In addition to
* the current array item, the helper exposes the following variables
* to the inner block:
*
* - `index`
* - `total`
* - `isFirst`
* - `isLast`
*
* Also, `@index` is exposed as a private variable, and additional
* private variables may be defined as hash arguments.
*
* ```handlebars
* <!-- accounts = [
* {'name': 'John', 'email': '[email protected]'},
* {'name': 'Malcolm', 'email': '[email protected]'},
* {'name': 'David', 'email': '[email protected]'}
* ] -->
*
* {{#forEach accounts}}
* <a href="mailto:{{ email }}" title="Send an email to {{ name }}">
* {{ name }}
* </a>{{#unless isLast}}, {{/unless}}
* {{/forEach}}
* ```
* @param {Array} `array`
* @return {String}
* @block
* @api public
*/
helpers.forEach = function(array, options) {
if (!utils.isObject(options)) {
throw new TypeError('forEach expects options to be an object');
}
let data = utils.createFrame(options);
data = utils.extend(data, options.hash);
const length = array.length;
let buffer = '';
let i = -1;
while (++i < length) {
const item = array[i];
data.index = i;
item.index = i + 1;
item.total = length;
item.isFirst = i === 0;
item.isLast = i === (length - 1);
buffer += options.fn(item, { data: data });
}
return buffer;
};
/**
* ```handlebars
* <!-- array: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] -->
* {{#eachIndex array}}
* {{item}} is {{index}}
* {{/eachIndex}}
* ```
* @param {Array} `array`
* @param {Object} `options`
* @return {String}
* @block
* @api public
*/
helpers.eachIndex = function(array, options) {
let result = '';
for (let i = 0; i < array.length; i++) {
result += options.fn({item: array[i], index: i});
}
return result;
};
/**
* Block helper that filters the given array and renders the block for values that
* evaluate to `true`, otherwise the inverse block is returned.
*
* ```handlebars
* <!-- array: ['a', 'b', 'c'] -->
* {{#filter array "foo"}}AAA{{else}}BBB{{/filter}}
* <!-- results in: 'BBB' -->
* ```
* @param {Array} `array`
* @param {any} `value`
* @param {Object} `options`
* @return {String}
* @block
* @api public
*/
helpers.filter = function(array, value, options) {
let content = '';
let results = [];
// filter on a specific property
const prop = options.hash && (options.hash.property || options.hash.prop);
if (prop) {
results = array.filter(function(val) {
return value === utils.get(val, prop);
});
} else {
// filter on a string value
results = array.filter(function(v) {
return value === v;
});
}
if (results && results.length > 0) {
for (let i = 0; i < results.length; i++) {
content += options.fn(results[i]);
}
return content;
}
return options.inverse(this);
};
/**
* Returns the first item, or first `n` items of an array.
*
* ```handlebars
* {{first "['a', 'b', 'c', 'd', 'e']" 2}}
* <!-- results in: '["a", "b"]' -->
* ```
* @param {Array} `array`
* @param {Number} `n` Number of items to return, starting at `0`.
* @return {Array}
* @api public
*/
helpers.first = function(array, n) {
if (utils.isUndefined(array)) return '';
if (!_.isFunction(array.slice)) return '';
if (!utils.isNumber(n)) {
return array[0];
}
return array.slice(0, n);
};
/**
* Block helper that renders the block if an array has the
* given `value`. Optionally specify an inverse block to render
* when the array does not have the given value.
*
* ```handlebars
* <!-- array: ['a', 'b', 'c'] -->
* {{#inArray array "d"}}
* foo
* {{else}}
* bar
* {{/inArray}}
* <!-- results in: 'bar' -->
* ```
* @param {Array} `array`
* @param {any} `value`
* @param {Object} `options`
* @return {String}
* @block
* @api public
*/
helpers.inArray = function(array, value, options) {
const idx = Array.isArray(array) ? array.indexOf(value) : -1;
return utils.value(idx > -1, this, options);
};
/**
* Returns true if `value` is an es5 array.
*
* ```handlebars
* {{isArray "abc"}}
* <!-- results in: false -->
*
* <!-- array: [1, 2, 3] -->
* {{isArray array}}
* <!-- results in: true -->
* ```
* @param {any} `value` The value to test.
* @return {Boolean}
* @api public
*/
helpers.isArray = function(value) {
return Array.isArray(value);
};
/**
* Returns the item from `array` at index `idx`.
*
* ```handlebars
* <!-- array: ['a', 'b', 'c'] -->
* {{itemAt array 1}}
* <!-- results in: 'b' -->
* ```
* @param {Array} `array`
* @param {Number} `idx`
* @return {any} `value`
* @block
* @api public
*/
helpers.itemAt = function(array, idx) {
array = utils.result(array);
if (Array.isArray(array)) {
idx = utils.isNumber(idx) ? +idx : 0;
if (idx < 0) {
return array[array.length + idx];
}
if (idx < array.length) {
return array[idx];
}
}
};
/**
* Join all elements of array into a string, optionally using a
* given separator.
*
* ```handlebars
* <!-- array: ['a', 'b', 'c'] -->
* {{join array}}
* <!-- results in: 'a, b, c' -->
*
* {{join array '-'}}
* <!-- results in: 'a-b-c' -->
* ```
* @param {Array} `array`
* @param {String} `separator` The separator to use. Defaults to `, `.
* @return {String}
* @api public
*/
helpers.join = function(array, separator) {
if (typeof array === 'string') return array;
if (!Array.isArray(array)) return '';
separator = utils.isString(separator) ? separator : ', ';
return array.join(separator);
};
/**
* Returns true if the the length of the given `value` is equal
* to the given `length`. Can be used as a block or inline helper.
*
* @param {Array|String} `value`
* @param {Number} `length`
* @param {Object} `options`
* @return {String}
* @block
* @api public
*/
helpers.equalsLength = function(value, length, options) {
if (utils.isOptions(length)) {
options = length;
length = 0;
}
let len = 0;
if (typeof value === 'string' || Array.isArray(value)) {
len = value.length;
}
return utils.value(len === length, this, options);
};
/**
* Returns the last item, or last `n` items of an array or string.
* Opposite of [first](#first).
*
* ```handlebars
* <!-- const value = ['a', 'b', 'c', 'd', 'e'] -->
*
* {{last value}}
* <!-- results in: ['e'] -->
*
* {{last value 2}}
* <!-- results in: ['d', 'e'] -->
*
* {{last value 3}}
* <!-- results in: ['c', 'd', 'e'] -->
* ```
* @param {Array|String} `value` Array or string.
* @param {Number} `n` Number of items to return from the end of the array.
* @return {Array}
* @api public
*/
helpers.last = function(value, n) {
if (!Array.isArray(value) && typeof value !== 'string') {
return '';
}
if (!utils.isNumber(n)) {
return value[value.length - 1];
}
return value.slice(-Math.abs(n));
};
/**
* Returns the length of the given string or array.
*
* ```handlebars
* {{length '["a", "b", "c"]'}}
* <!-- results in: 3 -->
*
* <!-- results in: myArray = ['a', 'b', 'c', 'd', 'e']; -->
* {{length myArray}}
* <!-- results in: 5 -->
*
* <!-- results in: myObject = {'a': 'a', 'b': 'b'}; -->
* {{length myObject}}
* <!-- results in: 2 -->
* ```
* @param {Array|Object|String} `value`
* @return {Number} The length of the value.
* @api public
*/
helpers.length = function(value) {
if (utils.isPlainObject(value) && !utils.isOptions(value)) {
value = Object.keys(value);
}
if (typeof value === 'string' || Array.isArray(value)) {
return value.length;
}
return 0;
};
/**
* Alias for [equalsLength](#equalsLength)
*
* @api public
*/
helpers.lengthEqual = helpers.equalsLength;
/**
* Returns a new array, created by calling `function` on each
* element of the given `array`. For example,
*
* ```handlebars
* <!-- array: ['a', 'b', 'c'], and "double" is a
* fictitious function that duplicates letters -->
* {{map array double}}
* <!-- results in: '["aa", "bb", "cc"]' -->
* ```
*
* @param {Array} `array`
* @param {Function} `fn`
* @return {String}
* @api public
*/
helpers.map = function(array, iter) {
if (!Array.isArray(array)) return '';
const len = array.length;
const res = new Array(len);
let i = -1;
if (typeof iter !== 'function') {
return array;
}
while (++i < len) {
res[i] = iter(array[i], i, array);
}
return res;
};
/**
* Map over the given object or array or objects and create an array of values
* from the given `prop`. Dot-notation may be used (as a string) to get
* nested properties.
*
* ```handlebars
* // {{pluck items "data.title"}}
* <!-- results in: '["aa", "bb", "cc"]' -->
* ```
* @param {Array|Object} `collection`
* @param {Function} `prop`
* @return {String}
* @api public
*/
helpers.pluck = function(arr, prop) {
if (utils.isUndefined(arr)) return '';
return utils.map(arr, e => utils.get(e, prop));
};
/**
* Reverse the elements in an array, or the characters in a string.
*
* ```handlebars
* <!-- value: 'abcd' -->
* {{reverse value}}
* <!-- results in: 'dcba' -->
* <!-- value: ['a', 'b', 'c', 'd'] -->
* {{reverse value}}
* <!-- results in: ['d', 'c', 'b', 'a'] -->
* ```
* @param {Array|String} `value`
* @return {Array|String} Returns the reversed string or array.
* @api public
*/
helpers.reverse = function(val) {
if (Array.isArray(val)) {
val.reverse();
return val;
}
if (val && typeof val === 'string') {
return val.split('').reverse().join('');
}
};
/**
* Block helper that returns the block if the callback returns true
* for some value in the given array.
*
* ```handlebars
* <!-- array: [1, 'b', 3] -->
* {{#some array isString}}
* Render me if the array has a string.
* {{else}}
* Render me if it doesn't.
* {{/some}}
* <!-- results in: 'Render me if the array has a string.' -->
* ```
* @param {Array} `array`
* @param {Function} `iter` Iteratee
* @param {Options} Handlebars provided options object
* @return {String}
* @block
* @api public
*/
helpers.some = function(array, iter, options) {
if (Array.isArray(array)) {
for (let i = 0; i < array.length; i++) {
if (iter(array[i], i, array)) {
return options.fn(this);
}
}
}
return options.inverse(this);
};
/**
* Sort the given `array`. If an array of objects is passed,
* you may optionally pass a `key` to sort on as the second
* argument. You may alternatively pass a sorting function as
* the second argument.
*
* ```handlebars
* <!-- array: ['b', 'a', 'c'] -->
* {{sort array}}
* <!-- results in: '["a", "b", "c"]' -->
* ```
*
* @param {Array} `array` the array to sort.
* @param {String|Function} `key` The object key to sort by, or sorting function.
* @api public
*/
helpers.sort = function(array, options) {
if (!Array.isArray(array)) return '';
if (utils.get(options, 'hash.reverse')) {
return array.sort().reverse();
}
return array.sort();
};
/**
* Sort an `array`. If an array of objects is passed,
* you may optionally pass a `key` to sort on as the second
* argument. You may alternatively pass a sorting function as
* the second argument.
*
* ```handlebars
* <!-- array: [{a: 'zzz'}, {a: 'aaa'}] -->
* {{sortBy array "a"}}
* <!-- results in: '[{"a":"aaa"}, {"a":"zzz"}]' -->
* ```
*
* @param {Array} `array` the array to sort.
* @param {String|Function} `props` One or more properties to sort by, or sorting functions to use.
* @api public
*/
helpers.sortBy = function(array, prop) {
if (!Array.isArray(array)) return '';
if (utils.isString(prop)) return utils.sortBy(array, prop);
if (utils.isFunction(prop)) return [...array].sort(prop);
return [...array].sort();
};
/**
* Use the items in the array _after_ the specified index
* as context inside a block. Opposite of [withBefore](#withBefore).
*
* ```handlebars
* <!-- array: ['a', 'b', 'c', 'd', 'e'] -->
* {{#withAfter array 3}}
* {{this}}
* {{/withAfter}}
* <!-- results in: "de" -->
* ```
* @param {Array} `array`
* @param {Number} `idx`
* @param {Object} `options`
* @return {Array}
* @block
* @api public
*/
helpers.withAfter = function(array, idx, options) {
if (!Array.isArray(array)) return '';
array = array.slice(idx);
let result = '';
for (let i = 0; i < array.length; i++) {
result += options.fn(array[i]);
}
return result;
};
/**
* Use the items in the array _before_ the specified index
* as context inside a block. Opposite of [withAfter](#withAfter).
*
* ```handlebars
* <!-- array: ['a', 'b', 'c', 'd', 'e'] -->
* {{#withBefore array 3}}
* {{this}}
* {{/withBefore}}
* <!-- results in: 'ab' -->
* ```
* @param {Array} `array`
* @param {Number} `idx`
* @param {Object} `options`
* @return {Array}
* @block
* @api public
*/
helpers.withBefore = function(array, idx, options) {
if (!Array.isArray(array)) return '';
array = array.slice(0, idx);
let result = '';
for (let i = 0; i < array.length; i++) {
result += options.fn(array[i]);
}
return result;
};
/**
* Use the first item in a collection inside a handlebars
* block expression. Opposite of [withLast](#withLast).
*
* ```handlebars
* <!-- array: ['a', 'b', 'c'] -->
* {{#withFirst array}}
* {{this}}
* {{/withFirst}}
* <!-- results in: 'a' -->
* ```
* @param {Array} `array`
* @param {Number} `idx`
* @param {Object} `options`
* @return {String}
* @block
* @api public
*/
helpers.withFirst = function(array, idx, options) {
if (utils.isUndefined(array)) return '';
array = utils.result(array);
if (!utils.isUndefined(idx)) {
idx = parseFloat(utils.result(idx));
}
if (utils.isUndefined(idx)) {
options = idx;
return options.fn(array[0]);
}
array = array.slice(0, idx);
let result = '';
for (let i = 0; i < array.length; i++) {
result += options.fn(array[i]);
}
return result;
};
/**
* Block helper that groups array elements by given group `size`.
*
* ```handlebars
* <!-- array: ['a','b','c','d','e','f','g','h'] -->
* {{#withGroup array 4}}
* {{#each this}}
* {{.}}
* {{each}}
* <br>
* {{/withGroup}}
* <!-- results in: -->
* <!-- 'a','b','c','d'<br> -->
* <!-- 'e','f','g','h'<br> -->
* ```
* @param {Array} `array` The array to iterate over
* @param {Number} `size` The desired length of each array "group"
* @param {Object} `options` Handlebars options
* @return {String}
* @block
* @api public
*/
helpers.withGroup = function(array, size, options) {
let result = '';
if (Array.isArray(array) && array.length > 0) {
let subcontext = [];
for (let i = 0; i < array.length; i++) {
if (i > 0 && (i % size) === 0) {
result += options.fn(subcontext);
subcontext = [];
}
subcontext.push(array[i]);
}
result += options.fn(subcontext);
}
return result;
};
/**
* Use the last item or `n` items in an array as context inside a block.
* Opposite of [withFirst](#withFirst).
*
* ```handlebars
* <!-- array: ['a', 'b', 'c'] -->
* {{#withLast array}}
* {{this}}
* {{/withLast}}
* <!-- results in: 'c' -->
* ```
* @param {Array} `array`
* @param {Number} `idx` The starting index.
* @param {Object} `options`
* @return {String}
* @block
* @api public
*/
helpers.withLast = function(array, idx, options) {
if (utils.isUndefined(array)) return '';
array = utils.result(array);
if (!utils.isUndefined(idx)) {
idx = parseFloat(utils.result(idx));
}
if (utils.isUndefined(idx)) {
options = idx;
return options.fn(array[array.length - 1]);
}
array = array.slice(-idx);
let len = array.length, i = -1;
let result = '';
while (++i < len) {
result += options.fn(array[i]);
}
return result;
};
/**
* Block helper that sorts a collection and exposes the sorted
* collection as context inside the block.
*
* ```handlebars
* <!-- array: ['b', 'a', 'c'] -->
* {{#withSort array}}{{this}}{{/withSort}}
* <!-- results in: 'abc' -->
* ```
* @param {Array} `array`
* @param {String} `prop`
* @param {Object} `options` Specify `reverse="true"` to reverse the array.
* @return {String}
* @block
* @api public
*/
helpers.withSort = function(array, prop, options) {
if (utils.isUndefined(array)) return '';
let result = '';
if (utils.isUndefined(prop)) {
options = prop;
array = array.sort();
if (utils.get(options, 'hash.reverse')) {
array = array.reverse();
}
for (let i = 0, len = array.length; i < len; i++) {
result += options.fn(array[i]);
}
return result;
}
array.sort(function(a, b) {
a = utils.get(a, prop);
b = utils.get(b, prop);
return a > b ? 1 : (a < b ? -1 : 0);
});
if (utils.get(options, 'hash.reverse')) {
array = array.reverse();
}
let alen = array.length, j = -1;
while (++j < alen) {
result += options.fn(array[j]);
}
return result;
};
/**
* Block helper that return an array with all duplicate
* values removed. Best used along with a [each](#each) helper.
*
* ```handlebars
* <!-- array: ['a', 'a', 'c', 'b', 'e', 'e'] -->
* {{#each (unique array)}}{{.}}{{/each}}
* <!-- results in: 'acbe' -->
* ```
* @param {Array} `array`
* @param {Object} `options`
* @return {Array}
* @api public
*/
helpers.unique = function(array, options) {
if (utils.isUndefined(array)) return '';
return array.filter(function(item, index, arr) {
return arr.indexOf(item) === index;
});
};
/**
* Block helper that returns an array of elements split
* into groups the length of size.
*
* ```handlebars
* <!-- array: ['a', 'a', 'c', 'b', 'e', 'e'] -->
* {{#partition array 3}}<{{#each this}}{{this}}{{/each}}>{{/partition}}
* <!-- results in: '<abc><def><gh>' -->
* ```
* @param {Array} `array`
* @param {Number} `size`
* @param {Object} `options` The handlebars options object, if used as a block helper.
* @return {String}
* @block
* @api public
*/
helpers.partition = function(array, size = 1, options) {
if (!Array.isArray(array)) {
throw new Error('First parameter have to be an array');
}
const blockFn = options.fn;
return utils.chunk(array, size).reduce((acc, chunk, index) => acc + blockFn(chunk, { data: { index } }), '');
};
/**
* Block helper that merge two or more arrays and apply
* passed math helper to merge.
*
* ```handlebars
* <!-- arrays: { quantities: [1, 2, 5], weights: [0.125, 0.5, 0.75] } -->
* {{mergeArrays "times" quantities weights }}
* <!-- results in: '0.125,1,3.75' -->
* ```
* @param {String} `helper`
* @param {...Array} params
* @return {String}
* @block
* @api public
*/
helpers.mergeArrays = function arrayMergeHelper(helper, ...params) {
const helperFn = mathHelpers[helper];
const arrays = params.slice(0, -1);
if (!utils.isFunction(helperFn)) {
throw new Error('An unknown helper function is passed as a first argument');
}
if (!Array.isArray(arrays) || arrays.some(arr => !Array.isArray(arr))) {
throw new Error('Some of the arrays arguments are incorrect');
}
const maxLength = Math.max(...arrays.map(arr => arr.length));
return Array(maxLength).fill().map((item, index) => {
const items = arrays.map(arr => arr[index]).filter(item => item);
return items.splice(1).reduce(helperFn, items[0]);
});
};