-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add array.prototype.flat, array.prototype.flatMap
- Loading branch information
Showing
27 changed files
with
446 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import jscodeshift from 'jscodeshift'; | ||
import { removeImport } from '../shared.js'; | ||
|
||
/** | ||
* @typedef {import('../../types.js').Codemod} Codemod | ||
* @typedef {import('../../types.js').CodemodOptions} CodemodOptions | ||
*/ | ||
|
||
/** | ||
* @param {CodemodOptions} [options] | ||
* @returns {Codemod} | ||
*/ | ||
export default function (options) { | ||
return { | ||
name: 'array.prototype.flat', | ||
transform: ({ file }) => { | ||
const j = jscodeshift; | ||
const root = j(file.source); | ||
let dirtyFlag = false; | ||
|
||
const { identifier } = removeImport('array.prototype.flat', root, j); | ||
|
||
root | ||
.find(j.CallExpression, { | ||
callee: { | ||
type: 'Identifier', | ||
name: identifier, | ||
}, | ||
}) | ||
.forEach((path) => { | ||
const args = path.value.arguments; | ||
if (args.length === 2) { | ||
const [array, callback] = args; | ||
|
||
const newExpression = j.callExpression( | ||
//@ts-ignore | ||
j.memberExpression(array, j.identifier('flat')), | ||
[callback], | ||
); | ||
j(path).replaceWith(newExpression); | ||
dirtyFlag = true; | ||
} | ||
}); | ||
|
||
return dirtyFlag ? root.toSource(options) : file.source; | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import jscodeshift from 'jscodeshift'; | ||
import { removeImport } from '../shared.js'; | ||
|
||
/** | ||
* @typedef {import('../../types.js').Codemod} Codemod | ||
* @typedef {import('../../types.js').CodemodOptions} CodemodOptions | ||
*/ | ||
|
||
/** | ||
* @param {CodemodOptions} [options] | ||
* @returns {Codemod} | ||
*/ | ||
export default function (options) { | ||
return { | ||
name: 'array.prototype.flatMap', | ||
transform: ({ file }) => { | ||
const j = jscodeshift; | ||
const root = j(file.source); | ||
let dirtyFlag = false; | ||
|
||
const { identifier } = removeImport('array.prototype.flatMap', root, j); | ||
|
||
root | ||
.find(j.CallExpression, { | ||
callee: { | ||
type: 'Identifier', | ||
name: identifier, | ||
}, | ||
}) | ||
.forEach((path) => { | ||
const args = path.value.arguments; | ||
if (args.length === 2) { | ||
const [array, callback] = args; | ||
|
||
const newExpression = j.callExpression( | ||
//@ts-ignore | ||
j.memberExpression(array, j.identifier('flatMap')), | ||
[callback], | ||
); | ||
j(path).replaceWith(newExpression); | ||
dirtyFlag = true; | ||
} | ||
}); | ||
|
||
return dirtyFlag ? root.toSource(options) : file.source; | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
var assert = require('assert'); | ||
|
||
assert.deepEqual( | ||
[1, 2, 3].flat(function (x) { | ||
return x >= 2; | ||
}), | ||
[2, 3], | ||
); | ||
assert.deepEqual( | ||
[1, 2, 3].flat(function (x) { | ||
return x <= 2; | ||
}), | ||
[1, 2], | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
var flat = require('array.prototype.flat'); | ||
var assert = require('assert'); | ||
|
||
assert.deepEqual( | ||
flat([1, 2, 3], function (x) { | ||
return x >= 2; | ||
}), | ||
[2, 3], | ||
); | ||
assert.deepEqual( | ||
flat([1, 2, 3], function (x) { | ||
return x <= 2; | ||
}), | ||
[1, 2], | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
var assert = require('assert'); | ||
|
||
assert.deepEqual( | ||
[1, 2, 3].flat(function (x) { | ||
return x >= 2; | ||
}), | ||
[2, 3], | ||
); | ||
assert.deepEqual( | ||
[1, 2, 3].flat(function (x) { | ||
return x <= 2; | ||
}), | ||
[1, 2], | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
var assert = require('assert'); | ||
|
||
assert.deepEqual( | ||
[1, 2, 3].flat(function (x) { | ||
return x >= 2; | ||
}), | ||
[2, 3], | ||
); | ||
assert.deepEqual( | ||
[1, 2, 3].flat(function (x) { | ||
return x <= 2; | ||
}), | ||
[1, 2], | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import flat from 'array.prototype.flat'; | ||
var assert = require('assert'); | ||
|
||
assert.deepEqual( | ||
flat([1, 2, 3], function (x) { | ||
return x >= 2; | ||
}), | ||
[2, 3], | ||
); | ||
assert.deepEqual( | ||
flat([1, 2, 3], function (x) { | ||
return x <= 2; | ||
}), | ||
[1, 2], | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
var assert = require('assert'); | ||
|
||
assert.deepEqual( | ||
[1, 2, 3].flat(function (x) { | ||
return x >= 2; | ||
}), | ||
[2, 3], | ||
); | ||
assert.deepEqual( | ||
[1, 2, 3].flat(function (x) { | ||
return x <= 2; | ||
}), | ||
[1, 2], | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
var assert = require('assert'); | ||
|
||
assert.deepEqual( | ||
[1, 2, 3].flat(function (x) { | ||
return x >= 2; | ||
}), | ||
[2, 3], | ||
); | ||
assert.deepEqual( | ||
[1, 2, 3].flat(function (x) { | ||
return x <= 2; | ||
}), | ||
[1, 2], | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
var banana = require('array.prototype.flat'); | ||
var assert = require('assert'); | ||
|
||
assert.deepEqual( | ||
banana([1, 2, 3], function (x) { | ||
return x >= 2; | ||
}), | ||
[2, 3], | ||
); | ||
assert.deepEqual( | ||
banana([1, 2, 3], function (x) { | ||
return x <= 2; | ||
}), | ||
[1, 2], | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
var assert = require('assert'); | ||
|
||
assert.deepEqual( | ||
[1, 2, 3].flat(function (x) { | ||
return x >= 2; | ||
}), | ||
[2, 3], | ||
); | ||
assert.deepEqual( | ||
[1, 2, 3].flat(function (x) { | ||
return x <= 2; | ||
}), | ||
[1, 2], | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
var assert = require('assert'); | ||
|
||
assert.deepEqual( | ||
[1, 2, 3].flat(function (x) { | ||
return x >= 2; | ||
}), | ||
[2, 3], | ||
); | ||
assert.deepEqual( | ||
[1, 2, 3].flat(function (x) { | ||
return x <= 2; | ||
}), | ||
[1, 2], | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import banana from 'array.prototype.flat'; | ||
var assert = require('assert'); | ||
|
||
assert.deepEqual( | ||
banana([1, 2, 3], function (x) { | ||
return x >= 2; | ||
}), | ||
[2, 3], | ||
); | ||
assert.deepEqual( | ||
banana([1, 2, 3], function (x) { | ||
return x <= 2; | ||
}), | ||
[1, 2], | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
var assert = require('assert'); | ||
|
||
assert.deepEqual( | ||
[1, 2, 3].flat(function (x) { | ||
return x >= 2; | ||
}), | ||
[2, 3], | ||
); | ||
assert.deepEqual( | ||
[1, 2, 3].flat(function (x) { | ||
return x <= 2; | ||
}), | ||
[1, 2], | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
var assert = require('assert'); | ||
|
||
assert.deepEqual( | ||
[1, 2, 3].flatMap(function (x) { | ||
return x >= 2; | ||
}), | ||
[2, 3], | ||
); | ||
assert.deepEqual( | ||
[1, 2, 3].flatMap(function (x) { | ||
return x <= 2; | ||
}), | ||
[1, 2], | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
var flatMap = require('array.prototype.flatMap'); | ||
var assert = require('assert'); | ||
|
||
assert.deepEqual( | ||
flatMap([1, 2, 3], function (x) { | ||
return x >= 2; | ||
}), | ||
[2, 3], | ||
); | ||
assert.deepEqual( | ||
flatMap([1, 2, 3], function (x) { | ||
return x <= 2; | ||
}), | ||
[1, 2], | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
var assert = require('assert'); | ||
|
||
assert.deepEqual( | ||
[1, 2, 3].flatMap(function (x) { | ||
return x >= 2; | ||
}), | ||
[2, 3], | ||
); | ||
assert.deepEqual( | ||
[1, 2, 3].flatMap(function (x) { | ||
return x <= 2; | ||
}), | ||
[1, 2], | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
var assert = require('assert'); | ||
|
||
assert.deepEqual( | ||
[1, 2, 3].flatMap(function (x) { | ||
return x >= 2; | ||
}), | ||
[2, 3], | ||
); | ||
assert.deepEqual( | ||
[1, 2, 3].flatMap(function (x) { | ||
return x <= 2; | ||
}), | ||
[1, 2], | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import flatMap from 'array.prototype.flatMap'; | ||
var assert = require('assert'); | ||
|
||
assert.deepEqual( | ||
flatMap([1, 2, 3], function (x) { | ||
return x >= 2; | ||
}), | ||
[2, 3], | ||
); | ||
assert.deepEqual( | ||
flatMap([1, 2, 3], function (x) { | ||
return x <= 2; | ||
}), | ||
[1, 2], | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
var assert = require('assert'); | ||
|
||
assert.deepEqual( | ||
[1, 2, 3].flatMap(function (x) { | ||
return x >= 2; | ||
}), | ||
[2, 3], | ||
); | ||
assert.deepEqual( | ||
[1, 2, 3].flatMap(function (x) { | ||
return x <= 2; | ||
}), | ||
[1, 2], | ||
); |
Oops, something went wrong.