From b4ff1add39f7ec6ee2a96fa98ed0dbc7872c5d48 Mon Sep 17 00:00:00 2001 From: Denis Pushkarev Date: Sat, 26 May 2018 01:04:47 +0700 Subject: [PATCH] rename `Array.prototype.flatten` to `Array.prototype.flat`, fixes #405 https://github.com/tc39/proposal-flatMap/commit/093eacc7fe0906e70f7626bf 6c7d6e9dfc53cce9 --- README.md | 30 ++++++++--------- packages/core-js-builder/config.js | 2 +- .../features/array/{flatten.js => flat.js} | 4 +-- packages/core-js/features/array/index.js | 2 +- .../array/virtual/{flatten.js => flat.js} | 4 +-- .../core-js/features/array/virtual/index.js | 2 +- packages/core-js/index.js | 2 +- ....array.flatten.js => esnext.array.flat.js} | 8 ++--- packages/core-js/stage/3.js | 2 +- tests/commonjs.js | 4 +++ tests/pure/esnext.array.flat.js | 28 ++++++++++++++++ tests/pure/esnext.array.flatten.js | 28 ---------------- tests/pure/index.js | 2 +- tests/tests/esnext.array.flat.js | 32 +++++++++++++++++++ tests/tests/esnext.array.flatten.js | 32 ------------------- tests/tests/index.js | 2 +- 16 files changed, 94 insertions(+), 90 deletions(-) rename packages/core-js/features/array/{flatten.js => flat.js} (51%) rename packages/core-js/features/array/virtual/{flatten.js => flat.js} (50%) rename packages/core-js/modules/{esnext.array.flatten.js => esnext.array.flat.js} (82%) create mode 100644 tests/pure/esnext.array.flat.js delete mode 100644 tests/pure/esnext.array.flatten.js create mode 100644 tests/tests/esnext.array.flat.js delete mode 100644 tests/tests/esnext.array.flatten.js diff --git a/README.md b/README.md index dc9140c47a7f..6068e74fd82b 100644 --- a/README.md +++ b/README.md @@ -12,31 +12,31 @@ Modular standard library for JavaScript. Includes polyfills for [ECMAScript 5, 2 import 'core-js'; // <- at the top of your entry point Array.from(new Set([1, 2, 3, 2, 1])); // => [1, 2, 3] -[1, [2, 3], [4, [5]]].flatten(2); // => [1, 2, 3, 4, 5] +[1, [2, 3], [4, [5]]].flat(2); // => [1, 2, 3, 4, 5] Promise.resolve(32).then(x => console.log(x)); // => 32 ``` *You can load only required features*: ```js -import 'core-js/features/array/from'; // <- at the top of your entry point -import 'core-js/features/array/flatten'; // <- at the top of your entry point -import 'core-js/features/set'; // <- at the top of your entry point -import 'core-js/features/promise'; // <- at the top of your entry point +import 'core-js/features/array/from'; // <- at the top of your entry point +import 'core-js/features/array/flat'; // <- at the top of your entry point +import 'core-js/features/set'; // <- at the top of your entry point +import 'core-js/features/promise'; // <- at the top of your entry point Array.from(new Set([1, 2, 3, 2, 1])); // => [1, 2, 3] -[1, [2, 3], [4, [5]]].flatten(2); // => [1, 2, 3, 4, 5] +[1, [2, 3], [4, [5]]].flat(2); // => [1, 2, 3, 4, 5] Promise.resolve(32).then(x => console.log(x)); // => 32 ``` *Or use it without global namespace pollution*: ```js import from from 'core-js-pure/features/array/from'; -import flatten from 'core-js-pure/features/array/flatten'; +import flat from 'core-js-pure/features/array/flat'; import Set from 'core-js-pure/features/set'; import Promise from 'core-js-pure/features/promise'; from(new Set([1, 2, 3, 2, 1])); // => [1, 2, 3] -flatten([1, [2, 3], [4, [5]]], 2); // => [1, 2, 3, 4, 5] +flat([1, [2, 3], [4, [5]]], 2); // => [1, 2, 3, 4, 5] Promise.resolve(32).then(x => console.log(x)); // => 32 ``` @@ -1421,25 +1421,25 @@ None. ```js core-js(-pure)/stage/3 ``` -* `Array#flatten` and `Array#flatMap` [proposal](https://tc39.github.io/proposal-flatMap) - modules [`esnext.array.flatten`](https://github.com/zloirock/core-js/blob/v3/packages/core-js/modules/esnext.array.flatten.js) and [`esnext.array.flat-map`](https://github.com/zloirock/core-js/blob/v3/packages/core-js/modules/esnext.array.flat-map.js) +* `Array#flat` and `Array#flatMap` [proposal](https://tc39.github.io/proposal-flatMap) - modules [`esnext.array.flat`](https://github.com/zloirock/core-js/blob/v3/packages/core-js/modules/esnext.array.flat.js) and [`esnext.array.flat-map`](https://github.com/zloirock/core-js/blob/v3/packages/core-js/modules/esnext.array.flat-map.js) ```js class Array { - flatten(depthArg?: number = 1): Array; + flat(depthArg?: number = 1): Array; flatMap(mapFn: (value: any, index: number, target: any) => any, thisArg: any): Array; } ``` [*CommonJS entry points:*](#commonjs) ```js -core-js(-pure)/features/array/flatten +core-js(-pure)/features/array/flat core-js(-pure)/features/array/flat-map -core-js(-pure)/features/array/virtual/flatten +core-js(-pure)/features/array/virtual/flat core-js(-pure)/features/array/virtual/flat-map ``` [*Examples*](https://goo.gl/jTXsZi): ```js -[1, [2, 3], [4, 5]].flatten(); // => [1, 2, 3, 4, 5] -[1, [2, [3, [4]]], 5].flatten(); // => [1, 2, [3, [4]], 5] -[1, [2, [3, [4]]], 5].flatten(3); // => [1, 2, 3, 4, 5] +[1, [2, 3], [4, 5]].flat(); // => [1, 2, 3, 4, 5] +[1, [2, [3, [4]]], 5].flat(); // => [1, 2, [3, [4]], 5] +[1, [2, [3, [4]]], 5].flat(3); // => [1, 2, 3, 4, 5] [{ a: 1, b: 2 }, { a: 3, b: 4 }, { a: 5, b: 6 }].flatMap(it => [it.a, it.b]); // => [1, 2, 3, 4, 5, 6] ``` diff --git a/packages/core-js-builder/config.js b/packages/core-js-builder/config.js index 15bfd0e915c8..043af871210b 100644 --- a/packages/core-js-builder/config.js +++ b/packages/core-js-builder/config.js @@ -191,7 +191,7 @@ module.exports = { 'es.reflect.prevent-extensions', 'es.reflect.set', 'es.reflect.set-prototype-of', - 'esnext.array.flatten', + 'esnext.array.flat', 'esnext.array.flat-map', 'esnext.array.last-index', 'esnext.array.last-item', diff --git a/packages/core-js/features/array/flatten.js b/packages/core-js/features/array/flat.js similarity index 51% rename from packages/core-js/features/array/flatten.js rename to packages/core-js/features/array/flat.js index 40d9f7d7c51e..3e02b34a2fa9 100644 --- a/packages/core-js/features/array/flatten.js +++ b/packages/core-js/features/array/flat.js @@ -1,3 +1,3 @@ -require('../../modules/esnext.array.flatten'); +require('../../modules/esnext.array.flat'); -module.exports = require('../../internals/entry-unbind')('Array', 'flatten'); +module.exports = require('../../internals/entry-unbind')('Array', 'flat'); diff --git a/packages/core-js/features/array/index.js b/packages/core-js/features/array/index.js index 0abbdca8a41d..c14033bfe90b 100644 --- a/packages/core-js/features/array/index.js +++ b/packages/core-js/features/array/index.js @@ -26,6 +26,6 @@ require('../../modules/es.array.includes'); require('../../modules/esnext.array.last-item'); require('../../modules/esnext.array.last-index'); require('../../modules/esnext.array.flat-map'); -require('../../modules/esnext.array.flatten'); +require('../../modules/esnext.array.flat'); module.exports = require('../../internals/path').Array; diff --git a/packages/core-js/features/array/virtual/flatten.js b/packages/core-js/features/array/virtual/flat.js similarity index 50% rename from packages/core-js/features/array/virtual/flatten.js rename to packages/core-js/features/array/virtual/flat.js index 307f6808b727..fc531aa6d6fd 100644 --- a/packages/core-js/features/array/virtual/flatten.js +++ b/packages/core-js/features/array/virtual/flat.js @@ -1,3 +1,3 @@ -require('../../../modules/esnext.array.flatten'); +require('../../../modules/esnext.array.flat'); -module.exports = require('../../../internals/entry-virtual')('Array').flatten; +module.exports = require('../../../internals/entry-virtual')('Array').flat; diff --git a/packages/core-js/features/array/virtual/index.js b/packages/core-js/features/array/virtual/index.js index 3e8f6743dd06..9e95d5d22177 100644 --- a/packages/core-js/features/array/virtual/index.js +++ b/packages/core-js/features/array/virtual/index.js @@ -19,7 +19,7 @@ require('../../../modules/es.array.sort'); require('../../../modules/es.array.splice'); require('../../../modules/es.array.species'); require('../../../modules/es.array.iterator'); -require('../../../modules/esnext.array.flatten'); +require('../../../modules/esnext.array.flat'); require('../../../modules/esnext.array.flat-map'); module.exports = require('../../../internals/entry-virtual')('Array'); diff --git a/packages/core-js/index.js b/packages/core-js/index.js index e2eb438439b7..41bd6e824a9c 100644 --- a/packages/core-js/index.js +++ b/packages/core-js/index.js @@ -188,7 +188,7 @@ require('./modules/es.reflect.own-keys'); require('./modules/es.reflect.prevent-extensions'); require('./modules/es.reflect.set'); require('./modules/es.reflect.set-prototype-of'); -require('./modules/esnext.array.flatten'); +require('./modules/esnext.array.flat'); require('./modules/esnext.array.flat-map'); require('./modules/esnext.array.last-index'); require('./modules/esnext.array.last-item'); diff --git a/packages/core-js/modules/esnext.array.flatten.js b/packages/core-js/modules/esnext.array.flat.js similarity index 82% rename from packages/core-js/modules/esnext.array.flatten.js rename to packages/core-js/modules/esnext.array.flat.js index 90457afefb8f..05e797547f0b 100644 --- a/packages/core-js/modules/esnext.array.flatten.js +++ b/packages/core-js/modules/esnext.array.flat.js @@ -5,10 +5,10 @@ var toLength = require('../internals/to-length'); var toInteger = require('../internals/to-integer'); var arraySpeciesCreate = require('../internals/array-species-create'); -// `Array.prototype.flatten` method -// https://tc39.github.io/proposal-flatMap/#sec-Array.prototype.flatten +// `Array.prototype.flat` method +// https://tc39.github.io/proposal-flatMap/#sec-Array.prototype.flat require('../internals/export')({ target: 'Array', proto: true }, { - flatten: function flatten(/* depthArg = 1 */) { + flat: function flat(/* depthArg = 1 */) { var depthArg = arguments[0]; var O = toObject(this); var sourceLen = toLength(O.length); @@ -18,4 +18,4 @@ require('../internals/export')({ target: 'Array', proto: true }, { } }); -require('../internals/add-to-unscopables')('flatten'); +require('../internals/add-to-unscopables')('flat'); diff --git a/packages/core-js/stage/3.js b/packages/core-js/stage/3.js index 72559a3660ba..32f3570851fd 100644 --- a/packages/core-js/stage/3.js +++ b/packages/core-js/stage/3.js @@ -1,4 +1,4 @@ -require('../modules/esnext.array.flatten'); +require('../modules/esnext.array.flat'); require('../modules/esnext.array.flat-map'); require('../modules/esnext.global'); require('../modules/esnext.string.match-all'); diff --git a/tests/commonjs.js b/tests/commonjs.js index b98c96814022..f01dea92870b 100644 --- a/tests/commonjs.js +++ b/tests/commonjs.js @@ -61,6 +61,8 @@ for (const _PATH of ['../packages/core-js-pure', '../packages/core-js']) { ok(typeof load('features/array/for-each') === 'function'); ok(typeof load('features/array/map') === 'function'); ok(typeof load('features/array/filter') === 'function'); + ok(typeof load('features/array/flat') === 'function'); + ok(typeof load('features/array/flat-map') === 'function'); ok(typeof load('features/array/some') === 'function'); ok(typeof load('features/array/every') === 'function'); ok(typeof load('features/array/reduce') === 'function'); @@ -86,6 +88,8 @@ for (const _PATH of ['../packages/core-js-pure', '../packages/core-js']) { ok(typeof load('features/array/virtual/for-each') === 'function'); ok(typeof load('features/array/virtual/map') === 'function'); ok(typeof load('features/array/virtual/filter') === 'function'); + ok(typeof load('features/array/virtual/flat') === 'function'); + ok(typeof load('features/array/virtual/flat-map') === 'function'); ok(typeof load('features/array/virtual/some') === 'function'); ok(typeof load('features/array/virtual/every') === 'function'); ok(typeof load('features/array/virtual/reduce') === 'function'); diff --git a/tests/pure/esnext.array.flat.js b/tests/pure/esnext.array.flat.js new file mode 100644 index 000000000000..0f510e4bf941 --- /dev/null +++ b/tests/pure/esnext.array.flat.js @@ -0,0 +1,28 @@ +import { DESCRIPTORS, STRICT } from '../helpers/constants'; + +import flat from 'core-js-pure/features/array/flat'; +import defineProperty from 'core-js-pure/features/object/define-property'; + +QUnit.test('Array#flat', assert => { + assert.isFunction(flat); + assert.deepEqual(flat([]), []); + const array = [1, [2, 3], [4, [5, 6]]]; + assert.deepEqual(flat(array, 0), array); + assert.deepEqual(flat(array, 1), [1, 2, 3, 4, [5, 6]]); + assert.deepEqual(flat(array), [1, 2, 3, 4, [5, 6]]); + assert.deepEqual(flat(array, 2), [1, 2, 3, 4, 5, 6]); + assert.deepEqual(flat(array, 3), [1, 2, 3, 4, 5, 6]); + assert.deepEqual(flat(array, -1), array); + assert.deepEqual(flat(array, Infinity), [1, 2, 3, 4, 5, 6]); + if (STRICT) { + assert.throws(() => flat(null), TypeError); + assert.throws(() => flat(undefined), TypeError); + } + if (DESCRIPTORS) { + assert.notThrows(() => flat(defineProperty({ length: -1 }, 0, { + get() { + throw new Error(); + }, + })).length === 0, 'uses ToLength'); + } +}); diff --git a/tests/pure/esnext.array.flatten.js b/tests/pure/esnext.array.flatten.js deleted file mode 100644 index fd7175720a64..000000000000 --- a/tests/pure/esnext.array.flatten.js +++ /dev/null @@ -1,28 +0,0 @@ -import { DESCRIPTORS, STRICT } from '../helpers/constants'; - -import flatten from 'core-js-pure/features/array/flatten'; -import defineProperty from 'core-js-pure/features/object/define-property'; - -QUnit.test('Array#flatten', assert => { - assert.isFunction(flatten); - assert.deepEqual(flatten([]), []); - const array = [1, [2, 3], [4, [5, 6]]]; - assert.deepEqual(flatten(array, 0), array); - assert.deepEqual(flatten(array, 1), [1, 2, 3, 4, [5, 6]]); - assert.deepEqual(flatten(array), [1, 2, 3, 4, [5, 6]]); - assert.deepEqual(flatten(array, 2), [1, 2, 3, 4, 5, 6]); - assert.deepEqual(flatten(array, 3), [1, 2, 3, 4, 5, 6]); - assert.deepEqual(flatten(array, -1), array); - assert.deepEqual(flatten(array, Infinity), [1, 2, 3, 4, 5, 6]); - if (STRICT) { - assert.throws(() => flatten(null), TypeError); - assert.throws(() => flatten(undefined), TypeError); - } - if (DESCRIPTORS) { - assert.notThrows(() => flatten(defineProperty({ length: -1 }, 0, { - get() { - throw new Error(); - }, - })).length === 0, 'uses ToLength'); - } -}); diff --git a/tests/pure/index.js b/tests/pure/index.js index 24da4c95fb5f..af672f3b9d1b 100644 --- a/tests/pure/index.js +++ b/tests/pure/index.js @@ -130,7 +130,7 @@ import './es.weak-set'; QUnit.module('ESNext'); import './esnext.array.flat-map'; -import './esnext.array.flatten'; +import './esnext.array.flat'; import './esnext.asap'; import './esnext.composite-key'; import './esnext.composite-symbol'; diff --git a/tests/tests/esnext.array.flat.js b/tests/tests/esnext.array.flat.js new file mode 100644 index 000000000000..06ebc93a22b2 --- /dev/null +++ b/tests/tests/esnext.array.flat.js @@ -0,0 +1,32 @@ +import { DESCRIPTORS, STRICT } from '../helpers/constants'; + +QUnit.test('Array#flat', assert => { + const { flat } = Array.prototype; + const { defineProperty } = Object; + assert.isFunction(flat); + assert.name(flat, 'flat'); + assert.arity(flat, 0); + assert.looksNative(flat); + assert.nonEnumerable(Array.prototype, 'flat'); + assert.deepEqual([].flat(), []); + const array = [1, [2, 3], [4, [5, 6]]]; + assert.deepEqual(array.flat(0), array); + assert.deepEqual(array.flat(1), [1, 2, 3, 4, [5, 6]]); + assert.deepEqual(array.flat(), [1, 2, 3, 4, [5, 6]]); + assert.deepEqual(array.flat(2), [1, 2, 3, 4, 5, 6]); + assert.deepEqual(array.flat(3), [1, 2, 3, 4, 5, 6]); + assert.deepEqual(array.flat(-1), array); + assert.deepEqual(array.flat(Infinity), [1, 2, 3, 4, 5, 6]); + if (STRICT) { + assert.throws(() => flat.call(null), TypeError); + assert.throws(() => flat.call(undefined), TypeError); + } + if (DESCRIPTORS) { + assert.notThrows(() => flat.call(defineProperty({ length: -1 }, 0, { + get() { + throw new Error(); + }, + })).length === 0, 'uses ToLength'); + } + assert.ok('flat' in Array.prototype[Symbol.unscopables], 'In Array#@@unscopables'); +}); diff --git a/tests/tests/esnext.array.flatten.js b/tests/tests/esnext.array.flatten.js deleted file mode 100644 index 5e733c7cb8bc..000000000000 --- a/tests/tests/esnext.array.flatten.js +++ /dev/null @@ -1,32 +0,0 @@ -import { DESCRIPTORS, STRICT } from '../helpers/constants'; - -QUnit.test('Array#flatten', assert => { - const { flatten } = Array.prototype; - const { defineProperty } = Object; - assert.isFunction(flatten); - assert.name(flatten, 'flatten'); - assert.arity(flatten, 0); - assert.looksNative(flatten); - assert.nonEnumerable(Array.prototype, 'flatten'); - assert.deepEqual([].flatten(), []); - const array = [1, [2, 3], [4, [5, 6]]]; - assert.deepEqual(array.flatten(0), array); - assert.deepEqual(array.flatten(1), [1, 2, 3, 4, [5, 6]]); - assert.deepEqual(array.flatten(), [1, 2, 3, 4, [5, 6]]); - assert.deepEqual(array.flatten(2), [1, 2, 3, 4, 5, 6]); - assert.deepEqual(array.flatten(3), [1, 2, 3, 4, 5, 6]); - assert.deepEqual(array.flatten(-1), array); - assert.deepEqual(array.flatten(Infinity), [1, 2, 3, 4, 5, 6]); - if (STRICT) { - assert.throws(() => flatten.call(null), TypeError); - assert.throws(() => flatten.call(undefined), TypeError); - } - if (DESCRIPTORS) { - assert.notThrows(() => flatten.call(defineProperty({ length: -1 }, 0, { - get() { - throw new Error(); - }, - })).length === 0, 'uses ToLength'); - } - assert.ok('flatten' in Array.prototype[Symbol.unscopables], 'In Array#@@unscopables'); -}); diff --git a/tests/tests/index.js b/tests/tests/index.js index 26fd92bd5943..becfc5342e92 100644 --- a/tests/tests/index.js +++ b/tests/tests/index.js @@ -180,7 +180,7 @@ QUnit.module('ESNext'); import './esnext.array.last-item'; import './esnext.array.last-index'; import './esnext.array.flat-map'; -import './esnext.array.flatten'; +import './esnext.array.flat'; import './esnext.asap'; import './esnext.composite-key'; import './esnext.composite-symbol';