npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ebay/retriever

v1.1.0

Published

retrieve nested data safely

Downloads

19

Readme

retriever

retriever is a small utility library for retrieving nested data safely. It contains several improvements over solutions like lodash.get, such as:

  • Type checking: Ensure that accessed data is of the exact type that is needed.
  • Optional logging: Log warnings in cases of required data that is missing or of the wrong type.
  • Convenient defaults: Retrieving data looks for strings by default, and also provides extra utility for objects.

Installation

npm install @ebay/retriever --save

Usage

var r = require('@ebay/retriever');

// set optional logger for missing data or type mismatch with defaultValue
r.setLogger({
    warn: function (messageFormat, eventType, lookupPath, defaultValue) {} // used with need()
});

// sample data where content is not guaranteed
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
    }
};

// cache parent when accessing multiple children
var action = r.need(input, 'model.action', {}); // input.model.action (from object)

// assumed defaultValue is empty string if none is provided
var text = r.need(action, 'textSpans[0].text'); // 'Search for iphone on eBay' (from object)

// {} is transformed to contain helper property when used as default
var content = r.need(action, 'model.content', {}); // {__isEmpty: true} (from defaultValue)

// use has() when presence is needed, but not the actual data
var hasContent = r.has(input, 'model.content'); // false

// type of defaultValue must match type of data, otherwise will log
var count = r.need(input, 'model.count', 50); // 20 (from object)
var count = r.need(input, 'model.count', '50'); // '50' (from defaultValue), logs `warning`
var count = r.get(input, 'model.count', '50'); // '50' (from defaultValue), does not log

// defaults to defaultValue when data is missing or of mismatched type
var list = r.need(input, 'model.missingProperty', []); // [] (from defaultValue)
var list = r.need(input, 'model.list'); // '' (from default defaultValue)
var list = r.need(input, 'model.list', ''); // '' (from defaultValue)
var list = r.need(input, 'model.list', {}); // {__isEmpty: true} (from defaultValue)
var list = r.need(input, 'model.list', 0); // 0 (from defaultValue)
var list = r.need(input, 'model.list', false); // false (from defaultValue)
var list = r.need(input, 'model.list', []); // [1, 2, 3] (from object)

// use get() if logging isn't necessary
var disabled = r.get(input, 'model.disabled', false); // true (from object)
var enabled = r.get(input, 'model.enabled', false); // false (from defaultValue)
var enabled = r.get(input, 'model.enabled', true); // true (from defaultValue)

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 not log a warning.

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.

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. If you are using this with other logging libraries, you'll need to ensure that the logging is enabled per those environment settings.

Arguments

  • logger (Object): A logger object containing the function warn. This function will be called with the following parameters: messageFormat, eventType, lookupPath, defaultValue.

For example, a type mismatch warning, the parameters might look like this:

  • messageFormat: 'event: %s, path: %s, default: %s'
  • eventType: 'typeMismatch'
  • lookupPath: 'data.path[0]'
  • default: ''

Similar Projects