michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: "use strict"; michael@0: michael@0: module.metadata = { michael@0: "stability": "experimental" michael@0: }; michael@0: michael@0: /** michael@0: * Returns `true` if given `array` contain given `element` or `false` michael@0: * otherwise. michael@0: * @param {Array} array michael@0: * Target array. michael@0: * @param {Object|String|Number|Boolean} element michael@0: * Element being looked up. michael@0: * @returns {Boolean} michael@0: */ michael@0: var has = exports.has = function has(array, element) { michael@0: // shorter and faster equivalent of `array.indexOf(element) >= 0` michael@0: return !!~array.indexOf(element); michael@0: }; michael@0: var hasAny = exports.hasAny = function hasAny(array, elements) { michael@0: if (arguments.length < 2) michael@0: return false; michael@0: if (!Array.isArray(elements)) michael@0: elements = [ elements ]; michael@0: return array.some(function (element) { michael@0: return has(elements, element); michael@0: }); michael@0: }; michael@0: michael@0: /** michael@0: * Adds given `element` to the given `array` if it does not contain it yet. michael@0: * `true` is returned if element was added otherwise `false` is returned. michael@0: * @param {Array} array michael@0: * Target array. michael@0: * @param {Object|String|Number|Boolean} element michael@0: * Element to be added. michael@0: * @returns {Boolean} michael@0: */ michael@0: var add = exports.add = function add(array, element) { michael@0: var result; michael@0: if ((result = !has(array, element))) michael@0: array.push(element); michael@0: michael@0: return result; michael@0: }; michael@0: michael@0: /** michael@0: * Removes first occurrence of the given `element` from the given `array`. If michael@0: * `array` does not contain given `element` `false` is returned otherwise michael@0: * `true` is returned. michael@0: * @param {Array} array michael@0: * Target array. michael@0: * @param {Object|String|Number|Boolean} element michael@0: * Element to be removed. michael@0: * @returns {Boolean} michael@0: */ michael@0: exports.remove = function remove(array, element) { michael@0: var result; michael@0: if ((result = has(array, element))) michael@0: array.splice(array.indexOf(element), 1); michael@0: michael@0: return result; michael@0: }; michael@0: michael@0: /** michael@0: * Produces a duplicate-free version of the given `array`. michael@0: * @param {Array} array michael@0: * Source array. michael@0: * @returns {Array} michael@0: */ michael@0: function unique(array) { michael@0: return array.reduce(function(result, item) { michael@0: add(result, item); michael@0: return result; michael@0: }, []); michael@0: }; michael@0: exports.unique = unique; michael@0: michael@0: /** michael@0: * Produce an array that contains the union: each distinct element from all michael@0: * of the passed-in arrays. michael@0: */ michael@0: function union() { michael@0: return unique(Array.concat.apply(null, arguments)); michael@0: }; michael@0: exports.union = union; michael@0: michael@0: exports.flatten = function flatten(array){ michael@0: var flat = []; michael@0: for (var i = 0, l = array.length; i < l; i++) { michael@0: flat = flat.concat(Array.isArray(array[i]) ? flatten(array[i]) : array[i]); michael@0: } michael@0: return flat; michael@0: }; michael@0: michael@0: function fromIterator(iterator) { michael@0: let array = []; michael@0: if (iterator.__iterator__) { michael@0: for each (let item in iterator) michael@0: array.push(item); michael@0: } michael@0: else { michael@0: for (let item of iterator) michael@0: array.push(item); michael@0: } michael@0: return array; michael@0: } michael@0: exports.fromIterator = fromIterator; michael@0: michael@0: function find(array, predicate, fallback) { michael@0: var index = 0; michael@0: var count = array.length; michael@0: while (index < count) { michael@0: var value = array[index]; michael@0: if (predicate(value)) return value; michael@0: else index = index + 1; michael@0: } michael@0: return fallback; michael@0: } michael@0: exports.find = find;