addon-sdk/source/lib/sdk/util/array.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/lib/sdk/util/array.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,123 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +"use strict";
     1.8 +
     1.9 +module.metadata = {
    1.10 +  "stability": "experimental"
    1.11 +};
    1.12 +
    1.13 +/**
    1.14 + * Returns `true` if given `array` contain given `element` or `false`
    1.15 + * otherwise.
    1.16 + * @param {Array} array
    1.17 + *    Target array.
    1.18 + * @param {Object|String|Number|Boolean} element
    1.19 + *    Element being looked up.
    1.20 + * @returns {Boolean}
    1.21 + */
    1.22 +var has = exports.has = function has(array, element) {
    1.23 +  // shorter and faster equivalent of `array.indexOf(element) >= 0`
    1.24 +  return !!~array.indexOf(element);
    1.25 +};
    1.26 +var hasAny = exports.hasAny = function hasAny(array, elements) {
    1.27 +  if (arguments.length < 2)
    1.28 +    return false;
    1.29 +  if (!Array.isArray(elements))
    1.30 +    elements = [ elements ];
    1.31 +  return array.some(function (element) {
    1.32 +      return has(elements, element);
    1.33 +  });
    1.34 +};
    1.35 +
    1.36 +/**
    1.37 + * Adds given `element` to the given `array` if it does not contain it yet.
    1.38 + * `true` is returned if element was added otherwise `false` is returned.
    1.39 + * @param {Array} array
    1.40 + *    Target array.
    1.41 + * @param {Object|String|Number|Boolean} element
    1.42 + *    Element to be added.
    1.43 + * @returns {Boolean}
    1.44 + */
    1.45 +var add = exports.add = function add(array, element) {
    1.46 +  var result;
    1.47 +  if ((result = !has(array, element)))
    1.48 +    array.push(element);
    1.49 +
    1.50 +  return result;
    1.51 +};
    1.52 +
    1.53 +/**
    1.54 + * Removes first occurrence of the given `element` from the given `array`. If
    1.55 + * `array` does not contain given `element` `false` is returned otherwise
    1.56 + * `true` is returned.
    1.57 + * @param {Array} array
    1.58 + *    Target array.
    1.59 + * @param {Object|String|Number|Boolean} element
    1.60 + *    Element to be removed.
    1.61 + * @returns {Boolean}
    1.62 + */
    1.63 +exports.remove = function remove(array, element) {
    1.64 +  var result;
    1.65 +  if ((result = has(array, element)))
    1.66 +    array.splice(array.indexOf(element), 1);
    1.67 +
    1.68 +  return result;
    1.69 +};
    1.70 +
    1.71 +/**
    1.72 + * Produces a duplicate-free version of the given `array`.
    1.73 + * @param {Array} array
    1.74 + *    Source array.
    1.75 + * @returns {Array}
    1.76 + */
    1.77 +function unique(array) {
    1.78 +  return array.reduce(function(result, item) {
    1.79 +    add(result, item);
    1.80 +    return result;
    1.81 +  }, []);
    1.82 +};
    1.83 +exports.unique = unique;
    1.84 +
    1.85 +/**
    1.86 + * Produce an array that contains the union: each distinct element from all
    1.87 + * of the passed-in arrays.
    1.88 + */
    1.89 +function union() {
    1.90 +  return unique(Array.concat.apply(null, arguments));
    1.91 +};
    1.92 +exports.union = union;
    1.93 +
    1.94 +exports.flatten = function flatten(array){
    1.95 +   var flat = [];
    1.96 +   for (var i = 0, l = array.length; i < l; i++) {
    1.97 +    flat = flat.concat(Array.isArray(array[i]) ? flatten(array[i]) : array[i]);
    1.98 +   }
    1.99 +   return flat;
   1.100 +};
   1.101 +
   1.102 +function fromIterator(iterator) {
   1.103 +  let array = [];
   1.104 +  if (iterator.__iterator__) {
   1.105 +    for each (let item in iterator)
   1.106 +      array.push(item);
   1.107 +  }
   1.108 +  else {
   1.109 +    for (let item of iterator)
   1.110 +      array.push(item);
   1.111 +  }
   1.112 +  return array;
   1.113 +}
   1.114 +exports.fromIterator = fromIterator;
   1.115 +
   1.116 +function find(array, predicate, fallback) {
   1.117 +  var index = 0;
   1.118 +  var count = array.length;
   1.119 +  while (index < count) {
   1.120 +    var value = array[index];
   1.121 +    if (predicate(value)) return value;
   1.122 +    else index = index + 1;
   1.123 +  }
   1.124 +  return fallback;
   1.125 +}
   1.126 +exports.find = find;

mercurial