array.js
79 lines
| 2.2 KiB
| application/javascript
|
JavascriptLexer
r3363 | // # Copyright (C) 2010-2019 RhodeCode GmbH | ||
r1 | // # | ||
// # This program is free software: you can redistribute it and/or modify | |||
// # it under the terms of the GNU Affero General Public License, version 3 | |||
// # (only), as published by the Free Software Foundation. | |||
// # | |||
// # This program is distributed in the hope that it will be useful, | |||
// # but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
// # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
// # GNU General Public License for more details. | |||
// # | |||
// # You should have received a copy of the GNU Affero General Public License | |||
// # along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
// # | |||
// # This program is dual-licensed. If you wish to learn more about the | |||
// # RhodeCode Enterprise Edition, including its added features, Support services, | |||
// # and proprietary license terms, please see https://rhodecode.com/licenses/ | |||
if (!Array.prototype.indexOf) { | |||
Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) { | |||
"use strict"; | |||
if (this === null) { | |||
throw new TypeError(); | |||
} | |||
var t = Object(this); | |||
var len = t.length >>> 0; | |||
if (len === 0) { | |||
return -1; | |||
} | |||
var n = 0; | |||
if (arguments.length > 0) { | |||
n = Number(arguments[1]); | |||
if (n !== n) { // shortcut for verifying if it's NaN | |||
n = 0; | |||
} else if (n !== 0 && n !== Infinity && n !== -Infinity) { | |||
n = (n > 0 || -1) * Math.floor(Math.abs(n)); | |||
} | |||
} | |||
if (n >= len) { | |||
return -1; | |||
} | |||
var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0); | |||
for (; k < len; k++) { | |||
if (k in t && t[k] === searchElement) { | |||
return k; | |||
} | |||
} | |||
return -1; | |||
}; | |||
} | |||
if (!Array.prototype.filter) { | |||
Array.prototype.filter = function(fun /*, thisp*/) { | |||
'use strict'; | |||
if (this === null) { | |||
throw new TypeError(); | |||
} | |||
var t = Object(this), | |||
len = t.length >>> 0, | |||
res, thisp, i, val; | |||
if (typeof fun !== 'function') { | |||
throw new TypeError(); | |||
} | |||
res = []; | |||
thisp = arguments[1]; | |||
for (i = 0; i < len; i++) { | |||
if (i in t) { | |||
val = t[i]; // in case fun mutates this | |||
if (fun.call(thisp, val, i, t)) { | |||
res.push(val); | |||
} | |||
} | |||
} | |||
return res; | |||
}; | |||
} |