Skip to content

Commit

Permalink
rename Array.prototype.flatten to Array.prototype.flat, fixes #405
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed May 25, 2018
1 parent f52b193 commit b4ff1ad
Show file tree
Hide file tree
Showing 16 changed files with 94 additions and 90 deletions.
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down Expand Up @@ -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<mixed>;
flat(depthArg?: number = 1): Array<mixed>;
flatMap(mapFn: (value: any, index: number, target: any) => any, thisArg: any): Array<mixed>;
}
```
[*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]
```
Expand Down
2 changes: 1 addition & 1 deletion packages/core-js-builder/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
@@ -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');
2 changes: 1 addition & 1 deletion packages/core-js/features/array/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 1 addition & 1 deletion packages/core-js/features/array/virtual/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
2 changes: 1 addition & 1 deletion packages/core-js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -18,4 +18,4 @@ require('../internals/export')({ target: 'Array', proto: true }, {
}
});

require('../internals/add-to-unscopables')('flatten');
require('../internals/add-to-unscopables')('flat');
2 changes: 1 addition & 1 deletion packages/core-js/stage/3.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down
4 changes: 4 additions & 0 deletions tests/commonjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
Expand Down
28 changes: 28 additions & 0 deletions tests/pure/esnext.array.flat.js
Original file line number Diff line number Diff line change
@@ -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');
}
});
28 changes: 0 additions & 28 deletions tests/pure/esnext.array.flatten.js

This file was deleted.

2 changes: 1 addition & 1 deletion tests/pure/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
32 changes: 32 additions & 0 deletions tests/tests/esnext.array.flat.js
Original file line number Diff line number Diff line change
@@ -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');
});
32 changes: 0 additions & 32 deletions tests/tests/esnext.array.flatten.js

This file was deleted.

2 changes: 1 addition & 1 deletion tests/tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit b4ff1ad

Please sign in to comment.