Permalink
1365 lines (1193 sloc)
38.7 KB
| 'use strict'; | |
| /** | |
| * Array.prototype.findLastIndex() | |
| * version 0.0.0 | |
| * Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
| * Basic support No No No No No No | |
| * ------------------------------------------------------------------------------- | |
| */ | |
| if (!Array.prototype.findLastIndex) { | |
| Object.defineProperty(Array.prototype, "findLastIndex", | |
| { | |
| value: function (predicate, thisArg) { | |
| let idx = this.length - 1; | |
| while (idx >= 0) { | |
| const value = this[idx]; | |
| if (predicate.call(thisArg, value, idx, this)) { | |
| return idx; | |
| } | |
| idx--; | |
| } | |
| return -1; | |
| } | |
| , | |
| writable: true, | |
| enumerable: false, | |
| configurable: true | |
| }); | |
| } | |
| /** | |
| * Array.prototype.findLast() | |
| * version 0.0.0 | |
| * Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
| * Basic support No No No No No No | |
| * ------------------------------------------------------------------------------- | |
| */ | |
| if (!Array.prototype.findLast) { | |
| Object.defineProperty(Array.prototype, "findLast", | |
| { | |
| value: function (predicate, thisArg) { | |
| let idx = this.length - 1; | |
| while (idx >= 0) { | |
| const value = this[idx]; | |
| if (predicate.call(thisArg, value, idx, this)) { | |
| return value; | |
| } | |
| idx--; | |
| } | |
| return undefined; | |
| } | |
| , | |
| writable: true, | |
| enumerable: false, | |
| configurable: true | |
| }); | |
| } | |
| /** | |
| * Array.prototype.at() | |
| * version 0.0.0 | |
| * Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
| * Basic support No No No No No No | |
| * ------------------------------------------------------------------------------- | |
| */ | |
| if (!Array.prototype.at) { | |
| Object.defineProperty(Array.prototype, "at", | |
| { | |
| value: function (n) { | |
| // ToInteger() abstract op | |
| n = Math.trunc(n) || 0; | |
| // Allow negative indexing from the end | |
| if (n < 0) n += this.length; | |
| // OOB access is guaranteed to return undefined | |
| if (n < 0 || n >= this.length) return undefined; | |
| // Otherwise, this is just normal property access | |
| return this[n]; | |
| }, | |
| writable: true, | |
| enumerable: false, | |
| configurable: true | |
| }); | |
| } | |
| /** | |
| * Array.prototype.from() | |
| * version 0.0.0 | |
| * Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
| * Basic support 45 32 (No) (Yes) 9 (Yes) | |
| * ------------------------------------------------------------------------------- | |
| */ | |
| if (!Array.from) { | |
| Array.from = (function () { | |
| var toStr = Object.prototype.toString; | |
| var isCallable = function (fn) { | |
| return typeof fn === 'function' || toStr.call(fn) === '[object Function]'; | |
| }; | |
| var toInteger = function (value) { | |
| var number = Number(value); | |
| if (isNaN(number)) { | |
| return 0; | |
| } | |
| if (number === 0 || !isFinite(number)) { | |
| return number; | |
| } | |
| return (number > 0 ? 1 : -1) * Math.floor(Math.abs(number)); | |
| }; | |
| var maxSafeInteger = Math.pow(2, 53) - 1; | |
| var toLength = function (value) { | |
| var len = toInteger(value); | |
| return Math.min(Math.max(len, 0), maxSafeInteger); | |
| }; | |
| // The length property of the from method is 1. | |
| return function from(arrayLike /*, mapFn, thisArg */) { | |
| // 1. Let C be the this value. | |
| var C = this; | |
| // 2. Let items be ToObject(arrayLike). | |
| var items = Object(arrayLike); | |
| // 3. ReturnIfAbrupt(items). | |
| if (arrayLike == null) { | |
| throw new TypeError( | |
| 'Array.from requires an array-like object - not null or undefined' | |
| ); | |
| } | |
| // 4. If mapfn is undefined, then let mapping be false. | |
| var mapFn = arguments.length > 1 ? arguments[1] : void undefined; | |
| var T; | |
| if (typeof mapFn !== 'undefined') { | |
| // 5. else | |
| // 5. a If IsCallable(mapfn) is false, throw a TypeError exception. | |
| if (!isCallable(mapFn)) { | |
| throw new TypeError( | |
| 'Array.from: when provided, the second argument must be a function' | |
| ); | |
| } | |
| // 5. b. If thisArg was supplied, let T be thisArg; else let T be undefined. | |
| if (arguments.length > 2) { | |
| T = arguments[2]; | |
| } | |
| } | |
| // 10. Let lenValue be Get(items, "length"). | |
| // 11. Let len be ToLength(lenValue). | |
| var len = toLength(items.length); | |
| // 13. If IsConstructor(C) is true, then | |
| // 13. a. Let A be the result of calling the [[Construct]] internal method | |
| // of C with an argument list containing the single item len. | |
| // 14. a. Else, Let A be ArrayCreate(len). | |
| var A = isCallable(C) ? Object(new C(len)) : new Array(len); | |
| // 16. Let k be 0. | |
| var k = 0; | |
| // 17. Repeat, while k < len… (also steps a - h) | |
| var kValue; | |
| while (k < len) { | |
| kValue = items[k]; | |
| if (mapFn) { | |
| A[k] = | |
| typeof T === 'undefined' | |
| ? mapFn(kValue, k) | |
| : mapFn.call(T, kValue, k); | |
| } else { | |
| A[k] = kValue; | |
| } | |
| k += 1; | |
| } | |
| // 18. Let putStatus be Put(A, "length", len, true). | |
| A.length = len; | |
| // 20. Return A. | |
| return A; | |
| }; | |
| })(); | |
| } | |
| /** | |
| * Array.prototype.isArray() | |
| * version 0.0.0 | |
| * Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
| * Basic support 5 4 9 10.5 5 (Yes) | |
| * ------------------------------------------------------------------------------- | |
| */ | |
| if (!Array.isArray) { | |
| Array.isArray = function (arg) { | |
| return Object.prototype.toString.call(arg) === '[object Array]'; | |
| }; | |
| } | |
| /** | |
| * Array.prototype.of() | |
| * version 0.0.0 | |
| * Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
| * Basic support 45 25 (No) (No) 9 (Yes) | |
| * ------------------------------------------------------------------------------- | |
| */ | |
| if (!Array.of) { | |
| Array.of = function () { | |
| return Array.prototype.slice.call(arguments); | |
| }; | |
| } | |
| /** | |
| * Array.prototype.copyWithin() | |
| * version 0.0.0 | |
| * Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
| * Basic support 45 32 (No) 32 9 12 | |
| * ------------------------------------------------------------------------------- | |
| */ | |
| if (!Array.prototype.copyWithin) { | |
| Object.defineProperty(Array.prototype, 'copyWithin', { | |
| configurable: true, | |
| writable: true, | |
| value: function (target, start /*, end*/) { | |
| // Steps 1-2. | |
| if (this == null) { | |
| throw new TypeError('this is null or not defined'); | |
| } | |
| var O = Object(this); | |
| // Steps 3-5. | |
| var len = O.length >>> 0; | |
| // Steps 6-8. | |
| var relativeTarget = target >> 0; | |
| var to = | |
| relativeTarget < 0 | |
| ? Math.max(len + relativeTarget, 0) | |
| : Math.min(relativeTarget, len); | |
| // Steps 9-11. | |
| var relativeStart = start >> 0; | |
| var from = | |
| relativeStart < 0 | |
| ? Math.max(len + relativeStart, 0) | |
| : Math.min(relativeStart, len); | |
| // Steps 12-14. | |
| var end = arguments[2]; | |
| var relativeEnd = end === undefined ? len : end >> 0; | |
| var final = | |
| relativeEnd < 0 | |
| ? Math.max(len + relativeEnd, 0) | |
| : Math.min(relativeEnd, len); | |
| // Step 15. | |
| var count = Math.min(final - from, len - to); | |
| // Steps 16-17. | |
| var direction = 1; | |
| if (from < to && to < from + count) { | |
| direction = -1; | |
| from += count - 1; | |
| to += count - 1; | |
| } | |
| // Step 18. | |
| while (count > 0) { | |
| if (from in O) { | |
| O[to] = O[from]; | |
| } else { | |
| delete O[to]; | |
| } | |
| from += direction; | |
| to += direction; | |
| count--; | |
| } | |
| // Step 19. | |
| return O; | |
| }, | |
| }); | |
| } | |
| /** | |
| * Array.prototype.entries() | |
| * version 0.0.0 | |
| * Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
| * Basic support 38 28 (No) 25 7.1 ? | |
| * ------------------------------------------------------------------------------- | |
| */ | |
| if (!Array.prototype.entries) { | |
| Array.prototype.entries = function () { | |
| function Iterator() { } | |
| Iterator.prototype.next = function () { | |
| if (index > selfThis.length - 1) { | |
| done = true; | |
| } | |
| if (done) { | |
| return { value: undefined, done: true }; | |
| } | |
| return { value: [index, selfThis[index++]], done: false }; | |
| }; | |
| var selfThis = this; | |
| var index = 0; | |
| var done; | |
| return new Iterator(); | |
| }; | |
| } | |
| /** | |
| * Array.prototype.every() | |
| * version 0.0.0 | |
| * Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
| * Basic support (Yes) 1.5 9 (Yes) (Yes) ? | |
| * ------------------------------------------------------------------------------- | |
| */ | |
| if (!Array.prototype.every) { | |
| Object.defineProperty(Array.prototype, 'every', { | |
| configurable: true, | |
| writable: true, | |
| value: function (callbackfn, thisArg) { | |
| var T, k; | |
| if (this == null) { | |
| throw new TypeError('this is null or not defined'); | |
| } | |
| // 1. Let O be the result of calling ToObject passing the this | |
| // value as the argument. | |
| var O = Object(this); | |
| // 2. Let lenValue be the result of calling the Get internal method | |
| // of O with the argument "length". | |
| // 3. Let len be ToUint32(lenValue). | |
| var len = O.length >>> 0; | |
| // 4. If IsCallable(callbackfn) is false, throw a TypeError exception. | |
| if (typeof callbackfn !== 'function') { | |
| throw new TypeError(); | |
| } | |
| // 5. If thisArg was supplied, let T be thisArg; else let T be undefined. | |
| if (arguments.length > 1) { | |
| T = thisArg; | |
| } | |
| // 6. Let k be 0. | |
| k = 0; | |
| // 7. Repeat, while k < len | |
| while (k < len) { | |
| var kValue; | |
| // a. Let Pk be ToString(k). | |
| // This is implicit for LHS operands of the in operator | |
| // b. Let kPresent be the result of calling the HasProperty internal | |
| // method of O with argument Pk. | |
| // This step can be combined with c | |
| // c. If kPresent is true, then | |
| if (k in O) { | |
| // i. Let kValue be the result of calling the Get internal method | |
| // of O with argument Pk. | |
| kValue = O[k]; | |
| // ii. Let testResult be the result of calling the Call internal method | |
| // of callbackfn with T as the this value and argument list | |
| // containing kValue, k, and O. | |
| var testResult = callbackfn.call(T, kValue, k, O); | |
| // iii. If ToBoolean(testResult) is false, return false. | |
| if (!testResult) { | |
| return false; | |
| } | |
| } | |
| k++; | |
| } | |
| return true; | |
| }, | |
| }); | |
| } | |
| /** | |
| * Array.prototype.fill() | |
| * version 0.0.0 | |
| * Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
| * Basic support 45 31 (No) (No) 7.1 (Yes) | |
| * ------------------------------------------------------------------------------- | |
| */ | |
| if (!Array.prototype.fill) { | |
| Object.defineProperty(Array.prototype, 'fill', { | |
| configurable: true, | |
| writable: true, | |
| value: function (value) { | |
| // Steps 1-2. | |
| if (this == null) { | |
| throw new TypeError('this is null or not defined'); | |
| } | |
| var O = Object(this); | |
| // Steps 3-5. | |
| var len = O.length >>> 0; | |
| // Steps 6-7. | |
| var start = arguments[1]; | |
| var relativeStart = start >> 0; | |
| // Step 8. | |
| var k = | |
| relativeStart < 0 | |
| ? Math.max(len + relativeStart, 0) | |
| : Math.min(relativeStart, len); | |
| // Steps 9-10. | |
| var end = arguments[2]; | |
| var relativeEnd = end === undefined ? len : end >> 0; | |
| // Step 11. | |
| var final = | |
| relativeEnd < 0 | |
| ? Math.max(len + relativeEnd, 0) | |
| : Math.min(relativeEnd, len); | |
| // Step 12. | |
| while (k < final) { | |
| O[k] = value; | |
| k++; | |
| } | |
| // Step 13. | |
| return O; | |
| }, | |
| }); | |
| } | |
| /** | |
| * Array.prototype.filter() | |
| * version 0.0.0 | |
| * Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
| * Basic support (Yes) 1.5 9 (Yes) (Yes) ? | |
| * ------------------------------------------------------------------------------- | |
| */ | |
| if (!Array.prototype.filter) { | |
| Object.defineProperty(Array.prototype, 'filter', { | |
| configurable: true, | |
| writable: true, | |
| value: function (fun /*, thisArg*/) { | |
| if (this === void 0 || this === null) { | |
| throw new TypeError(); | |
| } | |
| var t = Object(this); | |
| var len = t.length >>> 0; | |
| if (typeof fun !== 'function') { | |
| throw new TypeError(); | |
| } | |
| var res = []; | |
| var thisArg = arguments.length >= 2 ? arguments[1] : void 0; | |
| for (var i = 0; i < len; i++) { | |
| if (i in t) { | |
| var val = t[i]; | |
| // NOTE: Technically this should Object.defineProperty at | |
| // the next index, as push can be affected by | |
| // properties on Object.prototype and Array.prototype. | |
| // But that method's new, and collisions should be | |
| // rare, so use the more-compatible alternative. | |
| if (fun.call(thisArg, val, i, t)) { | |
| res.push(val); | |
| } | |
| } | |
| } | |
| return res; | |
| }, | |
| }); | |
| } | |
| /** | |
| * Array.prototype.find() | |
| * version 0.0.0 | |
| * Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
| * Basic support 45 25 (No) 32 7.1 12 | |
| * ------------------------------------------------------------------------------- | |
| */ | |
| if (!Array.prototype.find) { | |
| Object.defineProperty(Array.prototype, 'find', { | |
| configurable: true, | |
| writable: true, | |
| value: function (predicate) { | |
| // 1. Let O be ? ToObject(this value). | |
| if (this == null) { | |
| throw new TypeError('"this" is null or not defined'); | |
| } | |
| var o = Object(this); | |
| // 2. Let len be ? ToLength(? Get(O, "length")). | |
| var len = o.length >>> 0; | |
| // 3. If IsCallable(predicate) is false, throw a TypeError exception. | |
| if (typeof predicate !== 'function') { | |
| throw new TypeError('predicate must be a function'); | |
| } | |
| // 4. If thisArg was supplied, let T be thisArg; else let T be undefined. | |
| var thisArg = arguments[1]; | |
| // 5. Let k be 0. | |
| var k = 0; | |
| // 6. Repeat, while k < len | |
| while (k < len) { | |
| // a. Let Pk be ! ToString(k). | |
| // b. Let kValue be ? Get(O, Pk). | |
| // c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)). | |
| // d. If testResult is true, return kValue. | |
| var kValue = o[k]; | |
| if (predicate.call(thisArg, kValue, k, o)) { | |
| return kValue; | |
| } | |
| // e. Increase k by 1. | |
| k++; | |
| } | |
| // 7. Return undefined. | |
| return undefined; | |
| }, | |
| }); | |
| } | |
| /** | |
| * Array.prototype.findIndex() | |
| * version 0.0.0 | |
| * Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
| * Basic support 45 25 (No) (Yes) 7.1 (Yes) | |
| * ------------------------------------------------------------------------------- | |
| */ | |
| if (!Array.prototype.findIndex) { | |
| Object.defineProperty(Array.prototype, 'findIndex', { | |
| configurable: true, | |
| writable: true, | |
| value: function (predicate) { | |
| // 1. Let O be ? ToObject(this value). | |
| if (this == null) { | |
| throw new TypeError('"this" is null or not defined'); | |
| } | |
| var o = Object(this); | |
| // 2. Let len be ? ToLength(? Get(O, "length")). | |
| var len = o.length >>> 0; | |
| // 3. If IsCallable(predicate) is false, throw a TypeError exception. | |
| if (typeof predicate !== 'function') { | |
| throw new TypeError('predicate must be a function'); | |
| } | |
| // 4. If thisArg was supplied, let T be thisArg; else let T be undefined. | |
| var thisArg = arguments[1]; | |
| // 5. Let k be 0. | |
| var k = 0; | |
| // 6. Repeat, while k < len | |
| while (k < len) { | |
| // a. Let Pk be ! ToString(k). | |
| // b. Let kValue be ? Get(O, Pk). | |
| // c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)). | |
| // d. If testResult is true, return k. | |
| var kValue = o[k]; | |
| if (predicate.call(thisArg, kValue, k, o)) { | |
| return k; | |
| } | |
| // e. Increase k by 1. | |
| k++; | |
| } | |
| // 7. Return -1. | |
| return -1; | |
| }, | |
| }); | |
| } | |
| /** | |
| * Array.prototype.flat() | |
| * version 0.0.0 | |
| * Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
| * Basic support 69 62 (No) 56 12 (No) | |
| * ------------------------------------------------------------------------------- | |
| */ | |
| if (!Array.prototype.flat) { | |
| Object.defineProperty(Array.prototype, 'flat', { | |
| configurable: true, | |
| writable: true, | |
| value: function () { | |
| var depth = | |
| typeof arguments[0] === 'undefined' ? 1 : Number(arguments[0]) || 0; | |
| var result = []; | |
| var forEach = result.forEach; | |
| var flatDeep = function (arr, depth) { | |
| forEach.call(arr, function (val) { | |
| if (depth > 0 && Array.isArray(val)) { | |
| flatDeep(val, depth - 1); | |
| } else { | |
| result.push(val); | |
| } | |
| }); | |
| }; | |
| flatDeep(this, depth); | |
| return result; | |
| }, | |
| }); | |
| } | |
| /** | |
| * Array.prototype.flatMap() | |
| * version 0.0.0 | |
| * Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
| * Basic support 69 62 (No) 56 12 (No) | |
| * ------------------------------------------------------------------------------- | |
| */ | |
| if (!Array.prototype.flatMap) { | |
| Object.defineProperty(Array.prototype, 'flatMap', { | |
| configurable: true, | |
| writable: true, | |
| value: function () { | |
| return Array.prototype.map.apply(this, arguments).flat(1); | |
| }, | |
| }); | |
| } | |
| /** | |
| * Array.prototype.forEach() | |
| * version 0.0.0 | |
| * Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
| * Basic support (Yes) 1.5 9 (Yes) (Yes) ? | |
| * ------------------------------------------------------------------------------- | |
| */ | |
| if (!Array.prototype.forEach) { | |
| Object.defineProperty(Array.prototype, 'forEach', { | |
| configurable: true, | |
| writable: true, | |
| value: function (callback /*, thisArg*/) { | |
| var T, k; | |
| if (this == null) { | |
| throw new TypeError('this is null or not defined'); | |
| } | |
| // 1. Let O be the result of calling toObject() passing the | |
| // |this| value as the argument. | |
| var O = Object(this); | |
| // 2. Let lenValue be the result of calling the Get() internal | |
| // method of O with the argument "length". | |
| // 3. Let len be toUint32(lenValue). | |
| var len = O.length >>> 0; | |
| // 4. If isCallable(callback) is false, throw a TypeError exception. | |
| // See: http://es5.github.com/#x9.11 | |
| if (typeof callback !== 'function') { | |
| throw new TypeError(callback + ' is not a function'); | |
| } | |
| // 5. If thisArg was supplied, let T be thisArg; else let | |
| // T be undefined. | |
| if (arguments.length > 1) { | |
| T = arguments[1]; | |
| } | |
| // 6. Let k be 0 | |
| k = 0; | |
| // 7. Repeat, while k < len | |
| while (k < len) { | |
| var kValue; | |
| // a. Let Pk be ToString(k). | |
| // This is implicit for LHS operands of the in operator | |
| // b. Let kPresent be the result of calling the HasProperty | |
| // internal method of O with argument Pk. | |
| // This step can be combined with c | |
| // c. If kPresent is true, then | |
| if (k in O) { | |
| // i. Let kValue be the result of calling the Get internal | |
| // method of O with argument Pk. | |
| kValue = O[k]; | |
| // ii. Call the Call internal method of callback with T as | |
| // the this value and argument list containing kValue, k, and O. | |
| callback.call(T, kValue, k, O); | |
| } | |
| // d. Increase k by 1. | |
| k++; | |
| } | |
| // 8. return undefined | |
| }, | |
| }); | |
| } | |
| /** | |
| * Array.prototype.includes() | |
| * version 0.0.0 | |
| * Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
| * Basic support 47 43 (No) 34 9 14 | |
| * ------------------------------------------------------------------------------- | |
| */ | |
| if (!Array.prototype.includes) { | |
| Object.defineProperty(Array.prototype, 'includes', { | |
| configurable: true, | |
| writable: true, | |
| value: function (searchElement, fromIndex) { | |
| // 1. Let O be ? ToObject(this value). | |
| if (this == null) { | |
| throw new TypeError('"this" is null or not defined'); | |
| } | |
| var o = Object(this); | |
| // 2. Let len be ? ToLength(? Get(O, "length")). | |
| var len = o.length >>> 0; | |
| // 3. If len is 0, return false. | |
| if (len === 0) { | |
| return false; | |
| } | |
| // 4. Let n be ? ToInteger(fromIndex). | |
| // (If fromIndex is undefined, this step produces the value 0.) | |
| var n = fromIndex | 0; | |
| // 5. If n ≥ 0, then | |
| // a. Let k be n. | |
| // 6. Else n < 0, | |
| // a. Let k be len + n. | |
| // b. If k < 0, let k be 0. | |
| var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0); | |
| function sameValueZero(x, y) { | |
| return ( | |
| x === y || | |
| (typeof x === 'number' && | |
| typeof y === 'number' && | |
| isNaN(x) && | |
| isNaN(y)) | |
| ); | |
| } | |
| // 7. Repeat, while k < len | |
| while (k < len) { | |
| // a. Let elementK be the result of ? Get(O, ! ToString(k)). | |
| // b. If SameValueZero(searchElement, elementK) is true, return true. | |
| // c. Increase k by 1. | |
| if (sameValueZero(o[k], searchElement)) { | |
| return true; | |
| } | |
| k++; | |
| } | |
| // 8. Return false | |
| return false; | |
| }, | |
| }); | |
| } | |
| /** | |
| * Array.prototype.indexOf() | |
| * version 0.0.0 | |
| * Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
| * Basic support (Yes) 1.5 9 (Yes) (Yes) ? | |
| * ------------------------------------------------------------------------------- | |
| */ | |
| if (!Array.prototype.indexOf) { | |
| Object.defineProperty(Array.prototype, 'indexOf', { | |
| configurable: true, | |
| writable: true, | |
| value: function (searchElement, fromIndex) { | |
| var k; | |
| // 1. Let o be the result of calling ToObject passing | |
| // the this value as the argument. | |
| if (this == null) { | |
| throw new TypeError('"this" is null or not defined'); | |
| } | |
| var o = Object(this); | |
| // 2. Let lenValue be the result of calling the Get | |
| // internal method of o with the argument "length". | |
| // 3. Let len be ToUint32(lenValue). | |
| var len = o.length >>> 0; | |
| // 4. If len is 0, return -1. | |
| if (len === 0) { | |
| return -1; | |
| } | |
| // 5. If argument fromIndex was passed let n be | |
| // ToInteger(fromIndex); else let n be 0. | |
| var n = fromIndex | 0; | |
| // 6. If n >= len, return -1. | |
| if (n >= len) { | |
| return -1; | |
| } | |
| // 7. If n >= 0, then Let k be n. | |
| // 8. Else, n<0, Let k be len - abs(n). | |
| // If k is less than 0, then let k be 0. | |
| k = Math.max(n >= 0 ? n : len - Math.abs(n), 0); | |
| // 9. Repeat, while k < len | |
| while (k < len) { | |
| // a. Let Pk be ToString(k). | |
| // This is implicit for LHS operands of the in operator | |
| // b. Let kPresent be the result of calling the | |
| // HasProperty internal method of o with argument Pk. | |
| // This step can be combined with c | |
| // c. If kPresent is true, then | |
| // i. Let elementK be the result of calling the Get | |
| // internal method of o with the argument ToString(k). | |
| // ii. Let same be the result of applying the | |
| // Strict Equality Comparison Algorithm to | |
| // searchElement and elementK. | |
| // iii. If same is true, return k. | |
| if (k in o && o[k] === searchElement) { | |
| return k; | |
| } | |
| k++; | |
| } | |
| return -1; | |
| }, | |
| }); | |
| } | |
| /** | |
| * Array.prototype.join() | |
| * version 0.0.0 | |
| * Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
| * Basic support 1 1 5.5 (Yes) (Yes) ? | |
| * ------------------------------------------------------------------------------- | |
| */ | |
| /** | |
| * Array.prototype.keys() | |
| * version 0.0.0 | |
| * Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
| * Basic support 38 28 (No) 25 7.1 (Yes) | |
| * ------------------------------------------------------------------------------- | |
| */ | |
| if (!Array.prototype.keys) { | |
| Array.prototype.keys = function () { | |
| function Iterator() { } | |
| Iterator.prototype.next = function () { | |
| if (index > selfThis.length - 1) { | |
| done = true; | |
| } | |
| if (done) { | |
| return { value: undefined, done: true }; | |
| } | |
| return { value: index++, done: false }; | |
| }; | |
| var selfThis = this; | |
| var index = 0; | |
| var done; | |
| return new Iterator(); | |
| }; | |
| } | |
| /** | |
| * Array.prototype.lastIndexOf() | |
| * version 0.0.0 | |
| * Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
| * Basic support (Yes) (Yes) 9 (Yes) (Yes) ? | |
| * ------------------------------------------------------------------------------- | |
| */ | |
| if (!Array.prototype.lastIndexOf) { | |
| Object.defineProperty(Array.prototype, 'lastIndexOf', { | |
| configurable: true, | |
| writable: true, | |
| value: function (searchElement /*, fromIndex*/) { | |
| if (this === void 0 || this === null) { | |
| throw new TypeError(); | |
| } | |
| var n, | |
| k, | |
| t = Object(this), | |
| len = t.length >>> 0; | |
| if (len === 0) { | |
| return -1; | |
| } | |
| n = len - 1; | |
| if (arguments.length > 1) { | |
| n = Number(arguments[1]); | |
| if (n != n) { | |
| n = 0; | |
| } else if (n != 0 && n != 1 / 0 && n != -(1 / 0)) { | |
| n = (n > 0 || -1) * Math.floor(Math.abs(n)); | |
| } | |
| } | |
| for (k = n >= 0 ? Math.min(n, len - 1) : len - Math.abs(n); k >= 0; k--) { | |
| if (k in t && t[k] === searchElement) { | |
| return k; | |
| } | |
| } | |
| return -1; | |
| }, | |
| }); | |
| } | |
| /** | |
| * Array.prototype.map() | |
| * version 0.0.0 | |
| * Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
| * Basic support (Yes) 1.5 9 (Yes) (Yes) ? | |
| * ------------------------------------------------------------------------------- | |
| */ | |
| if (!Array.prototype.map) { | |
| Object.defineProperty(Array.prototype, 'map', { | |
| configurable: true, | |
| writable: true, | |
| value: function (callback /*, thisArg*/) { | |
| var T, A, k; | |
| if (this == null) { | |
| throw new TypeError('this is null or not defined'); | |
| } | |
| // 1. Let O be the result of calling ToObject passing the |this| | |
| // value as the argument. | |
| var O = Object(this); | |
| // 2. Let lenValue be the result of calling the Get internal | |
| // method of O with the argument "length". | |
| // 3. Let len be ToUint32(lenValue). | |
| var len = O.length >>> 0; | |
| // 4. If IsCallable(callback) is false, throw a TypeError exception. | |
| // See: http://es5.github.com/#x9.11 | |
| if (typeof callback !== 'function') { | |
| throw new TypeError(callback + ' is not a function'); | |
| } | |
| // 5. If thisArg was supplied, let T be thisArg; else let T be undefined. | |
| if (arguments.length > 1) { | |
| T = arguments[1]; | |
| } | |
| // 6. Let A be a new array created as if by the expression new Array(len) | |
| // where Array is the standard built-in constructor with that name and | |
| // len is the value of len. | |
| A = new Array(len); | |
| // 7. Let k be 0 | |
| k = 0; | |
| // 8. Repeat, while k < len | |
| while (k < len) { | |
| var kValue, mappedValue; | |
| // a. Let Pk be ToString(k). | |
| // This is implicit for LHS operands of the in operator | |
| // b. Let kPresent be the result of calling the HasProperty internal | |
| // method of O with argument Pk. | |
| // This step can be combined with c | |
| // c. If kPresent is true, then | |
| if (k in O) { | |
| // i. Let kValue be the result of calling the Get internal | |
| // method of O with argument Pk. | |
| kValue = O[k]; | |
| // ii. Let mappedValue be the result of calling the Call internal | |
| // method of callback with T as the this value and argument | |
| // list containing kValue, k, and O. | |
| mappedValue = callback.call(T, kValue, k, O); | |
| // iii. Call the DefineOwnProperty internal method of A with arguments | |
| // Pk, Property Descriptor | |
| // { Value: mappedValue, | |
| // Writable: true, | |
| // Enumerable: true, | |
| // Configurable: true }, | |
| // and false. | |
| // In browsers that support Object.defineProperty, use the following: | |
| // Object.defineProperty(A, k, { | |
| // value: mappedValue, | |
| // writable: true, | |
| // enumerable: true, | |
| // configurable: true | |
| // }); | |
| // For best browser support, use the following: | |
| A[k] = mappedValue; | |
| } | |
| // d. Increase k by 1. | |
| k++; | |
| } | |
| // 9. return A | |
| return A; | |
| }, | |
| }); | |
| } | |
| /** | |
| * Array.prototype.pop() | |
| * version 0.0.0 | |
| * Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
| * Basic support 1 1 5.5 (Yes) (Yes) ? | |
| * ------------------------------------------------------------------------------- | |
| */ | |
| /** | |
| * Array.prototype.push() | |
| * version 0.0.0 | |
| * Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
| * Basic support 1 1 5.5 (Yes) (Yes) ? | |
| * ------------------------------------------------------------------------------- | |
| */ | |
| /** | |
| * Array.prototype.reduce() | |
| * version 0.0.0 | |
| * Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
| * Basic support (Yes) 3 9 10.5 4 ? | |
| * ------------------------------------------------------------------------------- | |
| */ | |
| if (!Array.prototype.reduce) { | |
| Object.defineProperty(Array.prototype, 'reduce', { | |
| configurable: true, | |
| writable: true, | |
| value: function (callback /*, initialValue*/) { | |
| if (this === null) { | |
| throw new TypeError( | |
| 'Array.prototype.reduce ' + 'called on null or undefined' | |
| ); | |
| } | |
| if (typeof callback !== 'function') { | |
| throw new TypeError(callback + ' is not a function'); | |
| } | |
| // 1. Let O be ? ToObject(this value). | |
| var o = Object(this); | |
| // 2. Let len be ? ToLength(? Get(O, "length")). | |
| var len = o.length >>> 0; | |
| // Steps 3, 4, 5, 6, 7 | |
| var k = 0; | |
| var value; | |
| if (arguments.length >= 2) { | |
| value = arguments[1]; | |
| } else { | |
| while (k < len && !(k in o)) { | |
| k++; | |
| } | |
| // 3. If len is 0 and initialValue is not present, | |
| // throw a TypeError exception. | |
| if (k >= len) { | |
| throw new TypeError( | |
| 'Reduce of empty array ' + 'with no initial value' | |
| ); | |
| } | |
| value = o[k++]; | |
| } | |
| // 8. Repeat, while k < len | |
| while (k < len) { | |
| // a. Let Pk be ! ToString(k). | |
| // b. Let kPresent be ? HasProperty(O, Pk). | |
| // c. If kPresent is true, then | |
| // i. Let kValue be ? Get(O, Pk). | |
| // ii. Let accumulator be ? Call( | |
| // callbackfn, undefined, | |
| // « accumulator, kValue, k, O »). | |
| if (k in o) { | |
| value = callback(value, o[k], k, o); | |
| } | |
| // d. Increase k by 1. | |
| k++; | |
| } | |
| // 9. Return accumulator. | |
| return value; | |
| }, | |
| }); | |
| } | |
| /** | |
| * Array.prototype.reduceRight() | |
| * version 0.0.0 | |
| * Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
| * Basic support (Yes) 3 9 10.5 4 ? | |
| * ------------------------------------------------------------------------------- | |
| */ | |
| if ('function' !== typeof Array.prototype.reduceRight) { | |
| Object.defineProperty(Array.prototype, 'reduceRight', { | |
| configurable: true, | |
| writable: true, | |
| value: function (callback /*, initialValue*/) { | |
| if (null === this || 'undefined' === typeof this) { | |
| throw new TypeError( | |
| 'Array.prototype.reduce called on null or undefined' | |
| ); | |
| } | |
| if ('function' !== typeof callback) { | |
| throw new TypeError(callback + ' is not a function'); | |
| } | |
| var t = Object(this), | |
| len = t.length >>> 0, | |
| k = len - 1, | |
| value; | |
| if (arguments.length >= 2) { | |
| value = arguments[1]; | |
| } else { | |
| while (k >= 0 && !(k in t)) { | |
| k--; | |
| } | |
| if (k < 0) { | |
| throw new TypeError('Reduce of empty array with no initial value'); | |
| } | |
| value = t[k--]; | |
| } | |
| for (; k >= 0; k--) { | |
| if (k in t) { | |
| value = callback(value, t[k], k, t); | |
| } | |
| } | |
| return value; | |
| }, | |
| }); | |
| } | |
| /** | |
| * Array.prototype.reverse() | |
| * version 0.0.0 | |
| * Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
| * Basic support 1 1 5.5 (Yes) (Yes) (Yes) | |
| * ------------------------------------------------------------------------------- | |
| */ | |
| /** | |
| * Array.prototype.shift() | |
| * version 0.0.0 | |
| * Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
| * Basic support 1 1 5.5 (Yes) (Yes) (Yes) | |
| * ------------------------------------------------------------------------------- | |
| */ | |
| /** | |
| * Array.prototype.slice() | |
| * version 0.0.0 | |
| * Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
| * Basic support 1 1 5.5 (Yes) (Yes) (Yes) | |
| * ------------------------------------------------------------------------------- | |
| */ | |
| /** | |
| * Array.prototype.some() | |
| * version 0.0.0 | |
| * Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
| * Basic support 1 1.5 9 (Yes) (Yes) ? | |
| * ------------------------------------------------------------------------------- | |
| */ | |
| if (!Array.prototype.some) { | |
| Object.defineProperty(Array.prototype, 'some', { | |
| configurable: true, | |
| writable: true, | |
| value: function (fun /*, thisArg*/) { | |
| if (this == null) { | |
| throw new TypeError('Array.prototype.some called on null or undefined'); | |
| } | |
| if (typeof fun !== 'function') { | |
| throw new TypeError(); | |
| } | |
| var t = Object(this); | |
| var len = t.length >>> 0; | |
| var thisArg = arguments.length >= 2 ? arguments[1] : void 0; | |
| for (var i = 0; i < len; i++) { | |
| if (i in t && fun.call(thisArg, t[i], i, t)) { | |
| return true; | |
| } | |
| } | |
| return false; | |
| }, | |
| }); | |
| } | |
| /** | |
| * Array.prototype.sort() | |
| * version 0.0.0 | |
| * Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
| * Basic support 1 1 5.5 (Yes) (Yes) (Yes) | |
| * ------------------------------------------------------------------------------- | |
| */ | |
| /** | |
| * Array.prototype.splice() | |
| * version 0.0.0 | |
| * Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
| * Basic support 1 1 5.5 (Yes) (Yes) (Yes) | |
| * ------------------------------------------------------------------------------- | |
| */ | |
| /** | |
| * Array.prototype.toLocaleString() | |
| * version 0.0.0 | |
| * Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
| * Basic support (Yes) (Yes) (Yes) (Yes) (Yes) (Yes) | |
| * ------------------------------------------------------------------------------- | |
| */ | |
| if (!Array.prototype.toLocaleString) { | |
| Object.defineProperty(Array.prototype, 'toLocaleString', { | |
| configurable: true, | |
| writable: true, | |
| value: function (locales, options) { | |
| // 1. Let O be ? ToObject(this value). | |
| if (this == null) { | |
| throw new TypeError('"this" is null or not defined'); | |
| } | |
| var a = Object(this); | |
| // 2. Let len be ? ToLength(? Get(A, "length")). | |
| var len = a.length >>> 0; | |
| // 3. Let separator be the String value for the | |
| // list-separator String appropriate for the | |
| // host environment's current locale (this is | |
| // derived in an implementation-defined way). | |
| // NOTE: In this case, we will use a comma | |
| var separator = ','; | |
| // 4. If len is zero, return the empty String. | |
| if (len === 0) { | |
| return ''; | |
| } | |
| // 5. Let firstElement be ? Get(A, "0"). | |
| var firstElement = a[0]; | |
| // 6. If firstElement is undefined or null, then | |
| // a.Let R be the empty String. | |
| // 7. Else, | |
| // a. Let R be ? | |
| // ToString(? | |
| // Invoke( | |
| // firstElement, | |
| // "toLocaleString", | |
| // « locales, options » | |
| // ) | |
| // ) | |
| var r = | |
| firstElement == null | |
| ? '' | |
| : firstElement.toLocaleString(locales, options); | |
| // 8. Let k be 1. | |
| var k = 1; | |
| // 9. Repeat, while k < len | |
| while (k < len) { | |
| // a. Let S be a String value produced by | |
| // concatenating R and separator. | |
| var s = r + separator; | |
| // b. Let nextElement be ? Get(A, ToString(k)). | |
| var nextElement = a[k]; | |
| // c. If nextElement is undefined or null, then | |
| // i. Let R be the empty String. | |
| // d. Else, | |
| // i. Let R be ? | |
| // ToString(? | |
| // Invoke( | |
| // nextElement, | |
| // "toLocaleString", | |
| // « locales, options » | |
| // ) | |
| // ) | |
| r = | |
| nextElement == null | |
| ? '' | |
| : nextElement.toLocaleString(locales, options); | |
| // e. Let R be a String value produced by | |
| // concatenating S and R. | |
| r = s + r; | |
| // f. Increase k by 1. | |
| k++; | |
| } | |
| // 10. Return R. | |
| return r; | |
| }, | |
| }); | |
| } | |
| /** | |
| * Array.prototype.toString() | |
| * version 0.0.0 | |
| * Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
| * Basic support (Yes) (Yes) (Yes) (Yes) (Yes) (Yes) | |
| * ------------------------------------------------------------------------------- | |
| */ | |
| /** | |
| * Array.prototype.unshift() | |
| * version 0.0.0 | |
| * Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
| * Basic support 1 1 5.5 (Yes) (Yes) ? | |
| * ------------------------------------------------------------------------------- | |
| */ | |
| /** | |
| * Array.prototype.values() | |
| * version 0.0.0 | |
| * Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
| * Basic support (No) (No) (No) (No) 9 (Yes) | |
| * ------------------------------------------------------------------------------- | |
| */ | |
| if (!Array.prototype.values) { | |
| Array.prototype.values = function () { | |
| function Iterator() { } | |
| Iterator.prototype.next = function () { | |
| if (index > selfThis.length - 1) { | |
| done = true; | |
| } | |
| if (done) { | |
| return { value: undefined, done: true }; | |
| } | |
| return { value: selfThis[index++], done: false }; | |
| }; | |
| var selfThis = this; | |
| var index = 0; | |
| var done; | |
| return new Iterator(); | |
| }; | |
| } |