babel-plugin-transform-array-prototype-find
v1.2.1
Published
Transforms `arr.find(predicate)` to ES5 without a polyfill
Downloads
113
Maintainers
Readme
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];
(function (o, a0) { return Array.isArray(o) ? o.filter(a0)[0] : o.find(a0); })(arr, v => v === 1);
(function (o, a0) { return Array.isArray(o) ? o.filter(a0)[0] : o.find(a0); })(arr, 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"]
});