Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

babel-plugin-transform-js-macros

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-transform-js-macros

A handful of useful macros as babel-plugin.

  • 0.0.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
increased by200%
Maintainers
1
Weekly downloads
 
Created
Source

JS-MACROS CircleCI codecov

A handful of useful macros as babel-plugin.

Install

npm install --save-dev babel-plugin-transform-js-macros

.babelrc <- { "plugins": ["babel-plugin-transform-js-macros"] }

SymbolicExpression

Detect free variables in an expression and return a lambda that takes those as arguments.

symbolic, x + y;

gives

({ x, y }) => x + y;

DoNotation

Mimics haskell do-notation. Takes an additional function to flatten the structure.

join, (x = 1), (y = 2), 3;

gives

_joiner => _joiner(1, x => _joiner(2, y => 3));

examples:

// Promise chain
const then = (promise, callback) => Promise.resolve(promise).then(callback);
const six = (join,
(x = Promise.resolve(4)), // binding a Promise
(y = x * 2), // Promise.resolve will take care non-promise values
Promise.resolve(y - 2))(then);
six.then(console.log); // 6
// Maybe
class Nothing {
  mbind(f) {
    return new Nothing();
  }
}
class Just {
  constructor(x) {
    this.x = x;
  }
  mbind(f) {
    return f(this.x);
  }
}
const bind = (monad, binder) => monad.mbind(binder);

// here the magic
const plus3 = n =>
  (join,
  (a = n), // n must be a monad
  (b = new Just(3)),
  new Just(a + b))(bind);

expect(plus3(new Nothing())).toEqual(new Nothing());
expect(plus3(new Just(5))).toEqual(new Just(8));
const toArray = (item, next) => [item].concat(next(item));
expect((join, (a = 1), (b = 2), a + b)(toArray)).toEqual([1, 2, 3]);
const assign = (item, next) => next(item);
expect((join, (a = 1), (b = 2), { a, b })(assign)).toEqual({ a: 1, b: 2 });

TODO

  • fix name collision
  • allow nested macros

Keywords

FAQs

Package last updated on 30 Jan 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc