1.0.0 • Published 5 years ago
babel-plugin-transform-array-prototype-find v1.0.0
babel-plugin-transform-array-prototype-find
Transforms arr.find(predicate) to ES5 without a polyfill
Inspired by babel-plugin-array-includes.
Example
In
[1, 2, 3].find(v => v === 1);
[1, 2, 3]['find'](v => v === 1);
arr.find(v => v === 1);
arr['find'](v => v === 1);Out
[1, 2, 3].filter(v => v === 1)[0];
[1, 2, 3].filter(v => v === 1)[0];
Array.isArray(arr) ? arr.filter(v => v === 1)[0] : arr.find(v => v === 1);
Array.isArray(arr) ? arr.filter(v => v === 1)[0] : arr['find'](v => v === 1);Caveats
This is not a true replacement for find.
While find stops iterating when it finds a match, filter does not.
If the predicate causes side effects, do not use this plugin.
Installation
$ npm install babel-plugin-transform-array-prototype-findUsage
Via .babelrc (Recommended)
.babelrc
{
"plugins": ["transform-array-prototype-find"]
}Via CLI
$ babel --plugins transform-array-prototype-find script.jsVia Node API
require("@babel/core").transform("code", {
plugins: ["transform-array-prototype-find"]
});1.0.0
5 years ago