retriever
retriever
is a small utility library for retrieving nested data safely. It contains several improvements over solutions like lodash.get
, such as convenient defaults, type checking, and optional logging.
Installation
npm install @ebay/retriever --save
Usage
var r = require('@ebay/retriever');
r.setLogger({
debug: function (messageFormat, lookupPath, defaultValue, logType) {},
warn: function (messageFormat, lookupPath, defaultValue, logType) {}
});
var input = {
model: {
action: {
url: 'https://www.ebay.com/sch/iphone'
textSpans: [
{
text: 'Search for iphone on eBay'
}
]
},
list: [1, 2, 3],
count: 20,
disabled: true
}
};
var action = r.need(input, 'model.action', {});
var text = r.need(action, 'textSpans[0].text');
var content = r.need(action, 'model.content', {});
var hasContent = r.has(input, 'model.content');
var count = r.need(input, 'model.count', 50);
var count = r.need(input, 'model.count', '50');
var count = r.get(input, 'model.count', '50');
var list = r.need(input, 'model.missingProperty', []);
var list = r.need(input, 'model.list');
var list = r.need(input, 'model.list', '');
var list = r.need(input, 'model.list', {});
var list = r.need(input, 'model.list', 0);
var list = r.need(input, 'model.list', false);
var list = r.need(input, 'model.list', []);
var disabled = r.get(input, 'model.disabled', false);
var enabled = r.get(input, 'model.enabled', false);
var enabled = r.get(input, 'model.enabled', true);
API
need(object, path, [defaultValue])
get(object, path, [defaultValue])
Gets the value at path of object. Uses Lodash's get method. If the resolved value is undefined
or if there is a type mismatch between the resolved value and default value, the defaultValue
is returned in its place. If the defaultValue
is an empty object, an object with an internal helper __isEmpty
property is returned in its place. A type mismatch is determined with strict type checking that differentiates between object
, array
, and null
. This is opposed to the native typeof
which treats those identically as being type object
.
need()
assumes that the data of the specified type needs to be present. Otherwise, it will log a warning.
get()
is more lenient, and will only log debug
instead of warn
.
Arguments
object
(Object): The object to query.path
(Array | String): The path of the property to get.[defaultValue]
(*): The value returned for undefined resolved values. (defaults to '')
Returns
(*): Returns the resolved value.
has(object, path)
Checks if path is a direct property of object, and has a value that is not null or undefined.
This will log debug
if the data is missing.
Arguments
object
(Object): The object to query.path
(Array | String): The path of the check.
Returns
(boolean): Returns true
if path exists, else false
.
setLogger(logger)
Sets the logger to be used for logging any issues in retrieving the data. If logging is desired, this should be called once at the start of the app to be used for all subsequent usage. If retriever
logging is desired on the client, setLogger
must be initialized in the browser as well.
Arguments
logger
(Object): A logger object containing the functions debug
and warn
. These functions will be called with the following parameters:
messageFormat
, lookupPath
, defaultValue
, logType
.
Similar Projects