0.3.1 • Published 5 years ago

babel-plugin-proto-ops v0.3.1

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

babel-plugin-proto-ops

A simple babel plugin to allow operator overloading on Object prototypes.

Definitions:

symbolmeaning
Any of these valid operators
_(underscore) any variable
a --> ba transpiles to b
-x-: fnfn does not transpile

How it works

This plugin does 2 things:

1. Calls Object.defineProperties on Object.prototype to define operator methods on the Object prototype.

2. Transpiles all operator functionality in code to call their relevent methods instead.

  • a ⊙ b --> a['⊙'](b)
  • Exception: -x- : return this.valueOf() ⊙ _.valueOf() -x- : return _.valueOf() ⊙ this.valueOf()

    This plugin is not recommended for client-side scripts as it transpiles all operators into functions - which has a massive performance impact.

Valid Operators: (current: Binary only)

Usage

This plugin's powers can be made use of in the following ways:

1. Operator overloading

To overload an operator ⊙ for any object type simply change the function logic for the method "⊙" of its prototype.

Example:

String.prototype["*"] = function(x) {
    return this.repeat(x) //repeat "this" string x times
}

const [oh, oros] = "oh-.oros".split('.')
console.log(oh * 4 + oros)

2. Class method definitions

To define specific operator logic for members of any class, simply define the method in the class using computed string keys.

Example:

class Vector {
    constructor(...cells) {
        this.cells = cells
    }
    ["+"](other) {
        const result = this.cells.map((cell, c) => cell + other.cells[c])
        return new Vector(...result)
    }
    ["-"](other) {
        const result = this.cells.map((cell, c) => cell - other.cells[c])
        return new Vector(...result)
    }
}

const A = new Vector(1,2)
const B = new Vector(2,3)
console.log(A + B) // --> A['+'](B)
0.3.1

5 years ago

0.3.0

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago

0.0.3

5 years ago