@walltowall/auxline
v0.1.2
Published
Generate auxillary data using a series of contextually aware functions.
Downloads
1
Readme
auxline
Generate auxillary data using a series of contextually aware functions.
npm i auxline
Usage
import auxline from 'auxline'
// Data used to generate auxillary data.
const list = [
{ id: 1, type: 'HeroBlock', text: 'Text for a Hero component' },
{ id: 2, type: 'CallToActionBlock', text: 'Hey', buttonText: 'Call Me' },
{ id: 3, type: 'FooterBlock', year: 2074 },
]
// List of functions to generate auxillary data derived from the provided data.
const fns = {
// Get a list of types for each item in data.
types: data => data.map(x => x.type),
// Get a list of keys for each item in data.
keys: data => data.map(x => x.id),
// Get a list of containers for each item in data.
containers: (data, { types }) =>
data.map((x, i) => (types[i] === 'FooterBlock' ? 'footer' : 'body')),
// Group data items by container with keys.
tree: (data, { keys, containers }) =>
data.reduce((acc, x, i) => {
const key = keys[i]
const container = containers[i]
acc[container] = (acc[container] || []).concat({ ...x, key })
return acc
}, {}),
}
const { types, keys, containers, tree } = auxline(fns, list)
API
auxline(fns, data)
fns
: Object of functions. Each key represents a group of auxillary data. Auxillary data will be available to subsequent functions using the function's key.data
: Any data to be used to generate auxillary data.
Each function in fns
is provided two pieces of data:
data
: The original data provided toauxline
.context
: An object of auxillary data. Each function receives the aggregate auxillary data keyed by function name.
Functions are called serially in the order of property definition.