UNPKG

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