This repository was archived by the owner on Dec 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathCollectionResource.js
More file actions
1185 lines (1091 loc) · 32.8 KB
/
CollectionResource.js
File metadata and controls
1185 lines (1091 loc) · 32.8 KB
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
'use strict';
var _ = require('underscore');
var async = require('async');
var InstanceResource = require('./InstanceResource');
var Resource = require('./Resource');
var utils = require('../utils');
/**
* @typedef {Object} CollectionQueryOptions
*
* This object allows you to add query parameters when requesting collections
* from the Stormpath REST API. The query parameters allow you to sort, search,
* and limit the results that are returned. For more information, please see:
* {@link https://docs.stormpath.com/rest/product-guide/latest/reference.html#collection-resources REST API Reference: Collection Resources}.
*
* @property {String} [expand]
* Comma-separated list of linked resources to expand on the returned resources.
* Not all links are available for expansion. For more information, please see:
* {@link https://docs.stormpath.com/rest/product-guide/latest/reference.html#links REST API Reference: Links}.
*
* Example: `'customData, directory'`
*
* @example
* // Expand custom data and directory when getting all accounts.
* client.getAccounts({ expand: 'customData, directory' });
*
* @property {Number} [limit]
* The maximum number of collection items to return for a single request.
* Minimum value is 1. Maximum value is 100. Default is 25.
*
* @property {Number} [offset]
* The zero-based starting index in the entire collection of the first item to
* return. Default is 0.
*
* @property {String} [orderBy]
* URL-encoded comma-separated list of ordering statements. Each ordering
* statement identifies a sortable attribute, and whether you would like the
* sorting to be ascending or descending.
*
* Example: `'email,username asc'`
*
* @example
* // Get all accounts, sorting by email, then username, ascending.
* application.getAccounts({ orderBy: 'email,username asc' }, callbackFn);
*
* @property {String} [q]
* Stormpath will perform a case-insensitive matching query on all viewable
* attributes in all the resources in the Collection. Note that “viewable” means
* that the attribute can be viewed by the current caller. For full
* documentation of filter searching in the Stormpah API, please see:
* {@link https://docs.stormpath.com/rest/product-guide/latest/reference.html#filter-search REST API Reference: Filter Search}.
*
* @example
* // Find any account that has a searchable attribute containing the text “Joe”:
* application.getAccounts({ q: 'Joe' }, callbackFn);
*
* @property {String} [attribute]
* Find objects by a specific attribute match, such as an email address. Not
* all attributes are available for matching on all resource types. Please
* refer to the specific resource type in this documentation, or see
* {@link https://docs.stormpath.com/rest/product-guide/latest/reference.html#attribute-search REST API Reference: Attribute Search}.
*
* @example
* // Find all accounts with this email address.
* application.getAccounts({ email: 'foo@bar.com'}, callbackFn);
*/
function wrapAsyncCallToAllPages(coll, func, args, callbackWrapper){
var page = _.pick(coll, [
'href', 'query', 'offset', 'limit', 'items',
'dataStore', 'instanceConstructor']);
var callback = args.pop();
function getNextPage(page, callback) {
// There is no next page.
if (page.items.length < page.limit) {
return callback();
}
// We've fully exhausted the current page. There could be another one on the server, so
// we have to execute another query to continue to the next page if one exists.
var nextQuery = utils.shallowCopy(page.query, {});
nextQuery.offset = page.offset + page.items.length;
nextQuery.limit = 100;
page.dataStore.getResource(page.href, nextQuery, page.instanceConstructor,
function onNextPage(err, collectionResource) {
if (err) {
return callback(err);
}
(typeof setImmediate !== 'undefined' ?
setImmediate : process.nextTick)(function(){
callback(null, collectionResource);
});
});
}
function worker(task, cb){
if (!task.collection.items || !task.collection.items.length){
return cb();
}
async.parallel([
function(parallel_cb){
if (callbackWrapper.isDone){
return parallel_cb();
}
var callArgs = Array.prototype
.concat(
[task.collection.items],
!!callbackWrapper.wrapPageArgs ? callbackWrapper.wrapPageArgs(args) : args,
callbackWrapper.wrapPageCallback(parallel_cb.bind(this, null)));
func.apply(this, callArgs);
},
function(parallel_cb){
if (callbackWrapper.isDone){
return parallel_cb();
}
getNextPage(task.collection, function(err, nextPage){
if (err || !nextPage){
return parallel_cb(err);
}
q.push({collection: nextPage});
parallel_cb();
});
}
], cb);
}
var q = async.queue(worker, 1);
q.drain = callbackWrapper.wrapCallback(callback);
q.push({ collection: page });
}
function ensureInstanceResources(items, InstanceConstructor, dataStore) {
var InstanceCtor = utils.valueOf(InstanceConstructor, InstanceResource);
if (!items || items.length === 0 || items[0] instanceof InstanceCtor /* already converted */) {
return items;
}
var convertedItems = [];
for (var i = 0; i < items.length; i++) {
var obj = items[i];
// We have to call instantiate via a require statement to avoid a circular dependency
// (instantiate references CollectionResource).
var converted = require('./ResourceFactory').instantiate(InstanceCtor, obj, null, dataStore);
convertedItems.push(converted);
}
return convertedItems;
}
/**
* @class CollectionResource
*
* @description
* Encapsulates a collection resource, such as an Application's Accounts
* collection, making it possible to iterate through the collection in an
* asynchronous manner. You do not need to manually construct a
* CollectionResource. Rather, you will obtain a resource collection from a
* getter method, such as {@link Application#getAccounts
* Application.getAccounts()}.
*
* Every CollectionResource has the following iteration methods, modeled after
* the [caolan/async library](https://github.com/caolan/async), for iterating
* over the paginated collection in an asynchronous manner.
*
* The collection resource will make multiple attempts to the REST API, to
* request each page in the collection. You can reduce
* the number of REST API calls by increasing the page size when you get the
* collection for the first time, using the `limit` option. See {@link
* CollectionQueryOptions}.
*/
function CollectionResource(/*data, query, InstanceCtor, dataStore*/) {
var args = Array.prototype.slice.call(arguments);
var data = args[0];
var dataStore = args[args.length - 1];
var query = null;
var InstanceCtor = InstanceResource; // Default for generic resource instances.
// Check if query params supplied.
if (args[1] instanceof Object && !(args[1] instanceof Function)) {
query = args[1];
}
// Check if a type-specific resource constructor function was supplied.
var secondToLastArg = args[args.length - 2];
if (secondToLastArg instanceof Function && utils.isAssignableFrom(Resource, secondToLastArg)) {
InstanceCtor = secondToLastArg;
}
// Convert raw items array (array of objects) to an array of Instance Resources.
if (data && data.hasOwnProperty('items') && data.items.length > 0) {
data.items = ensureInstanceResources(data.items, InstanceCtor, dataStore);
}
CollectionResource.super_.apply(this, [data, dataStore]);
Object.defineProperty(this, 'instanceConstructor', {
get: function getInstanceConstructor() {
return InstanceCtor;
},
set: function setInstanceConstructor(TheInstanceCtor) {
InstanceCtor = TheInstanceCtor;
}
});
this.instanceConstructor = InstanceCtor;
Object.defineProperty(this, 'query', {
get: function getQuery() {
return query;
},
set: function setQuery(aQuery) {
query = aQuery;
}
});
this.query = query;
}
utils.inherits(CollectionResource, Resource);
/**
* @name CollectionResource#concat
*
* @function
*
* @description
* Visit each resource in the collection and concatenate the return values of
* the `iterator`. Similar to {@link CollectionResource#map map}, but the
* return values should be arrays, and all arrays will be concatendated before
* being provied to the `doneCallback`.
*
* @param {Function} iterator
* The function to call, for each resource in the collection. Will be called
* with `(resource, next)`. The `next(err, results)` function must be called to
* advance to the next resource. If an `err` value is passed to next, the
* iteration will stop and the `doneCallback` will be called with the `err`
* value.
*
* @param {Function} doneCallback
* The function to call when iteration has ended. Will be called with `(err,
* [concatenatedResults])`.
*
* @example
* // Find all orders from all accounts, that are stored in your SQL database
*
* function iterator(account, next) {
* // Talk do your SQL databse, pseudo-code:
* sqlQuery('SELECT * FROM ORDERS WHERE userId=\"' + encodeURIComponent(account.customData.sqlId) + '\";', next);
* }
*
* function doneCallback(err, orders) {
* if (!err) {
* console.log(orders);
* }
* }
*
* application.getAccounts({ expand: 'customData' }, function (err, collection) {
* if (!err) {
* collection.concat(iterator, donCallback);
* }
* });
*/
/**
* @name CollectionResource#concatLimit
*
* @function
*
* @description
* The same interface as `concat`, but with a 2nd parameter that can be used to
* limit the number of parallel workers.
*
* @param {Function} iterator
* Same interface as {@link CollectionResource#concat concat}.
*
* @param {Number} limit
* The maximum number of parallel iterator workers.
*
* @param {Function} doneCallback
* Same interface as {@link CollectionResource#concat concat}.
*/
/**
* @name CollectionResource#concatSeries
*
* @function
*
* @description
* The same interface as `concat`, but the `iterator` will be called in series,
* not in parallel.
*
* @param {Function} iterator
* Same interface as {@link CollectionResource#concat concat}.
*
* @param {Function} doneCallback
* Same interface as {@link CollectionResource#concat concat}.
*/
/**
* @name CollectionResource#detect
*
* @function
*
* @description
* Visit each resource in the collection and return the first resource that
* passes a truth test in the iterator. The iterator is applied in parallel, and
* the first iterator to return true will end the iteration.
*
* @param {Function} iterator
* The function to call, for each resource in the collection. Will be called
* with `(resource, next)`. The `next(err, test)` function must be called to
* advance to the next resource. Iteration is ended as soon as a truthy value
* is provided to either `err` or `test`.
*
* @param {Function} doneCallback
* The function to call when iteration has ended. Will be called with `(err,
* foundResource)`.
*
* @example
* // Find the first account that likes pizza.
*
* function iterator(account, next) {
* next(null, account.customData.favoriteFood === 'pizza');
* }
*
* function doneCallback(err, foundAccount) {
* if (!err) {
* console.log('Guess who likes pizza??').
* console.log(foundAccount.fullName);
* }
* }
*
* application.getAccounts({ expand: 'customData' }, function (err, collection) {
* if (!err) {
* collection.detect(iterator, donCallback);
* }
* });
*/
/**
* @name CollectionResource#detectLimit
*
* @function
*
* @description
* The same interface as `detect`, but with a 2nd parameter that can be used to
* limit the number of parallel workers.
*
* @param {Function} iterator
* Same interface as {@link CollectionResource#detect detect}.
*
* @param {Number} limit
* The maximum number of parallel iterator workers.
*
* @param {Function} doneCallback
* Same interface as {@link CollectionResource#detect detect}.
*/
/**
* @name CollectionResource#detectSeries
*
* @function
*
* @description
* The same interface as `detect`, but the `iterator` will be called in series,
* not in parallel.
*
* @param {Function} iterator
* Same interface as {@link CollectionResource#detect detect}.
*
* @param {Function} doneCallback
* Same interface as {@link CollectionResource#detect detect}.
*/
/**
* @name CollectionResource#each
*
* @function
*
* @description
* Visit each resource in the collection. The `iterator` will be called in
* parallel.
*
* @param {Function} iterator
* The function to call, for each resource in the collection. Will be called
* with `(resource, next)`. The `next()` function must be called in order to
* advance to the next resource. If an `err` value is passed to next, the
* iteration will stop and the `doneCallback` will be called with the `err`
* value.
*
* @param {Function} doneCallback
* The function to call when all items have been visited. If `next(err)` has
* been used, the `err` will be passed to this function.
*
* @example
* function iterator(account, next) {
* console.log('Found account for ' + account.givenName + ' (' + account.email + ')');
* next();
* }
*
* function doneCallback(err) {
* if (!err) {
* console.log('All accounts have been visited.').
* }
* }
*
* application.getAccounts({ email: 'foo@example.com' }, function (err, collection) {
* if (!err) {
* collection.each(iterator, doneCallback);
* }
* });
*/
/**
* @name CollectionResource#eachLimit
*
* @function
*
* @description
* The same interface as `each`, but with a 2nd parameter that can be used to
* limit the number of parallel workers.
*
* @param {Function} iterator
* Same interface as {@link CollectionResource#each each}.
*
* @param {Number} limit
* The maximum number of parallel iterator workers.
*
* @param {Function} doneCallback
* Same interface as {@link CollectionResource#each each}.
*/
/**
* @name CollectionResource#eachSeries
*
* @function
*
* @description
* The same interface as `each`, but the `iterator` will be called in series,
* not in parallel.
*
* @param {Function} iterator
* Same interface as {@link CollectionResource#each each}.
*
* @param {Function} doneCallback
* Same interface as {@link CollectionResource#each each}.
*/
/**
* @name CollectionResource#every
*
* @function
*
* @description
* Visit each resource in the collection and return true if all resources pass
* a truth test in the `iterator`. The `iterator` will be called in parallel.
*
* @param {Function} iterator
* The function to call, for each resource in the collection. Will be called
* with `(resource, next)`. The `next(err, test)` function must be called to
* advance to the next resource. If an `err` value is passed to next, the
* iteration will stop and the `doneCallback` will be called with the `err`
* value.
*
* @param {Function} doneCallback
* The function to call when iteration has ended. Will be called with `(err,
* test)`.
*
* @example
* // Does everyone like Brie cheese?
*
* function iterator(account, next) {
* next(null, account.customData.likesBrie === true);
* }
*
* function doneCallback(err, test) {
* if (!err && test === true) {
* console.log('Everyone likes Brie??');
* }
* }
*
* application.getAccounts({ expand: 'customData' }, function (err, collection) {
* if (!err) {
* collection.every(iterator, donCallback);
* }
* });
*/
/**
* @name CollectionResource#everyLimit
*
* @function
*
* @description
* The same interface as `every`, but with a 2nd parameter that can be used to
* limit the number of parallel workers.
*
* @param {Function} iterator
* Same interface as {@link CollectionResource#every every}.
*
* @param {Number} limit
* The maximum number of parallel iterator workers.
*
* @param {Function} doneCallback
* Same interface as {@link CollectionResource#every every}.
*/
/**
* @name CollectionResource#everySeries
*
* @function
*
* @description
* The same interface as `every`, but the `iterator` will be called in series,
* not in parallel.
*
* @param {Function} iterator
* Same interface as {@link CollectionResource#every every}.
*
* @param {Function} doneCallback
* Same interface as {@link CollectionResource#every every}.
*/
/**
* @name CollectionResource#filter
*
* @function
*
* @description
* Visit each resource in the collection, and filter down to a new set of
* resources, based on a truth test that is evaluated by the `iterator`.
* The `iterator` will be called in parallel.
*
* @param {Function} iterator
* The function to call for each resource in the collection. Will be called
* with `(resource, next)`. The `next(err, test)` function must be called in order to
* advance to the next resource. The `test` value should be a boolean value that
* indicates if the resource should be included in the filtered collection.
*
* If an `err` value is passed to next, the iteration will stop and the
* `doneCallback` will be called with the `err` value.
*
* @param {Function} doneCallback
* The function to call when all items have been visited. Will be called with
* `(err, [filteredResources])`.
*
* @example
* // Find all accounts that have the 'paid' plan property, in custom data.
*
* function iterator(account, next) {
* next(null, account.customData.plan === 'paid');
* }
*
* function doneCallback(err, paidAccounts) {
* if (!err) {
* console.log('All paid accounts:').
* console.log(paidAccounts);
* }
* }
*
* application.getAccounts({ expand: 'customData' }, function (err, collection) {
* if (!err) {
* collection.filter(iterator, doneCallback);
* }
* });
*/
/**
* @name CollectionResource#filterLimit
*
* @function
*
* @description
* The same interface as `filter`, but with a 2nd parameter that can be used to
* limit the number of parallel workers.
*
* @param {Function} iterator
* Same interface as {@link CollectionResource#filter filter}.
*
* @param {Number} limit
* The maximum number of parallel iterator workers.
*
* @param {Function} doneCallback
* Same interface as {@link CollectionResource#filter filter}.
*/
/**
* @name CollectionResource#filterSeries
*
* @function
*
* @description
* The same interface as `filter`, but the `iterator` will be called in series,
* not in parallel.
*
* @param {Function} iterator
* Same interface as {@link CollectionResource#filter filter}.
*
* @param {Function} doneCallback
* Same interface as {@link CollectionResource#filter filter}.
*/
/**
* @name CollectionResource#map
*
* @function
*
* @description
* Visit each resource in the collection and produce a new value for that
* resource. The `iterator` will be called in parallel.
*
* @param {Function} iterator
* The function to call for each resource in the collection. Will be called
* with `(resource, next)`. The `next(err, value)` function must be called with
* the new value in order to advance to the next resource. If an `err` value is
* passed to next, the iteration will stop and the `doneCallback` will be called
* with the `err` value.
*
* @param {Function} doneCallback
* The function to call when all items have been visited. Will be called with
* `(err, [transformedResources])`.
*
* @example
* // Pluck the email address from all accounts.
*
* function iterator(account, next) {
* next(null, account.email);
* }
*
* function doneCallback(err, emails) {
* if (!err) {
* console.log('All email addresses in this application:').
* console.log(emails);
* }
* }
*
* application.getAccounts(function (err, collection) {
* if (!err) {
* collection.map(iterator, doneCallback);
* }
* });
*/
/**
* @name CollectionResource#mapLimit
*
* @function
*
* @description
* The same interface as `map`, but with a 2nd parameter that can be used to
* limit the number of parallel workers.
*
* @param {Function} iterator
* Same interface as {@link CollectionResource#map map}.
*
* @param {Number} limit
* The maximum number of parallel iterator workers.
*
* @param {Function} doneCallback
* Same interface as {@link CollectionResource#map map}.
*/
/**
* @name CollectionResource#mapSeries
*
* @function
*
* @description
* The same interface as `map`, but the `iterator` will be called in series,
* not in parallel.
*
* @param {Function} iterator
* Same interface as {@link CollectionResource#map map}.
*
* @param {Function} doneCallback
* Same interface as {@link CollectionResource#map map}.
*/
/**
* @name CollectionResource#reduce
*
* @function
*
* @description
* Visit each resource in the collection and use the `iterator` to reduce down
* to a new value. The `iterator` will be called in parallel.
*
* @param {*} initialValue
* The initial value that will then be passed to the `iterator` as `result`,
* this is the value that you will mutate over time as the iterator is called
* with every resource in the collection.
*
* @param {Function} iterator
* The function to call, for each resource in the collection. Will be called
* with `(result, resource, next)`. The `next(err, result)` function must be
* called to advance to the next resource. If an `err` value is passed to next,
* the iteration will stop and the `doneCallback` will be called with the `err`
* value.
*
* @param {Function} doneCallback
* The function to call when iteration has ended. Will be called with `(err,
* result)`.
*
* @example
* // Find all the unique favorite colors, of all your accounts
*
* function iterator(result, account, next) {
* var color = account.customData.favoriteColor;
* if (result.indexOf(color) === -1) {
* result.push(color);
* }
* return result;
* }
*
* function doneCallback(err, colors) {
* if (!err) {
* console.log(colors);
* }
* }
*
* application.getAccounts({ expand: 'customData' }, function (err, collection) {
* if (!err) {
* collection.reduce([], iterator, donCallback);
* }
* });
*/
/**
* @name CollectionResource#reduceRight
*
* @function
*
* @description
* The same interface as {@link CollectionResource#reduce reduce}, but operates
* on the collection in reverse order.
*
* @param {*} initialValue
* Same interface as {@link CollectionResource#reduce reduce}.
*
* @param {Function} iterator
* Same interface as {@link CollectionResource#reduce reduce}.
*
* @param {Function} doneCallback
* Same interface as {@link CollectionResource#reduce reduce}.
*/
/**
* @name CollectionResource#reject
*
* @function
*
* @description
* The opposite of {@link CollectionResource#filter filter}. Removes values that
* pass an async truth test.
*
* @param {Function} iterator
* Same interface as {@link CollectionResource#filter filter}, but removes
* values if they pass the truth test.
*
* @param {Function} doneCallback
* Same interface as {@link CollectionResource#filter filter}.
*/
/**
* @name CollectionResource#rejectLimit
*
* @function
*
* @description
* The same interface as `reject`, but with a 2nd parameter that can be used to
* limit the number of parallel workers.
*
* @param {Function} iterator
* Same interface as {@link CollectionResource#reject reject}.
*
* @param {Number} limit
* The maximum number of parallel iterator workers.
*
* @param {Function} doneCallback
* Same interface as {@link CollectionResource#reject reject}.
*/
/**
* @name CollectionResource#rejectSeries
*
* @function
*
* @description
* The same interface as `reject`, but the `iterator` will be called in series,
* not in parallel.
*
* @param {Function} iterator
* Same interface as {@link CollectionResource#reject reject}.
*
* @param {Function} doneCallback
* Same interface as {@link CollectionResource#reject reject}.
*/
/**
* @name CollectionResource#some
*
* @function
*
* @description
* Visit each resource in the collection and return true if any of the resources
* pass a truth test in the `iterator`. The `iterator` will be called in
* parallel, but iteration stops once the test is true for any resource.
*
* @param {Function} iterator
* The function to call for each resource in the collection. Will be called
* with `(resource, next)`. The `next(err, test)` function must be called to
* advance to the next resource. If an `err` value is passed to next, the
* iteration will stop and the `doneCallback` will be called with the `err`
* value.
*
* @param {Function} doneCallback
* The function to call when iteration has ended. Will be called with `(err,
* test)`.
*
* @example
* // Does anyone like Brie cheese?
*
* function iterator(account, next) {
* next(null, account.customData.likesBrie === true);
* }
*
* function doneCallback(err, test) {
* if (!err && test === true) {
* console.log('Some people like Brie.');
* }
* }
*
* application.getAccounts({ expand: 'customData' }, function (err, collection) {
* if (!err) {
* collection.some(iterator, donCallback);
* }
* });
*/
/**
* @name CollectionResource#someLimit
*
* @function
*
* @description
* The same interface as `some`, but with a 2nd parameter that can be used to
* limit the number of parallel workers.
*
* @param {Function} iterator
* Same interface as {@link CollectionResource#some some}.
*
* @param {Number} limit
* The maximum number of parallel iterator workers.
*
* @param {Function} doneCallback
* Same interface as {@link CollectionResource#some some}.
*/
/**
* @name CollectionResource#someSeries
*
* @function
*
* @description
* The same interface as `some`, but the `iterator` will be called in series,
* not in parallel.
*
* @param {Function} iterator
* Same interface as {@link CollectionResource#some some}.
*
* @param {Function} doneCallback
* Same interface as {@link CollectionResource#some some}.
*/
/**
* @name CollectionResource#sortBy
*
* @function
*
* @description
* Visit each resource in the collection and return the resources in a sorted
* array. The sort is determined by the sortable value that you your return in
* the `iterator`.
*
* @param {Function} iterator
* The function to call for each resource in the collection. Will be called
* with `(resource, next)`. The `next(err, sortableValue)` function must be
* called to advance to the next resource. If an `err` value is passed to next,
* the iteration will stop and the `doneCallback` will be called with the `err`
* value.
*
* @param {Function} doneCallback
* The function to call when iteration has ended. Will be called with `(err,
* test)`.
*
* @example
* // Sort accounts by their birthday.
*
* function iterator(account, next) {
* next(null, account.customData.birthday);
* }
*
* function doneCallback(err, sortedAccounts) {
* if (!err) {
* console.log(sortedAccounts);
* }
* }
*
* application.getAccounts({ expand: 'customData' }, function (err, collection) {
* if (!err) {
* collection.sortBy(iterator, donCallback);
* }
* });
*/
/**
* @name CollectionResource#sortByLimit
*
* @function
*
* @description
* The same interface as `sortBy`, but with a 2nd parameter that can be used to
* limit the number of parallel workers.
*
* @param {Function} iterator
* Same interface as {@link CollectionResource#sortBy sortBy}.
*
* @param {Number} limit
* The maximum number of parallel iterator workers.
*
* @param {Function} doneCallback
* Same interface as {@link CollectionResource#sortBy sortBy}.
*/
/**
* @name CollectionResource#sortBySeries
*
* @function
*
* @description
* The same interface as `sortBy`, but the `iterator` will be called in series,
* not in parallel.
*
* @param {Function} iterator
* Same interface as {@link CollectionResource#sortBy sortBy}.
*
* @param {Function} doneCallback
* Same interface as {@link CollectionResource#sortBy sortBy}.
*/
(function extendCollectionResourceWithAsyncMethods(){
function CallbackWrapper(argsLength){
var res = [];
this.wrapPageCallback = function wrapPageCallback(cb){
return function funcCallback(err, result){
if (argsLength === 1){
result = err;
err = null;
}
res = Array.prototype.concat.call(res, result);
cb(err, result);
};
};
this.wrapCallback = function(callback){
return function(err){
if (argsLength === 1){
return callback(res);
}
callback(err, res);
};
};
}
function Every(){
var res = true;
var that = this;
this.isDone = false;
this.wrapPageCallback = function wrapPageCallback(cb){
return function funcCallback(err, result){
that.isDone = err || !result;
res = res && result;
cb(err, result);
};