Transforms `arr.find(predicate)` to ES5 without a polyfill
npm install babel-plugin-transform-array-prototype-findTransforms arr.find(predicate) to ES5 without a polyfill
Inspired by babel-plugin-array-includes.
In
``javascript`
[1, 2, 3].find(v => v === 1);
[1, 2, 3]'find';
arr.find(v => v === 1);
arr'find';
Out
`javascript`
[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);
This is not a true replacement for find.find
While stops iterating when it finds a match, filter does not.
If the predicate causes side effects, do not use this plugin.
`sh`
$ npm install babel-plugin-transform-array-prototype-find
.babelrc
`json`
{
"plugins": ["transform-array-prototype-find"]
}
`sh`
$ babel --plugins transform-array-prototype-find script.js
`javascript``
require("@babel/core").transform("code", {
plugins: ["transform-array-prototype-find"]
});