Skip to content

Latest commit

 

History

History
205 lines (136 loc) · 4.95 KB

utils.md

File metadata and controls

205 lines (136 loc) · 4.95 KB

nutzen.internal

  • binapi

  • flat - flat

  • R - Ramda

  • l - console.log

  • z - console.log

    • z.n - adds new line before and after console.log
    • z.j - console.log(j(...))
    • z.p - display protos.
    • z.d -
    • z.pa - display all protos.
  • noop - noop function

  • c - 8 bit color palette

  • alpha_sort - alpha-sort

  • esp - error-stack-parser

  • deep_freeze - deep-freeze

  • advanced_pad - advanced-pad

  • lit - zip printer for color output

  • jspc - json-stringify-pretty-compact

    • jspc.r - arguments reversed and curried.
  • wait - setTimeout with the arguments reversed.

  • print_fail - used in test files to show file location for test failure.

  • create_stack - wrapper for error-stack-parser that accepts error object.

  • common_symbols - Symbols used to identify different objects, like nutzen.type.

  • loopfault - a proxy object that acts as a dummy return object to prevent throwing unnecessary errors.

  • util_inspect_custom - wrapper for node.js's util_inspect_custom that does not throw error if used in browser.

nutzen.internal.binapi

Quick Example 1
var binapi = require("nutzen").internal.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.

nutzen.guard namespaces also depends on binapi.

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 :

  1. application function - it is run whenever there is a call from the user.
  2. getter function - run whenever . operation is appiled, needed for updating state variable needed by the application function.

However, it accepts more parameters:

  1. state - the data that is mutated between each getter call.

  2. ulog - custom logger function, the return value is what is displayed.

  3. user_map - when more keys need to be customized.

Quick Example 2
var binapi = require("nutzen").internal.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.

🟡 ..Example 3 - adding state variable as second argument..

var binapi = require("nutzen").internal.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 giving better object information when using console.log, custom log function is added as the 4rth argument.

🟡 ..Example 4 - custom logger provided as 4rth argument..

var binapi = require("nutzen").internal.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 )