PageSourceSearch

https://cdn4.hola.org/util/array.js

js hola.org collected 2026-09-24 10:18:50 UTC 5,488 bytes, 233 lines download raw bytes

1// LICENSE_CODE ZON ISC
2'use strict'; /*jslint node:true, browser:true*/
3(function(){
4var define;
5var is_node = typeof module=='object' && module.exports && module.children;
6if (!is_node)
7    define = self.define;
8else
9    define = require('./require_node.js').define(module, '../');
10define([], function(){
11var E = {};
12
13var proto_slice = Array.prototype.slice;
14E.copy = function(a){
15    switch (a.length)
16    {
17    case 0: return [];
18    case 1: return [a[0]];
19    case 2: return [a[0], a[1]];
20    case 3: return [a[0], a[1], a[2]];
21    case 4: return [a[0], a[1], a[2], a[3]];
22    case 5: return [a[0], a[1], a[2], a[3], a[4]];
23    default: return proto_slice.call(a);
24    }
25};
26
27E.push = function(a){
28    for (var i=1; i<arguments.length; i++)
29    {
30        var arg = arguments[i];
31        if (Array.isArray(arg))
32            a.push.apply(a, arg);
33        else
34            a.push(arg);
35    }
36    return a.length;
37};
38E.unshift = function(a){
39    for (var i=arguments.length-1; i>0; i--)
40    {
41        var arg = arguments[i];
42        if (Array.isArray(arg))
43            a.unshift.apply(a, arg);
44        else
45            a.unshift(arg);
46    }
47    return a.length;
48};
49
50E.slice = function(args, from, to){
51    return Array.prototype.slice.call(args, from, to); };
52
53E.compact = function(a){ return E.compact_self(a.slice()); };
54E.compact_self = function(a){
55    var i, j, n = a.length;
56    for (i=0; i<n && a[i]; i++);
57    if (i==n)
58        return a;
59    for (j=i; i<n; i++)
60    {
61        if (!a[i])
62            continue;
63        a[j++] = a[i];
64    }
65    a.length = j;
66    return a;
67};
68
69// same as _.flatten(a, true)
70E.flatten_shallow = function(a){ return Array.prototype.concat.apply([], a); };
71E.flatten = function(a){
72    var _a = [], i;
73    for (i=0; i<a.length; i++)
74    {
75        if (Array.isArray(a[i]))
76            Array.prototype.push.apply(_a, E.flatten(a[i]));
77        else
78            _a.push(a[i]);
79    }
80    return _a;
81};
82E.unique = function(a){
83    var _a = [];
84    for (var i=0; i<a.length; i++)
85    {
86        if (!_a.includes(a[i]))
87            _a.push(a[i]);
88    }
89    return _a;
90};
91E.to_nl = function(a, sep){
92    if (!a || !a.length)
93        return '';
94    if (sep===undefined)
95        sep = '\n';
96    return a.join(sep)+sep;
97};
98E.sed = function(a, regex, replace){
99    var _a = new Array(a.length), i;
100    for (i=0; i<a.length; i++)
101        _a[i] = a[i].replace(regex, replace);
102    return _a;
103};
104E.grep = function(a, regex, replace){
105    var _a = [], i;
106    for (i=0; i<a.length; i++)
107    {
108        // don't use regex.test() since with //g sticky tag it does not reset
109        if (a[i].search(regex)<0)
110            continue;
111        if (replace!==undefined)
112            _a.push(a[i].replace(regex, replace));
113        else
114            _a.push(a[i]);
115    }
116    return _a;
117};
118
119E.rm_elm = function(a, elm){
120    var i = a.indexOf(elm);
121    if (i<0)
122        return;
123    a.splice(i, 1);
124    return elm;
125};
126
127E.rm_elm_tail = function(a, elm){
128    var i = a.length-1;
129    if (elm===a[i]) // fast-path
130    {
131        a.pop();
132        return elm;
133    }
134    if ((i = a.lastIndexOf(elm, i-1))<0)
135        return;
136    a.splice(i, 1);
137    return elm;
138};
139
140E.add_elm = function(a, elm){
141    if (a.includes(elm))
142        return;
143    a.push(elm);
144    return elm;
145};
146
147E.split_every = function(a, n){
148    var ret = [];
149    for (var i=0; i<a.length; i+=n)
150        ret.push(a.slice(i, i+n));
151    return ret;
152};
153
154E.split_at = function(a, delim){
155    var ret = [];
156    delim = delim||'';
157    for (var i=0; i<a.length; i++)
158    {
159        var chunk = [];
160        for (; i<a.length && a[i]!=delim; i++)
161            chunk.push(a[i]);
162        if (chunk.length)
163            ret.push(chunk);
164    }
165    return ret;
166};
167
168E.rotate = function(a, n){
169    if (a && a.length>1 && (n = n%a.length))
170        E.unshift(a, a.splice(n));
171    return a;
172};
173
174E.move = function(a, from, to, n){
175    return Array.prototype.splice.apply(a, [to, n]
176        .concat(a.slice(from, from+n)));
177};
178
179E.binary_search = function(a, cmp){
180    var low = 0, high = a.length-1, mid;
181    while (low<=high)
182    {
183        mid = low+high >> 1;
184        var cmp_res = cmp(a[mid], mid, low, high);
185        if (cmp_res==0)
186            return mid;
187        if (cmp_res<0)
188            low = mid+1;
189        else
190            high = mid-1;
191    }
192    return mid;
193};
194
195E.trunc = function(a, len){
196    if (!a || !a.length || a.length<=len)
197        return a;
198    return a.slice(0, len).concat('...');
199};
200
201E.to_array = function(v){ return Array.isArray(v) ? v : v==null ? [] : [v]; };
202
203var proto = {};
204proto.sed = function(regex, replace){
205    return E.sed(this, regex, replace); };
206proto.grep = function(regex, replace){
207    return E.grep(this, regex, replace); };
208proto.to_nl = function(sep){ return E.to_nl(this, sep); };
209proto.push_a = function(){
210    return E.push.apply(null, [this].concat(Array.from(arguments))); };
211proto.unshift_a = function(){
212    return E.unshift.apply(null, [this].concat(Array.from(arguments))); };
213var installed;
214E.prototype_install = function(){
215    if (installed)
216        return;
217    installed = true;
218    for (var i in proto)
219    {
220        Object.defineProperty(Array.prototype, i,
221            {value: proto[i], configurable: true, enumerable: false,
222            writable: true});
223    }
224};
225E.prototype_uninstall = function(){
226    if (!installed)
227        return;
228    installed = false;
229    // XXX sergey: store orig proto, then load it back
230    for (var i in proto)
231        delete Array.prototype[i];
232};
233return E; }); }());

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.