UNPKG

1.6 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.asArray = asArray;
7exports.removeFromArray = removeFromArray;
8exports.isPopulatedArray = isPopulatedArray;
9exports.toggleItem = toggleItem;
10
11function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
12
13function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
14
15function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
16
17function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }
18
19var isArray = Array.isArray;
20/**
21 * @param {*} value
22 * @return {Array}
23 */
24
25function asArray(value) {
26 if (isArray(value)) {
27 return value;
28 }
29
30 return [value];
31}
32/**
33 * Mutate a given array by removing a given element.
34 *
35 * @param {Array} array
36 * The array to mutate.
37 * @param {*} value
38 * The element to remove.
39 */
40
41
42function removeFromArray(array, value) {
43 var index = array.indexOf(value);
44 var length = 1;
45 array.splice(index, length);
46}
47/**
48 * @param {Array} array
49 * @return {boolean}
50 */
51
52
53function isPopulatedArray(array) {
54 return Boolean(isArray(array) && array.length);
55}
56
57function toggleItem(array, item) {
58 if (array.includes(item)) {
59 return array.filter(function (thisId) {
60 return thisId !== item;
61 });
62 }
63
64 return [].concat(_toConsumableArray(array), [item]);
65}
\No newline at end of file