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

babel-plugin-overload

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-overload

Operator overloading plugin for babel

  • 0.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
9
decreased by-78.57%
Maintainers
1
Weekly downloads
 
Created
Source

babel-plugin-overload

CircleCI

This is an experimental plugin that allows you to overload operators in your Javascript code.

Compare with Python:

class Addable:
  def __add__(self, other):
    return # some computation which uses self and other

Using this Babel plugin:

class Addable {
  [Symbol.for('+')] (other) {
    return // some computation which uses this and other
  }
}

This plugin is still in beta and should be considered highly experimental. It should go without saying that you probably should not use this in production.

Example

In

x + y

Out

function (_left, _right) {
  if (_left !== null && _left !== undefined && _left[Symbol.for("+")]) return _left[Symbol.for("+")](_right);else return _left + _right;
}(x, y);

Supported Operators

At the moment, all binary operators are supported. Unary operators are coming soon.

The full supported list is:

  • The arithmetic operators: +, -, /, *, %, **
  • The bitwise operators: &, |, ^
  • The bitshift operators: >>, <<, >>>, <<<
  • The equality operators: ==, ===, !=, !==
  • The inequality operators: >, <, >=, <=
  • The remaining miscellaneous operators: in and instanceof

To overload any of them, define a method named with the Symbol you get with Symbol.for for the appropriate operator. For instance, use Symbol.for('+') to overload the + operator, and Symbol.for('in') to overload the in operator.

You can define the overloading method on the object itself or anywhere on the object's prototype chain.

At the moment, overloading is only supported for the left side value. Support for overloading on the right side value (the equivalent of Python's __radd__ family of methods) is coming soon.

Installation

$ npm install babel-plugin-overload

Usage

.babelrc

{
  "plugins": ["overload"]
}

Via CLI

$ babel --plugins overload script.js

Via Node API

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

Keywords

FAQs

Package last updated on 21 Jul 2017

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