1 | const expose = require('./expose');
|
2 | const { isArray } = Array;
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | function asArray(value) {
|
9 | if (isArray(value)) {
|
10 | return value;
|
11 | }
|
12 |
|
13 | return [value];
|
14 | }
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | function removeFromArray(array, value) {
|
25 | const index = array.indexOf(value);
|
26 | const length = 1;
|
27 |
|
28 | array.splice(index, length);
|
29 | }
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | function isPopulatedArray(array) {
|
36 | return Boolean(isArray(array) && array.length);
|
37 | }
|
38 |
|
39 | function toggleItem(array, item) {
|
40 | if(array.includes(item)) {
|
41 | return array.filter(thisId => thisId !== item);
|
42 | }
|
43 |
|
44 | return [...array, item];
|
45 | }
|
46 |
|
47 | module.exports = expose({
|
48 | asArray,
|
49 | removeFromArray,
|
50 | isPopulatedArray,
|
51 | toggleItem,
|
52 | });
|