1.2.0 • Published 6 years ago

spicy-transducers v1.2.0

Weekly downloads
28
License
MIT
Repository
github
Last release
6 years ago

spicy-transducers

JavaScript library for composable algorithmic transformations.

Heavily inspired by Clojure Transducers, in case it was not obvious enough.

Be warned: Still in very early stage of development. Use at your own risk.

Why

Using transducers decouples data transformations from the actual data being transformed so that they can be easily composed and reused.

Install

npm install --save spicy-transducers
# or
yarn add spicy-transducers

Function Docs

Functions are grouped and exported based on the data type that they operate on.

Any

NameUsageNotes
chainchain(fn1, fn1, ...fnN)(value)Chain n functions and provide the initial value to be passed.
constantconstant(value)Creates a constant function.
fallbackfallback(fallbackValue)(testValue)Enables you to always have a fallback value if testValue is null or undefined.
identityidentity(value)An identity function.

Array

NameUsageNotes
mapmap(mapFunction)(array)Map function can use the same signature as Array.prototype.map().
reducereduce(reduceFunction, defaultValue)(array)Reduce function can have the same signature as Array.prototype.reduce().
poppop(array)Returns a new array, with the previous head removed.
pushpush(value)(array)Returns a new array, with the new value pushed as the head.
getget(index)(array)
setset(index, value)(array)
updateupdate(index, updateFn)(array)The update function will get the current value of the specified index as it’s argument.

Number

NameUsage
getNthFromget(array)(index)

Object

NameUsageNotes
callMethodcall(methodName)(argument1, argument2, ...argumentN)(object)Make sure the method exists, otherwise TypeError: value[methodName] is not a function.
valuesvalues(object)Uses Object.keys()
entriesentries(object)Uses Object.entries()
keyskeys(object)Uses Object.keys()
getget(key)(object)
setset(key, value)(object)
updateupdate(key, updateFn)(object)The update function will get the current value of the specified key as it’s argument.
deletedelete(key)(object)

Usage Examples

Let’s say you have an object { q1: Number, q2: Number, q3: Number, q4: Number } which represents the number of sales per quarter. You want to: 1. Flatten this structure 2. Multiply the number of sales by their price of $99.99 3. Sum the total profits

Let’s see how some of the functions exported by this module can help you build a reusable transducer for this data.

  import { chain, map, reduce , values } from 'spicy-transducers'; 
  const sales = { q1: 33, q2: 12, q3: 40, q4: 65 }; // our input data
  const toPrice = _ => _ * 99.99; // our multiplier
  const add = (acc, _) => acc + _; // our sum function
  
  const profitsTransducer = chain(
    values,
    map(toPrice),
    reduce(add, 0)
  );
  const result = profitsTransducers(sales); // => 14998.5

Contributors

License

Licensed under the MIT License.

Copyright © 2017-2018 SpiceFactory

1.2.0

6 years ago

1.1.0

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago