source: trip-planner-front/node_modules/asynckit/serialOrdered.js@ 76712b2

Last change on this file since 76712b2 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.7 KB
Line 
1var iterate = require('./lib/iterate.js')
2 , initState = require('./lib/state.js')
3 , terminator = require('./lib/terminator.js')
4 ;
5
6// Public API
7module.exports = serialOrdered;
8// sorting helpers
9module.exports.ascending = ascending;
10module.exports.descending = descending;
11
12/**
13 * Runs iterator over provided sorted array elements in series
14 *
15 * @param {array|object} list - array or object (named list) to iterate over
16 * @param {function} iterator - iterator to run
17 * @param {function} sortMethod - custom sort function
18 * @param {function} callback - invoked when all elements processed
19 * @returns {function} - jobs terminator
20 */
21function serialOrdered(list, iterator, sortMethod, callback)
22{
23 var state = initState(list, sortMethod);
24
25 iterate(list, iterator, state, function iteratorHandler(error, result)
26 {
27 if (error)
28 {
29 callback(error, result);
30 return;
31 }
32
33 state.index++;
34
35 // are we there yet?
36 if (state.index < (state['keyedList'] || list).length)
37 {
38 iterate(list, iterator, state, iteratorHandler);
39 return;
40 }
41
42 // done here
43 callback(null, state.results);
44 });
45
46 return terminator.bind(state, callback);
47}
48
49/*
50 * -- Sort methods
51 */
52
53/**
54 * sort helper to sort array elements in ascending order
55 *
56 * @param {mixed} a - an item to compare
57 * @param {mixed} b - an item to compare
58 * @returns {number} - comparison result
59 */
60function ascending(a, b)
61{
62 return a < b ? -1 : a > b ? 1 : 0;
63}
64
65/**
66 * sort helper to sort array elements in descending order
67 *
68 * @param {mixed} a - an item to compare
69 * @param {mixed} b - an item to compare
70 * @returns {number} - comparison result
71 */
72function descending(a, b)
73{
74 return -1 * ascending(a, b);
75}
Note: See TracBrowser for help on using the repository browser.