README
transformer
A utility library to easily transform your data: describe your desired output, provide an input, and voilà 🌟!
goal
The goal of this library is to allow you to easily create utility functions to transform one type of data to another, using functional programming. You can read more on why you might need transformer on the wiki.
installation
npm i @waldojeffers/transformer
usage
import { identity, map, mapFrom, transform } from '@waldojeffers/transformer'
const input = {
firstname: 'James',
lastname: 'M',
occupation: 'Drummer',
drumsticks: 2,
}
const transformer = transform({
age: x => 2 * x, // age is undefined, this will not be called
fullname: mapFrom(
// compute a from from 2 input props
['firstname', 'lastname'],
([firstname, lastname]) => `${firstname} ${lastname}`,
),
drumsticks: identity, // pass through
occupation: s => s.toLowerCase(), // apply a transformation
})
transformer(input) // { fullname: 'James M', drumsticks: 2, occupation: 'drummer' }
documentation
transform(descriptor)
description
transform :: Object descriptor => (Object input => Object output)
Returns a function which transforms an input object based on the descriptor object you provide it. transform
is a pure function: it will create a new output object and will not modify the input object.
parameters
- descriptor
Object<outputProperty: Transformer>
: An object which describes how the output object will look like. Its keys define which properties will exist on the output object, and its values are transformers (such asidentity
,map
,mapFrom
) which compute the output object's values, usually by mapping from the input values. ⚠️ Any property from the input object whose key is missing in the descriptor will be stripped off in the output object.
return value
A function whose signature is Function: input: Object -> output: Object
. It accepts an input object which will be transformed into an ouput object based on the descriptor object provided earlier.
example
import { identity, mapFrom, transform } from '@waldojeffers/transformer'
const input = {
age: 27,
balance: 400.17,
name: 'James M',
}
const descriptor = {
balance: mapFrom('balance', amount => '