binapi
v0.2.1
Published
binary api
Downloads
6
Maintainers
Readme
npm install binapi
// github much install |
npm install sourcevault/binapi#dist
🟡 .. quick Example 1 ..
var binapi = require ("binapi")
var main = function (state,args)
{
var a = args[0]
var b = args[1]
if (state.flip) // flip arguments
{
var temporary = a
a = b
b = temporary
}
var output = a - b
if (state.abs) // output only absolute value
{
return Math.abs(output)
}
return output
}
getter = function(state,key) {
state[key] = true
return state;
}
var subtract = binapi(main,getter,{})
subtract(10,5) // 5
subtract.flip(10,5) // - -5
subtract.flip.abs(10,5) // 5
subtract.abs.flip(10,5) // 5
// last two operations are doing the same thing
As shown above, we are using object properties as switches to turn "ON" certain flags in main
.
colors is a good example of module that follows this pattern.
binapi
is a shorthand for binary APIs.
.. Features
- functions are built lazily, if you have 100 methods but the user only uses 3 functions - then only 3 objects are created.
binapi
requires 2 functions to initialize :
- application function - it is run whenever there is a call from the user.
- getter function - run whenever
.
operation is appiled, needed for updating state variable needed by the application function.
🟡 ...Example 2...
var binapi = require ("binapi")
folks =
{
charles:{age:null},
henry:{age:null}
}
var main = function(key,args)
{
folks[key] = args[0]
}
var getter = function(state,key) {return key}
var setAge = binapi(main,getter)
setAge.charles(32)
setAge.henry(29)
console.log(folks.charles) // 32
console.log(folks.henry) //29
..using state variable
Sometimes some state has to be present in your function, this is especially useful for nested binapi
s.
🟡 ..Example 3 - adding state variable as second argument..
var binapi = require("binapi");
var main,getter;
var loop = (state) => binapi(main,getter,state);
var get = ([num],key) => [num,key];
var F6 = ([x,key],args) =>
var y = args[0]
switch (key) {
case "init":
return loop([y]);
case "add":
return loop([x + y]);
case "multiply":
return loop([x * y]);
case "ret":
return x;
default:
return fail(6);
var compute = lopo(["init"])
var out = compute(5)
.add(5)
.multiply(10)
.ret!
Custom Logger
Internally binapi
uses ES6 proxies allowing binding of custom log functions - providing us with the option of providing better object information when using console.log
, custom log function is added as the 4rth argument.
🟡 ..Example 4 - custom logger provided as third argument..
var binapi = require ("binapi")
var main = function (){}
var getter = function(state,key) {return state.concat(key);}
var log = function(state)
{
var chain = state.join(' | ')
console.log ("( " + chain + " )")
}
test = binapi(main,getter,[],log)
tsf = test.sync.flip
console.log (tsf) // ( sync | flip )
Update and API change
0.1.0
major change in API, support forbinapi.obj
dropped.0.0.8
major change in API, defaultbinapi
is list.0.0.4
major change in API, does not usethis
but uses argument to passpath
andstate
.
LICENCE
- Code released under BSD-3-Clause.
- Documentation and images released under CC BY-NC-ND 4.0.
- details can be found here.