Sanfu 3.2.0

Utils

Utils

Source:

General purpose utilities

Methods

log(label, x) → {Any}

Source:
Signature:
  • String -> a -> a
Category:
  • Function

This is a small utility that is not intended to be used for anything but tests or just fast checks. It uses console.log to output whatever it gets along with the provided label (AKA title), then it returns the same thing logged to console. For a better alternative, suitable for more serious tasks take a loop at inspect

Example
pipe([
  log ('First step') // logs  {name: 'Joe', age: '19'} to console
  , prop('name')
  , log ('Second step') // logs `Joe` to console 
])({name: 'Joe', age: '19'})
Parameters:
Name Type Description
label String

What should be shown before logging to console

x Any

Anything that will be logged to the console

Returns:

The latest argument x untouched

Type
Any

spy(logger, label) → {Any}

Source:
Signature:
  • Function f => Logger -> String -> (a -> b) -> a -> b
Category:
  • Function

This function is inteded to be used to wrap other unary functions and take a look on what are they getting as input. The input will be logged using the provided logger and the inspected function will be applied to it, the result is what will be returned Please note that the wrapped functions must be unary.

Example
const addOne = a => a + 1
const divedeBy2 = a => a / 2
pipe([
spy('About to add one:')(addOne) // When executed logs 1 then returns 2
, spy('About to divide:')(divideBy2) // When executed logs 2 then returns 1
])(1)
Parameters:
Name Type Description
logger Logger

A logger instance to use

label String

Which label should be added to each log output

Returns:

The provided value with fn applied to it

Type
Any

tap(f, x) → {Any}

Source:
Category:
  • Function
tap :: (* -> *) -> a -> a

Wraps a function and returns a new function that will take any parameter, pass it to the wrapped function which return value will be ignored and ther returns the given value. It could be useful to introduce logging into pipelines and/or execute functions with side effects without interfering the pipeline

Parameters:
Name Type Description
f function

A function to be wrapped by tap

x Any

A value that will be passed to the wrapped function and then returned

Returns:

Returns the input untouched

Type
Any