Last change
on this file since ceaed42 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
941 bytes
|
Line | |
---|
1 | // API
|
---|
2 | module.exports = state;
|
---|
3 |
|
---|
4 | /**
|
---|
5 | * Creates initial state object
|
---|
6 | * for iteration over list
|
---|
7 | *
|
---|
8 | * @param {array|object} list - list to iterate over
|
---|
9 | * @param {function|null} sortMethod - function to use for keys sort,
|
---|
10 | * or `null` to keep them as is
|
---|
11 | * @returns {object} - initial state object
|
---|
12 | */
|
---|
13 | function state(list, sortMethod)
|
---|
14 | {
|
---|
15 | var isNamedList = !Array.isArray(list)
|
---|
16 | , initState =
|
---|
17 | {
|
---|
18 | index : 0,
|
---|
19 | keyedList: isNamedList || sortMethod ? Object.keys(list) : null,
|
---|
20 | jobs : {},
|
---|
21 | results : isNamedList ? {} : [],
|
---|
22 | size : isNamedList ? Object.keys(list).length : list.length
|
---|
23 | }
|
---|
24 | ;
|
---|
25 |
|
---|
26 | if (sortMethod)
|
---|
27 | {
|
---|
28 | // sort array keys based on it's values
|
---|
29 | // sort object's keys just on own merit
|
---|
30 | initState.keyedList.sort(isNamedList ? sortMethod : function(a, b)
|
---|
31 | {
|
---|
32 | return sortMethod(list[a], list[b]);
|
---|
33 | });
|
---|
34 | }
|
---|
35 |
|
---|
36 | return initState;
|
---|
37 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.