|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const ES = require('es-abstract'); |
| 4 | + |
| 5 | +if (typeof Symbol !== 'function' || !Symbol.isConcatSpreadable) { |
| 6 | + throw new Error('Symbol.isConcatSpreadable missing from the environment'); |
| 7 | +} |
| 8 | + |
| 9 | +if (typeof Array.prototype.flatMap !== 'function') { |
| 10 | + // eslint-disable-next-line no-extend-native |
| 11 | + Object.defineProperty(Array.prototype, 'flatMap', { |
| 12 | + configurable: true, |
| 13 | + writable: true, |
| 14 | + // https://tc39.github.io/proposal-flatMap/#sec-Array.prototype.flatMap |
| 15 | + value: function flatMap(callback, thisArg = undefined) { |
| 16 | + const O = ES.ToObject(this); |
| 17 | + // Could ES.IsCallable check callback, but it's better to let it throw a runtime error |
| 18 | + const sourceLen = ES.ToLength(O.length); |
| 19 | + const A = new (ES.SpeciesConstructor(O, Array))(0); |
| 20 | + flattenIntoArray(A, O, sourceLen, 0, 1, callback, thisArg); |
| 21 | + return A; |
| 22 | + }, |
| 23 | + }); |
| 24 | +} |
| 25 | + |
| 26 | +if (typeof Array.prototype.flatten !== 'function') { |
| 27 | + // eslint-disable-next-line no-extend-native |
| 28 | + Object.defineProperty(Array.prototype, 'flatten', { |
| 29 | + configurable: true, |
| 30 | + writable: true, |
| 31 | + // https://tc39.github.io/proposal-flatMap/#sec-Array.prototype.flatten |
| 32 | + value: function flatten(depthArg = 1) { |
| 33 | + const O = ES.ToObject(this); |
| 34 | + const sourceLen = ES.ToLength(O.length); |
| 35 | + const A = new (ES.SpeciesConstructor(O, Array))(0); |
| 36 | + const depth = ES.ToInteger(depthArg); |
| 37 | + flattenIntoArray(A, O, sourceLen, 0, depth); |
| 38 | + return A; |
| 39 | + }, |
| 40 | + }); |
| 41 | +} |
| 42 | + |
| 43 | +// https://tc39.github.io/proposal-flatMap/#sec-FlattenIntoArray |
| 44 | +/** |
| 45 | + * @param {any[]} target |
| 46 | + * @param {any[]} source |
| 47 | + * @param {number} startLen |
| 48 | + * @param {number} start |
| 49 | + * @param {number} depth |
| 50 | + * @param {Function=} mapper |
| 51 | + * @param {any=} thisArg |
| 52 | + */ |
| 53 | +function flattenIntoArray(target, source, sourceLen, start, depth, mapper, thisArg) { |
| 54 | + let targetIndex = start; |
| 55 | + let sourceIndex = 0; |
| 56 | + |
| 57 | + while (sourceIndex < sourceLen) { |
| 58 | + const P = ES.ToString(sourceIndex); |
| 59 | + if (P in source) { |
| 60 | + let element = source[P]; |
| 61 | + if (mapper) { |
| 62 | + element = mapper.call(thisArg, element, sourceIndex, target); |
| 63 | + } |
| 64 | + let spreadable; |
| 65 | + // https://tc39.github.io/ecma262/#sec-isconcatspreadable |
| 66 | + if (typeof element !== 'object') { |
| 67 | + spreadable = false; |
| 68 | + } else { |
| 69 | + spreadable = element[Symbol.isConcatSpreadable]; |
| 70 | + if (typeof spreadable !== 'undefined') { |
| 71 | + spreadable = !!spreadable; |
| 72 | + } else { |
| 73 | + spreadable = Array.isArray(element); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + if (spreadable && depth > 0) { |
| 78 | + const elementLen = ES.ToLength(element.length); |
| 79 | + const nextIndex = flattenIntoArray(target, element, elementLen, targetIndex, depth - 1); |
| 80 | + targetIndex = nextIndex - 1; |
| 81 | + } else { |
| 82 | + if (targetIndex !== ES.ToLength(targetIndex)) { |
| 83 | + throw TypeError(); |
| 84 | + } |
| 85 | + // eslint-disable-next-line no-param-reassign |
| 86 | + target[targetIndex] = element; |
| 87 | + } |
| 88 | + } |
| 89 | + targetIndex += 1; |
| 90 | + sourceIndex += 1; |
| 91 | + } |
| 92 | + return targetIndex; |
| 93 | +} |
0 commit comments