0.1.0 • Published 3 years ago
obj-from-keys-list v0.1.0
obj-from-keys-list
Create an object from a list of keys and a value generator function.
API
This module exports one function:
objectFromKeys(opt, keysList)
Iterate¹ over keysList to produce entries in an output object.
(¹ see opt.gen below.)
Returns the output object.
If opt is truthy but opt.gen (value generator function, see below)
is not, opt is treated as the value for opt.gen.
This is to simplify passing a function when you need no other options.
opt is an optional options object that supports these optional keys:
forEach: Reserved for detecting when you gave only akeysList, noopt.proto: Select the prototype for the output object.undefined(default): Start with an empty object literal.null: Start with an empty object that has no prototype.'map': Start with an empty native Map and use'set'asopt.setMtd.- any truthy value: Use this as the prototype.
gen: A value generator function. It will be invoked with whatever argumentskeysList.forEachwill give (so usually,(key, index, keysList)) and its result will be assigned to the output object as a property named the value ofkey..forEachis chosen to make it work with arrays as well as nativeMapandSetobjects.- The number
1can be used asopt.gento use the key as the value.
setMtd: Name of the setter method, as a string. If truth-y, instead of assigning properties of the output object, call itssetMtd-named method with arguments(key, value).
Usage
see test/usage.mjs.
Known issues
- Needs more/better tests and docs.
License
undefined
0.1.0
3 years ago