1.0.1 • Published 7 years ago

babel-plugin-drop-hof v1.0.1

Weekly downloads
304
License
MIT
Repository
github
Last release
7 years ago

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

var result = array.map(function (x) {
  return x * 2;
});

Out

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

isValid() && array.map(function (x) {
  return x * 2;
});

while (array.map(function (x) {
  return x * 2;
}));

Out

// **Same as input**

Installation

npm install --save babel-plugin-drop-hof

Usage

Via .babelrc

.babelrc

{
  "plugins": ["babel-plugin-drop-hof"]
}

Via CLI

babel --plugins babel-plugin-drop-hof script.js

Via Node API

require("babel-core").transform("code", {
  plugins: ["babel-plugin-drop-hof"]
});