babel-plugin-transform-reduce
TypeScript icon, indicating that this package has built-in type declarations

0.2.1 • Public • Published

babel-plugin-transform-reduce

Turns this

const fn = () => [1, 2, 3].filter(i => i > 1).map(i => i * 3)

Into this

const fn = () => {
  const _filterFn = (i) => i > 1
  const _mapFn = (i) => i * 3

  const _reducer = (_acc, _curr) => {
    if (_filterFn(_curr)) {
      _acc.push(_mapFn(_curr, _acc.length))
    }

    return _acc
  }

  return [1, 2, 3].reduce(_reducer, [])
}

Because it's way faster, but less readable. So I've created this plugin to deal with it: it transforms .filter() + .map() calls to .reduce() under the hood!

Usage:

yarn add babel-plugin-transform-reduce --dev

then, in your .babelrc:

{
  "plugins": ["transform-reduce"]
}

You're all set!

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i babel-plugin-transform-reduce

Weekly Downloads

9

Version

0.2.1

License

MIT

Unpacked Size

52.4 kB

Total Files

28

Last publish

Collaborators

  • zaguini