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

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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

mercurial