|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 "use strict"; |
|
5 |
|
6 module.metadata = { |
|
7 "stability": "experimental" |
|
8 }; |
|
9 |
|
10 /** |
|
11 * Returns `true` if given `array` contain given `element` or `false` |
|
12 * otherwise. |
|
13 * @param {Array} array |
|
14 * Target array. |
|
15 * @param {Object|String|Number|Boolean} element |
|
16 * Element being looked up. |
|
17 * @returns {Boolean} |
|
18 */ |
|
19 var has = exports.has = function has(array, element) { |
|
20 // shorter and faster equivalent of `array.indexOf(element) >= 0` |
|
21 return !!~array.indexOf(element); |
|
22 }; |
|
23 var hasAny = exports.hasAny = function hasAny(array, elements) { |
|
24 if (arguments.length < 2) |
|
25 return false; |
|
26 if (!Array.isArray(elements)) |
|
27 elements = [ elements ]; |
|
28 return array.some(function (element) { |
|
29 return has(elements, element); |
|
30 }); |
|
31 }; |
|
32 |
|
33 /** |
|
34 * Adds given `element` to the given `array` if it does not contain it yet. |
|
35 * `true` is returned if element was added otherwise `false` is returned. |
|
36 * @param {Array} array |
|
37 * Target array. |
|
38 * @param {Object|String|Number|Boolean} element |
|
39 * Element to be added. |
|
40 * @returns {Boolean} |
|
41 */ |
|
42 var add = exports.add = function add(array, element) { |
|
43 var result; |
|
44 if ((result = !has(array, element))) |
|
45 array.push(element); |
|
46 |
|
47 return result; |
|
48 }; |
|
49 |
|
50 /** |
|
51 * Removes first occurrence of the given `element` from the given `array`. If |
|
52 * `array` does not contain given `element` `false` is returned otherwise |
|
53 * `true` is returned. |
|
54 * @param {Array} array |
|
55 * Target array. |
|
56 * @param {Object|String|Number|Boolean} element |
|
57 * Element to be removed. |
|
58 * @returns {Boolean} |
|
59 */ |
|
60 exports.remove = function remove(array, element) { |
|
61 var result; |
|
62 if ((result = has(array, element))) |
|
63 array.splice(array.indexOf(element), 1); |
|
64 |
|
65 return result; |
|
66 }; |
|
67 |
|
68 /** |
|
69 * Produces a duplicate-free version of the given `array`. |
|
70 * @param {Array} array |
|
71 * Source array. |
|
72 * @returns {Array} |
|
73 */ |
|
74 function unique(array) { |
|
75 return array.reduce(function(result, item) { |
|
76 add(result, item); |
|
77 return result; |
|
78 }, []); |
|
79 }; |
|
80 exports.unique = unique; |
|
81 |
|
82 /** |
|
83 * Produce an array that contains the union: each distinct element from all |
|
84 * of the passed-in arrays. |
|
85 */ |
|
86 function union() { |
|
87 return unique(Array.concat.apply(null, arguments)); |
|
88 }; |
|
89 exports.union = union; |
|
90 |
|
91 exports.flatten = function flatten(array){ |
|
92 var flat = []; |
|
93 for (var i = 0, l = array.length; i < l; i++) { |
|
94 flat = flat.concat(Array.isArray(array[i]) ? flatten(array[i]) : array[i]); |
|
95 } |
|
96 return flat; |
|
97 }; |
|
98 |
|
99 function fromIterator(iterator) { |
|
100 let array = []; |
|
101 if (iterator.__iterator__) { |
|
102 for each (let item in iterator) |
|
103 array.push(item); |
|
104 } |
|
105 else { |
|
106 for (let item of iterator) |
|
107 array.push(item); |
|
108 } |
|
109 return array; |
|
110 } |
|
111 exports.fromIterator = fromIterator; |
|
112 |
|
113 function find(array, predicate, fallback) { |
|
114 var index = 0; |
|
115 var count = array.length; |
|
116 while (index < count) { |
|
117 var value = array[index]; |
|
118 if (predicate(value)) return value; |
|
119 else index = index + 1; |
|
120 } |
|
121 return fallback; |
|
122 } |
|
123 exports.find = find; |