/
list.js
824 lines (712 loc) · 30.4 KB
/
list.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
/* global RUNTIME_CHECKS, define */
(function () {
'use strict';
/**
* Maintains a list of items. The idea is to stay as true as possible to the native Array interface, but to augment its usefulness by triggering events whenever the list is updated, as well as adding convenience functions for mutating the List in place.
*
* The exported List constructor can be used as-is, in which case a generic List will be created. This is a quite common use case. Alternatively, a List "subclass" constructor can be created by calling `List.withProperties(...)`. This second approach provides the possibility to add custom _calculated_ properties to the list. See {@link module:bff/list#withProperties} for more details.
*
* @exports bff/list
*/
function moduleFactory(extend, eventEmitter, Record) {
var ADDED_EVENT = 'added';
var REPLACED_EVENT = 'replaced';
var REMOVED_EVENT = 'removed';
/**
* @event module:bff/list#item:added
* @arg {any} item - The item that was added to the List.
* @arg {number} index - The position withing the List where the item was added.
* @arg {module:bff/list} list - The List to which the item was added.
*/
var ITEM_ADDED_EVENT = 'item:added';
/**
* @event module:bff/list#item:replaced
* @arg {any} newItem - The item that was added to the List.
* @arg {any} oldItem - The item that was removed from the List.
* @arg {number} index - The position withing the List where the item was replaced.
* @arg {module:bff/list} list - The List in which the item was replaced.
*/
var ITEM_REPLACED_EVENT = 'item:replaced';
/**
* @event module:bff/list#item:removed
* @arg {any} item - The item that was removed from the List.
* @arg {number} index - The position withing the List where the item was removed.
* @arg {module:bff/list} list - The List from which the item was removed.
*/
var ITEM_REMOVED_EVENT = 'item:removed';
/**
* @event module:bff/list#change:length
* @arg {number} newLength - The current length of the List.
* @arg {number} oldLength - The previous length of the List.
* @arg {module:bff/list} list - The List whose length has changed.
*/
var CHANGE_LENGTH_EVENT = 'change:length';
// "Private event"
var PRECHANGE_LENGTH_EVENT = 'prechange:length';
var PRECHANGE_EVENT = 'prechange';
var CHANGE_EVENT = 'change';
/**
* @callback module:bff/list~forEachCallback
* @param {any} item - Current List item.
* @param {number} index - Item position in List.
* @param {module:bff/list} list - List being iterated over.
*/
/**
* @callback module:bff/list~mapCallback
* @param {any} item - Current List item.
* @param {number} index - Item position in List.
* @param {module:bff/list} list - List being iterated over.
* @returns {any} Transformed object.
*/
/**
* @callback module:bff/list~predicateCallback
* @param {any} item - Current List item.
* @param {number} index - Item position in List.
* @param {module:bff/list} list - List being iterated over.
* @returns {boolean} true if the item passes the test, false otherwise
*/
/**
* @callback module:bff/list~reduceCallback
* @param {any} previousItem - The value previously returned in the last invocation of the callback, or initialValue, if supplied. Usually an aggregate of previous items.
* @param {any} item - Current List item being processed.
* @param {number} index - Item position in List.
* @param {module:bff/list} list - List being iterated over.
* @returns {any} The aggregated value.
*/
/**
* Compares _itemA_ and _itemB_ arguments according to some sorting criterion. Should return -1 if _itemA_ comes before _itemB_, 0 if _itemA_ is equal to _itemB_, 1 if _itemB_ comes before _itemA_.
* @callback module:bff/list~compareFunction
* @param {any} itemA - A list item.
* @param {any} itemB - Another list item.
* @returns {number}.
*/
var ITEM_EVENT_TOKEN_MATCHER = /item:/;
function isEmitter(obj) { return !!(obj && obj.addEventListener); } // Quack!
function reemitItemEvent(self, item, strippedEventName, eventName) {
self.listenTo(item, strippedEventName, function reemitItemEvent() {
self.emitArgsAsArray(eventName, arguments);
});
}
function onItemAdded(self, item, index) {
if (!isEmitter(item)) {
self.emit(ITEM_ADDED_EVENT, item, index, self);
return;
}
for (var eventName in self.__private.reEmittingEvents) {
var strippedEventName = eventName.replace(ITEM_EVENT_TOKEN_MATCHER, '');
reemitItemEvent(self, item, strippedEventName, eventName);
}
item.emit(ADDED_EVENT, item, index, self);
}
function onItemRemoved(self, item, index) {
if (!isEmitter(item)) {
self.emit(ITEM_REMOVED_EVENT, item, index, self);
return;
}
item.emit(REMOVED_EVENT, item, index, self);
self.stopListening(item);
}
function onItemReplaced(self, newItem, oldItem, index) {
if (newItem === oldItem) { return; }
isEmitter(oldItem) ?
oldItem.emit(REPLACED_EVENT, newItem, oldItem, index, self) :
self.emit(ITEM_REPLACED_EVENT, newItem, oldItem, index, self);
}
function makeSetter(index) {
// TODO: Investigate if this is really a good idea, performance wise
return function setter(val) { this.splice(index, 1, val); };
}
function makeGetter(index) {
return function getter() { return this.__private.array[index]; };
}
function delegate(funcName) {
return function () {
return this.__private.array[funcName].apply(this.__private.array, arguments);
};
}
function delegateCreator(funcName) {
return function () {
return new this.constructor(this.__private.array[funcName].apply(this.__private.array, arguments));
};
}
function triggerPrechangeLengthEvent(self) {
self.emit(PRECHANGE_EVENT, 'length', self.length, self);
self.emit(PRECHANGE_LENGTH_EVENT, self.length, self);
}
function triggerChangeLengthEvent(self, prevLength) {
if (self.length === prevLength) { return; }
self.emit(CHANGE_EVENT, 'length', self.length, prevLength, self);
self.emit(CHANGE_LENGTH_EVENT, self.length, prevLength, self);
}
/**
* @alias module:bff/list
*/
var listFunctions = {};
/**
* Add one or more items to the end of the List. Mirrors Array.push behavior.
* @instance
* @arg {...any} item - Each item argument will be pushed onto the List.
* @emits module:bff/list#change:length
* @emits module:bff/list#item:added
* @returns {number} Updated List length
*/
listFunctions.push = function () {
var nItems = arguments.length;
if (nItems === 0) { return this.length; }
var prevLength = this.length;
triggerPrechangeLengthEvent(this);
var privArray = this.__private.array;
privArray.push.apply(privArray, arguments);
for (var i = prevLength; i < privArray.length; ++i) {
onItemAdded(this, privArray[i], i);
}
triggerChangeLengthEvent(this, prevLength);
return this.length;
};
/**
* Add one or more items to the beginning of the List. Mirrors Array.unshift behavior.
* @instance
* @arg {...any} item - Each item argument will be pushed onto the List.
* @emits module:bff/list#change:length
* @emits module:bff/list#item:added
* @returns {Number} Updated List length
*/
listFunctions.unshift = function () {
var nItems = arguments.length;
if (nItems === 0) { return this.length; }
var prevLength = this.length;
triggerPrechangeLengthEvent(this);
var privArray = this.__private.array;
privArray.unshift.apply(privArray, arguments);
for (var i = 0; i < nItems; ++i) {
onItemAdded(this, privArray[i], i);
}
triggerChangeLengthEvent(this, prevLength);
return this.length;
};
/**
* Remove and return one item from the end of the List. Mirrors Array.pop behavior.
* @instance
* @emits module:bff/list#change:length
* @emits module:bff/list#item:removed
* @returns {any} Removed item
*/
listFunctions.pop = function () {
if (this.length === 0) { return undefined; }
var prevLength = this.length;
triggerPrechangeLengthEvent(this);
var privArray = this.__private.array;
var poppedItem = privArray.pop.apply(privArray, arguments);
onItemRemoved(this, poppedItem, this.length);
triggerChangeLengthEvent(this, prevLength);
return poppedItem;
};
/**
* Remove and return one item from the beginning of the List. Mirrors Array.shift behavior.
* @instance
* @emits module:bff/list#change:length
* @emits module:bff/list#item:removed
* @returns {any} Removed item
*/
listFunctions.shift = function () {
if (this.length === 0) { return undefined; }
var prevLength = this.length;
triggerPrechangeLengthEvent(this);
var privArray = this.__private.array;
var shiftedItem = privArray.shift.apply(privArray, arguments);
onItemRemoved(this, shiftedItem, 0);
triggerChangeLengthEvent(this, prevLength);
return shiftedItem;
};
/**
* Changes the content of the List by removing existing items and/or adding new items. Mirrors Array.splice behavior.
* @instance
* @arg {number} start - Index at which to start changing the array. If greater than the length of the array, actual starting index will be set to the length of the array. If negative, will begin that many items from the end.
* @arg {number} nItemsToRemove - An integer indicating the number of old array items to remove. If nItemsToRemove is greater than the number of items left in the array starting at start, then all of the items through the end of the array will be deleted.
* @arg {...any} [itemToAdd] - Item that will be added to the array, starting at the index specified in the first argument.
* @emits module:bff/list#change:length
* @emits module:bff/list#item:added
* @emits module:bff/list#item:replaced
* @emits module:bff/list#item:removed
* @returns {any[]} Array of removed items
*/
listFunctions.splice = function (start, nItemsToRemove) {
if (RUNTIME_CHECKS) {
if (arguments.length < 2) { throw '"start" and "nItemsToRemove" arguments are mandatory'; }
if (typeof start !== 'number') { throw '"start" argument must be a number'; }
if (typeof nItemsToRemove !== 'number') { throw '"nItemsToRemove" argument must be a number'; }
}
var i;
var oldLength = this.length;
if (start < 0) {
start = oldLength + start;
}
nItemsToRemove = Math.min(nItemsToRemove, oldLength - start);
var nItemsToAdd = arguments.length - 2;
var nItemsToReplace = Math.min(nItemsToAdd, nItemsToRemove);
var nItemsAffected = Math.max(nItemsToAdd, nItemsToRemove);
var prevLength = this.length;
triggerPrechangeLengthEvent(this);
var privArray = this.__private.array;
var deletedItems = privArray.splice.apply(privArray, arguments);
for (i = 0; i < nItemsAffected; ++i) {
if (i < nItemsToAdd) {
onItemAdded(this, privArray[start + i], start + i);
}
if (i < nItemsToReplace) {
onItemReplaced(this, privArray[start + i], deletedItems[i], start + i);
}
if (i < nItemsToRemove) {
onItemRemoved(this, deletedItems[i], start + i);
}
}
triggerChangeLengthEvent(this, prevLength);
return deletedItems;
};
[
/**
* Executes the given function once per List item. Mirrors Array.forEach behavior.
* @func forEach
* @instance
* @arg {module:bff/list~forEachCallback} callback - The function that will be called once per List item.
* @arg {any} [thisArg] - Value to use as "this" when executing callback.
*/
'forEach',
/**
* Mirrors Array.every behavior.
* @func every
* @instance
* @arg {module:bff/list~predicateCallback} predicate - Executed once per List item.
* @arg {any} [thisArg] - Value to use as "this" when executing callback.
* @returns {boolean} true if all items passes the predicate test, false otherwise.
*/
'every',
/**
* Mirrors Array.some behavior.
* @func some
* @instance
* @arg {module:bff/list~predicateCallback} predicate - Executed once per List item.
* @arg {any} [thisArg] - Value to use as "this" when executing callback.
* @returns {boolean} true if at least one item passes the predicate test, false otherwise.
*/
'some',
/**
* Mirrors Array.indexOf behavior.
* @func indexOf
* @instance
* @arg {any} searchItem - The item to locate within the List.
* @arg {number} [fromIndex] - The index to start the search at. If the index is greater than or equal to the List's length, -1 is returned, which means the List will not be searched. If the provided index value is a negative number, it is taken as the offset from the end of the List.
* @returns {number} The first index at which a given item can be found in the List, or -1 if it is not present.
*/
'indexOf',
/**
* @func lastIndexOf
* @instance
* @arg {any} searchItem - The item to locate within the List.
* @arg {number} [fromIndex] - The index at which to start searching backwards. Defaults to the List's length minus one, i.e. the whole List will be searched. If the index is greater than or equal to the length of the List, the whole List will be searched. If negative, it is taken as the offset from the end of the List. Mirrors Array.lastIndexOf behavior.
* @returns {number} The last index at which a given item can be found in the List, or -1 if it is not present.
*/
'lastIndexOf',
/**
* @func join
* @instance
* @arg {string} [separator] - Specifies a string to separate each item of the List. If omitted, the List items are separated with a comma. Mirrors Array.join behavior.
* @returns {string} The stringified List items, joined by the 'separator' string argument.
*/
'join',
/**
* Applies a function against an accumulator and each value of the List (from left-to-right) to reduce it to a single value. Mirrors Array.reduce behavior.
* @func reduce
* @instance
* @arg {module:bff/list~reduceCallback} callback - The function that will be called once per List item.
* @arg {any} initialValue - Value to use as the first argument to the first call of the callback.
* @returns {any} Aggregated value
*/
'reduce',
/**
* Applies a function against an accumulator and each value of the List (from right-to-left) to reduce it to a single value. Mirrors Array.reduceRight behavior.
* @func reduceRight
* @instance
* @arg {module:bff/list~reduceCallback} callback - The function that will be called once per List item.
* @arg {any} initialValue - Value to use as the first argument to the first call of the callback.
* @returns {any} Aggregated value
*/
'reduceRight'
].forEach(function (funcName) {
listFunctions[funcName] = Array.prototype[funcName];
});
[
/**
* Sorts the items of the List in place. The sort is not necessarily stable. The default sort order is according to string Unicode code points, unless a custom comparator finction is provided. Mirrors the behavior of Array.sort. No events are emitted by this operation.
* @func sort
* @instance
* @arg {module:bff/list~compareFunction} [comparator] - A function that specifies the ordering of two arbitrary List items. Called multiple times in order to produce a total ordering of the items.
* @returns {List} The sorted list.
*/
'sort',
/**
* Reverses the List in place. Mirros the behavior of Array.reverse. No events are emitted by this operation.
* @func reverse
* @instance
*/
'reverse'
].forEach(function (funcName) {
listFunctions[funcName] = delegate(funcName);
});
[
/**
* Creates a new List with all items that pass the test implemented by the predicate function test. The original list is unchanged. Mirrors the behavior of Array.filter.
* @func filter
* @instance
* @arg {module:bff/list~predicateCallback} predicate - Executed once per List item.
* @returns {List} The new List.
*/
'filter',
/**
* Creates a new List from the range of current items specified by the begin and end arguments. Mirrors the behavior of Array.slice.
* @func slice
* @instance
* @arg {number} [begin] Index that specifies the beginning of the range. Inclusive. A negative index will
* be relative to the end of the List instead of the beginning.
* @arg {number} [end] Index that specifies the end of the range. Exclusive. A negative index will
* be relative to the end of the List instead of the beginning.
* @returns {List} The new List.
*/
'slice',
/**
* Creates a new List with the results of calling a provided callback function on every element in the List. Mirrors the behavior of Array.map.
* @func map
* @instance
* @arg {module:bff/module~mapCallback} callback - Executed once per List item.
* @arg {any} [thisArg] - Value to use as "this" when executing callback.
* @returns {List} The new List.
*/
'map'
].forEach(function (funcName) {
listFunctions[funcName] = delegateCreator(funcName);
});
/**
* Returns a new List comprised of the List on which it is called joined with the List(s), Array(s) and/or value(s) provided as arguments. Mirrors the behavior of Array.concat.
* @instance
* @arg {...any} value - A List, Array or value that will be concatenated with the original List. Lists and Arrays will be deconstructed and each item added to the new List.
* @returns {List} The new List.
*/
listFunctions.concat = function () {
for (var i = 0, n = arguments.length; i < n; ++i) {
var argument = arguments[i];
if (!(argument instanceof Array) && argument.length !== undefined) {
arguments[i] = argument.toArray();
}
}
return new this.constructor(this.__private.array.concat.apply(this.__private.array, arguments));
};
/**
* Mutates the List by removing all items that does not pass the predicate function test.
* @instance
* @arg {module:bff/list~predicateCallback} predicate - Called once per List item to determine whether the item should be removed.
* @arg {any} [thisArg] - Value to use as "this" when executing predicate callback.
* @emits module:bff/list#change:length
* @emits module:bff/list#item:removed
* @returns {List} The filtered List.
*/
listFunctions.filterMut = function (predicate, thisArg) {
if (RUNTIME_CHECKS) {
if (typeof predicate !== 'function') { throw '"predicate" argument must be a function'; }
}
var removeCount = 0;
for (var i = this.length - 1; i >= -1; --i) {
if (i > -1 && !predicate.call(thisArg, this[i], i, this)) {
removeCount++;
} else if (removeCount) {
this.splice(i + 1, removeCount);
removeCount = 0;
}
}
return this;
};
/**
* Mutates the List by removing all occurances of the provided item.
* @instance
* @arg {any} item - The item to remove from the list.
* @emits module:bff/list#change:length
* @emits module:bff/list#item:removed
* @returns {List} The List, without any occurances of 'item'.
*/
listFunctions.remove = function (item) {
return this.filterMut(function (listItem) { return item !== listItem; });
};
/**
* Mutates the List by removing all items.
* @instance
* @emits module:bff/list#change:length
* @emits module:bff/list#item:removed
* @returns {List} The List, now empty.
*/
listFunctions.clear = function () {
return this.splice(0, this.length);
};
/**
* Mutates the List by adding the items comprising the provided List or Array to the end of the List.
* @instance
* @arg {module:bff/list|Array} items - The items that will be appended to the List.
* @emits module:bff/list#change:length
* @emits module:bff/list#item:added
* @returns {List} The List, with new items appended.
*/
listFunctions.pushAll = function (items) {
if (RUNTIME_CHECKS && (!items || items.length === undefined)) {
throw '"items" argument must have a length property';
}
items.length && this.push.apply(this, items);
return this.length;
};
/**
* Mutates the List by removing all elements outside of range specified by the begin and end arguments.
* @instance
* @arg {number} [begin] Index that specifies the beginning of the range. Inclusive. A negative index will be relative to the end of the List instead of the beginning.
* @arg {number} [end] Index that specifies the end of the range. Exclusive. A negative index will be relative to the end of the List instead of the beginning.
* @emits module:bff/list#change:length
* @emits module:bff/list#item:removed
* @returns {List} The List, sans the items outside of the specified range.
*/
listFunctions.sliceMut = function (begin, end) {
if (RUNTIME_CHECKS) {
if (arguments.length < 2) { throw '"begin" and "end" arguments are mandatory'; }
if (typeof begin !== 'number') { throw '"begin" argument must be a number'; }
if (typeof end !== 'number') { throw '"end" argument must be a number'; }
}
var length = this.length;
end = (typeof end !== 'undefined') ? end : length;
// Handle negative value for "begin"
var start = begin || 0;
start = (start >= 0) ? start : Math.max(0, length + start);
// Handle negative value for "end"
var upTo = ((typeof end === 'number') ? Math.min(end, length) : length) - 1;
end < 0 && (upTo = length + end);
// Actual expected size of the slice
var size = upTo - start;
if (size !== length) {
this.splice(0, start);
this.splice(upTo, length - upTo);
}
return this;
};
/**
* Replaces all items in the List with new items, generated by the callback function.
* @instance
* @arg {module:bff/module~mapCallback} callback - Executed once per List item to produce new items.
* @arg {any} [thisArg] - Value to use as "this" when executing callback.
* @emits module:bff/list#item:replaced
* @returns {List} The List, with all items updated.
*/
listFunctions.mapMut = function (callback, thisArg) {
if (RUNTIME_CHECKS && typeof callback !== 'function') { throw '"callback" argument must be a function'; }
for (var i = 0, length = this.length; i < length; ++i) {
this[i] = callback.call(thisArg, this[i], i, this);
}
return this;
};
/**
* Returns the first item in the List that passes the predicate function test, or undefined if no item passes.
* @instance
* @arg {module:bff/list~predicateCallback} predicate - Called once per List item to in order to find a matching item.
* @returns {any|undefined} The matching item, if any.
*/
listFunctions.find = function (predicate, thisArg) {
if (RUNTIME_CHECKS && typeof predicate !== 'function') { throw '"predicate" argument must be a function'; }
for (var i = 0, length = this.length; i < length; ++i) {
if (predicate.call(thisArg, this[i], i, this)) { return this[i]; }
}
};
/**
* Returns the index of the first item in the List that passes the predicate function test, or -1 if no item passes.
* @instance
* @arg {module:bff/list~predicateCallback} predicate - Called once per List item to in order to find a matching item.
* @returns {number} The position of the matching item, or -1 if none matches.
*/
listFunctions.findIndex = function (predicate, thisArg) {
if (RUNTIME_CHECKS && typeof predicate !== 'function') { throw '"predicate" argument must be a function'; }
for (var i = 0, length = this.length; i < length; ++i) {
if (predicate.call(thisArg, this[i], i, this)) { return i; }
}
return -1;
};
/**
* Returns whether the provided item is part of the List.
* @instance
* @arg {any} item
* @arg {number} [fromIndex] - The index to start the search at. If the index is greater than or equal to the List's length, -1 is returned, which means the List will not be searched. If the provided index value is a negative number, it is taken as the offset from the end of the List.
* @returns {boolean} True if the item is part of the List, false otherwise.
*/
listFunctions.includes = function (item, fromIndex) {
if (RUNTIME_CHECKS && arguments.length > 1 && typeof fromIndex !== 'number') {
throw '"fromIndex" number must be a number';
}
fromIndex = fromIndex || 0;
var index = this.__private.array.indexOf(item);
return index !== -1 && index >= fromIndex;
};
/**
* Returns an newly created Array, containing all the items of the List. List properties are not copied to the Array object. The items are copied shallowly.
* @instance
* @returns {Array}
*/
listFunctions.toArray = function () {
return this.__private.array.slice();
};
/**
* Returns a newly created Array representation of the List, containing deep copies of all the List's items, but not its properties.
* @instance
* @returns {Array}
*/
listFunctions.toJSON = function () {
var jsonObj = new Array(this.length);
for (var i = 0, n = jsonObj.length; i < n; ++i) {
var item = this[i];
jsonObj[i] = item instanceof Object ?
(item.toJSON ? item.toJSON() : JSON.parse(JSON.stringify(item))) : item;
}
return jsonObj;
};
/**
* Returns a newly created Object containing the List's deep copied properties.
* @instance
* @returns {Object}
*/
listFunctions.propertiesToJSON = function () {
return Record.prototype.toJSON.call(this);
};
/**
* Augmented version of {@link bff/event-emitter#addEventListener} that provides the option to listen to events emitted from any item in the List. To add a listener to an item event, prepend the event name with 'item:'. For instance, to listen for the 'change' event on all the list items (even ones added after the listener was created), add a listener for the 'item:change' event.
* @instance
* @arg {string} - Identifier string for the event.
*/
listFunctions.addEventListener = function (eventName) {
if (RUNTIME_CHECKS && typeof eventName !== 'string') {
throw '"eventName" argument must be a string';
}
if (!ITEM_EVENT_TOKEN_MATCHER.test(eventName) || this.__private.reEmittingEvents[eventName]) { return; }
this.__private.reEmittingEvents[eventName] = true;
var strippedEventName = eventName.replace(ITEM_EVENT_TOKEN_MATCHER, '');
for (var i = 0, n = this.length; i < n; ++i) {
var item = this[i];
isEmitter(item) && reemitItemEvent(this, item, strippedEventName, eventName);
}
};
/**
* Augmented version of {@link bff/event-emitter#removeEventListener} that provides functionality for removing "item" event listeners, e.g. for stop listening to events like "items:change".
* @instance
* @arg {string} - Identifier string for the event.
*/
listFunctions.removeEventListener = function (eventName) {
if (RUNTIME_CHECKS && typeof eventName !== 'string') {
throw '"eventName" argument must be a string';
}
if (!ITEM_EVENT_TOKEN_MATCHER.test(eventName)) { return; }
// Check if we still need to re-emit this event
var listeners = this.__private.listeners[eventName];
if (listeners && listeners.length) { return; }
delete this.__private.reEmittingEvents[eventName];
var strippedEventName = eventName.replace(ITEM_EVENT_TOKEN_MATCHER, '');
this.stopListening(undefined, strippedEventName);
};
extend(listFunctions, eventEmitter, { 'function': 'merge' });
/**
* @instance
* @returns {string} A human readable string representation of the List.
*/
listFunctions.toString = function () {
return JSON.stringify(this, undefined, 2);
};
function withProperties(schema) {
for (var propName in schema) {
var propertySchema = schema[propName];
if (RUNTIME_CHECKS && !propertySchema.getter) {
throw 'List property ' + propName + ' must have a custom getter function';
}
if (RUNTIME_CHECKS && propertySchema.setter) {
throw 'List property ' + propName + ' may not have a setter';
}
propertySchema.setter = false;
}
extend(schema, {
length: {
getter: function () { return this.__private.array.length; },
setter: false,
},
first: {
getter: function () { return this[0]; },
setter: false,
},
last: {
getter: function () { return this[this.length - 1]; },
setter: false,
}
});
var RecordSubclass = Record.withProperties(schema, true);
function List(items) {
if (RUNTIME_CHECKS && arguments.length > 0 && (!items || items.length === undefined)) {
throw '"items" argument must have a length property';
}
this.__private || Object.defineProperty(this, '__private', { writable: true, value: {}, });
this.__private.array = [];
this.__private.reEmittingEvents = {};
this.listenTo(this, CHANGE_LENGTH_EVENT, function (length, prevLength) {
var diff = length - prevLength;
var i;
if (diff > 0) {
for (i = prevLength; i < length; ++i) {
Object.defineProperty(this, i, {
enumerable: true,
configurable: true,
get: makeGetter(i),
set: makeSetter(i),
});
}
} else {
for (i = length; i < prevLength; ++i) {
delete this[i];
}
}
});
// We don't want to send any arguments to the record constructor,
// none of the properties we added has any setters!
RecordSubclass.call(this);
items = items || [];
items.length && this.pushAll(items);
}
List.prototype = Object.create(RecordSubclass.prototype);
List.prototype.constructor = List;
extend(List.prototype, listFunctions);
return List;
}
/**
* Creates a new List, with an optional list or array of initial items.
* @constructor
* @alias module:bff/list
* @mixes bff/event-emitter
* @mixes bff/event-listener
* @arg {(Array|List)} [items] - Items that will be added to the List on creation.
*/
var List = withProperties({});
/**
* Creates a new List constructor function, that will create List instances with the property schema
* provided to this function
* @func
* @static
* @arg {Object} schema - An object describing the properties that will be part of all new instances created by the returned constructor function. The property descriptions should be on the same format at the schema specified for {@link module:bff/record.withProperties}, with the only exception that setters are not allowed.
* @returns {function} New constructor function based on the provided schema.
*/
List.withProperties = withProperties;
return List;
}
// Expose, based on environment
if (typeof define === 'function' && define.amd) { // AMD
define([ './extend', './event-emitter', './record' ], moduleFactory);
} else if (typeof exports === 'object') { // Node, CommonJS-like
module.exports = moduleFactory(require('./extend'), require('./event-emitter'), require('./record'));
} else { // Browser globals
var bff = window.bff = window.bff || {};
bff.List = moduleFactory(bff.extend, bff.eventEmitter, bff.Record);
}
}());