https://github.com/yehzhang/babel-plugin-drop-hof
Transforms higher-order function calls such as map, filter, and reduce to loops.
https://github.com/yehzhang/babel-plugin-drop-hof
babel babel-plugin javascript optimization performance
Last synced: 5 months ago
JSON representation
Transforms higher-order function calls such as map, filter, and reduce to loops.
- Host: GitHub
- URL: https://github.com/yehzhang/babel-plugin-drop-hof
- Owner: yehzhang
- License: mit
- Created: 2017-09-29T01:16:19.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2024-06-18T03:58:50.000Z (about 2 years ago)
- Last Synced: 2025-03-16T11:09:23.391Z (over 1 year ago)
- Topics: babel, babel-plugin, javascript, optimization, performance
- Language: JavaScript
- Homepage:
- Size: 26.4 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# babel-plugin-drop-hof
> Transforms higher-order function calls to loops.
Currently supported higher-order functions are: `forEach`, `map`, `filter`, `every`, `some`, and `reduce`.
## Example
**In**
```javascript
var result = array.map(function (x) {
return x * 2;
});
```
**Out**
```javascript
var _a = array;
var _i = 0;
var _f = function _f(x) {
return x * 2;
};
var _r = [];
for (; _i < _a.length; _i++) {
var _e = _a[_i];
var _z;
_z = _f(_e, _i, _a);
_r.push(_z);
}
var result = _r;
```
Some corner cases are handled properly:
**In**
```javascript
isValid() && array.map(function (x) {
return x * 2;
});
while (array.map(function (x) {
return x * 2;
}));
```
**Out**
```javascript
// **Same as input**
```
## Installation
```sh
npm install --save babel-plugin-drop-hof
```
## Usage
### Via `.babelrc`
**.babelrc**
```json
{
"plugins": ["babel-plugin-drop-hof"]
}
```
### Via CLI
```sh
babel --plugins babel-plugin-drop-hof script.js
```
### Via Node API
```javascript
require("babel-core").transform("code", {
plugins: ["babel-plugin-drop-hof"]
});
```