All Projects → f → ufcs

f / ufcs

Licence: MIT license
DLang's Universal Function Call Syntax port to JavaScript

Programming Languages

javascript
184084 projects - #8 most used programming language

UFCS.js

DLang's Uniform Function Call Syntax (UFCS) port to JavaScript

It's mutates the Object.prototype, be careful when you are using it.

Install

npm install ufcs

Overview

A free function can be called with a syntax that looks as if the function were a member function of its first parameter type.

Idea is simple, result is awesome.

// With UFCS, you can call this function ...
funcName ( firstParameter, [otherParameters...] )

// ... like this.
firstParameter.funcName ( [otherParameters...] )

Uniform Wrapper

Just wrap the function and ufcs will handle rest.

var uniform = require('ufcs');

uniform(add);
function add(x, y) {
  return x + y;
}

var result = add(2, 3); //=> 5
var result = (2).add(3); //=> 5

Chaining

Since it mutates Object.prototype you can use it with every type. So, you can chain the function.

var result = add(2, 3).add(4).add(5); //=> 2 + 3 + 4 + 5 = 14

Examples

uniform(add);
function add(x, y) { return x + y; }

uniform(remove);
function remove(x, y) { return x - y; }

uniform(multiply);
function multiply(x, y) { return x * y; }

uniform(divide);
function divide(x, y) { return x / y; }

(2).add(3).multiply(6).divide(2).remove(3).add(5).divide(5); //=> 3.4,  It's ((((2 + 3) * 6) / 2) - 3 + 5) / 5

Multiple Uniform

function add(x, y) { return x + y; }
function remove(x, y) { return x - y; }
function multiply(x, y) { return x * y; }
function divide(x, y) { return x / y; }

uniform(add, remove, multiply, divide);

(2).add(3).multiply(6).divide(2).remove(3).add(5).divide(5); //=> 3.4,  It's ((((2 + 3) * 6) / 2) - 3 + 5) / 5

Alternative syntax:

uniform(
  function add(x, y) { return x + y; },
  function remove(x, y) { return x - y; },
  function multiply(x, y) { return x * y; },
  function divide(x, y) { return x / y; }
);

Object Uniform

Normally, it's not allowed to uniform an anonymous function, but in this case, you can. Please do not use this in these functions.

var Maths = {
  add: function (x, y) { return x + y; },
  remove: function (x, y) { return x - y; },
  multiply: function (x, y) { return x * y; },
  divide: function (x, y) { return x / y; }
};
uniform(Maths); // Uniform all the methods.

add(1, 2).divide(3); //=> 1
(1).add(2).divide(3); //=> 1

License

MIT.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].