Mapper
Stability: 2 - Stable
const Mapper = require('kado/lib/Mapper')
The Mapper
library provides a functional interface for Object handling.
Class: Mapper
Mapper is a general-purpose Object handling toolkit. Some functions are static and may be used as shortcuts rather than creating a full instance.
static Mapper.getInstance (data)
- Options same as main constructor, below
- Returns: {Mapper} new instance of
Mapper
Mapper.constructor (data)
data
{Object} optional data to populate- Returns: {Mapper} new instance of
Mapper
Instantiates a new empty Mapper
instance, optionally populated with data
Mapper.map (iterator)
iterator
{Function} a method that takesvalue
andkey
as arguments.- Returns: {Mapper} the original object.
Example
const someObject = { foo: 'baz', two: 2, three: null }
Mapper.getInstance(someObject).map((value, key) => {
if (value === null) value = ''
return value
})
Mapper.merge (data)
data
{Object} optional data to populate- Returns: {Mapper} the same as the called
Mapper
, for chaining
Mapper.get (key)
key
{mixed} path to requested data (see parseKey for valid types)- Returns: {mixed} data from key location
Mapper.set (key, value)
key
{mixed} path to target (see parseKey for valid types)value
{mixed} data to place at key location- Returns: {mixed} the same
value
Mapper.delete (key)
key
{mixed} path to delete (see parseKey for valid types)- Returns: {string} deleted item name
Mapper.all ()
- Returns: {Object} all data contained within instance
Static (stateless) functions
static Mapper.mergeObject (base, overlay, depth, maxDepth)
base
{Object} target Objectoverlay
{Object} source Objectdepth
{number} (Default: 0) traversal control for nested itemsmaxDepth
{number} (Default: 50) traversal control for nested items- Returns {Object} the
base
withoverlay
content merged as directed
static Mapper.getFromObject (base, keyArray)
base
{Object} target Object- Returns: {mixed} data from key location
static Mapper.setToObject (base, keyArray, value)
base
{Object} target Object- Returns {Object} the
base
withvalue
set at path indicated bykeyArray
static Mapper.deleteFromObject (base, keyArray, ctx = null)
base
{Object} target Object- Returns {Object} the
base
with path indicated bykeyArray
deleted
static Mapper.parseKey (key)
key
{mixed} any form of locator, per type:- {Array} already in key-array form; pass-through
- {string} path in dotted notation; will be split
- {function} function to call to obtain key
- Returns {Array} from least to most specific, used to traverse an Object