figgy-pudding
Note: pending imminent deprecation
This module will be deprecated once npm v7 is released. Please do not rely on it more than absolutely necessary (ie, only if you are depending on it for use with npm v6 internal dependencies).
figgy-pudding
is a small JavaScript library for managing and composing cascading options objects -- hiding what needs to be hidden from each layer, without having to do a lot of manual munging and passing of options.
The God Object is Dead!
Now Bring Us Some Figgy Pudding!
Install
$ npm install figgy-pudding
Table of Contents
Example
Features
hide options from layer that didn't ask for it
shared multi-layer options
make sure
opts
argument is availabletransparent key access like normal keys, through a Proxy. No need for
.get()
!default values
key aliases
arbitrary key filter functions
key/value iteration
serialization
100% test coverage using
tap --100
API
> figgyPudding({ key: { default: val } | String }, [opts]) -> PuddingFactory
Defines an Options constructor that can be used to collect only the needed options.
An optional default
property for specs can be used to specify default values if nothing was passed in.
If the value for a spec is a string, it will be treated as an alias to that other key.
Example
> PuddingFactory(...providers) -> FiggyPudding{}
Instantiates an options object defined by figgyPudding()
, which uses providers
, in order, to find requested properties.
Each provider can be either a plain object, a Map
-like object (that is, one with a .get()
method) or another figgyPudding Opts
object.
When nesting Opts
objects, their properties will not become available to the new object, but any further nested Opts
that reference that property will be able to read from their grandparent, as long as they define that key. Default values for nested Opts
parents will be used, if found.
Example
> opts.get(key) -> Value
Gets a value from the options object.
Example
> opts.concat(...moreProviders) -> FiggyPudding{}
Creates a new opts object of the same type as opts
with additional providers. Providers further to the right shadow providers to the left, with properties in the original opts
being shadows by the new providers.
Example
> opts.toJSON() -> Value
Converts opts
to a plain, JSON-stringifiable JavaScript value. Used internally by JavaScript to get JSON.stringify()
working.
Only keys that are readable by the current pudding type will be serialized.
Example
> opts.forEach((value, key, opts) => {}, thisArg) -> undefined
Iterates over the values of opts
, limited to the keys readable by the current pudding type. thisArg
will be used to set the this
argument when calling the fn
.
Example
> opts.entries() -> Iterator<[[key, value], ...]>
Returns an iterator that iterates over the keys and values in opts
, limited to the keys readable by the current pudding type. Each iteration returns an array of [key, value]
.
Example
> opts[Symbol.iterator]() -> Iterator<[[key, value], ...]>
Returns an iterator that iterates over the keys and values in opts
, limited to the keys readable by the current pudding type. Each iteration returns an array of [key, value]
. Makes puddings work natively with JS iteration mechanisms.
Example
> opts.keys() -> Iterator<[key, ...]>
Returns an iterator that iterates over the keys in opts
, limited to the keys readable by the current pudding type.
Example
> opts.values() -> Iterator<[value, ...]>
Returns an iterator that iterates over the values in opts
, limited to the keys readable by the current pudding type.
Example
'
Last updated