Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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";
6 module.metadata = {
7 "stability": "experimental"
8 };
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 };
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);
47 return result;
48 };
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);
65 return result;
66 };
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;
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;
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 };
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;
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;