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

simple-object-attribute-resolver

v1.0.4

Published

Simple Object Attribute Resolver - Resolves and returns the value from a Javascript object with simple path expression

Downloads

3

Readme

simple-object-attribute-resolver

Simple Object Attribute Resolver - Resolves and returns the value from a Javascript object with simple path expression.

Frequently we need to traverse deep attribute path to extract some value from some object. This might require some defensive undefined object reference checking. JSON path might be too burdensome to use for simple use cases. This package aims to proivde simple yet reasonible exporession to access attribute values of an object.

Quick Examples

const soar = require('simple-object-attribute-resolver');

const data = {
  attr1: {
    attr11: {
      attr111: "Hello"
    }, attr1list1: [
      "value1"
      ,"value2"
      ,"value3"
    ]
  }, attr2: {

  }, list1: [
    [[1, 2, 3], [4, 5]]
  ], list2: [
    {
      hello: "World"
    }
  ]
};

soar.resolve(data, 'attr1.attr11.attr111'); // Hello
soar.resolve(data, 'attr1.attr1list1[1]'); // value2

const soarr1 = soar.resolver('attr1.attr11.attr111');
soarr1(data); // Hello
  • See test directory for more examples

Install

Install from npm:

$ npm install simple-object-attribute-resolver

Methods

soar.isEmpty(data)

Convinent method that checks if data is empty. An empty data is null, undefined, array with length zero or object without any own property.

  • params:
    • data - the data to check
  • return:
    • true - if empty

soar.resolve(data, path)

Returns the value or reference to the element in the data object given by path.

  • params:
    • data - the object to resolve the value from
    • path - simple dot path expression
  • return:
    • the value of the element or the reference to the element in the data object.

soar.resolveArray(data, path)

Returns an array for the value or reference to the element in the data object given by path.

  • params:
    • data - the object to resolve the value from
    • path - simple dot path expression
  • return:
    • the value itself if it was an array or wrap the value in an array.

soar.resolver(path)

Returns the curried resolver function for the given path. It is useful to resolve the same attribute across different data object.

  • params:
    • path - simple dot path expression
  • return:
    • a resolver function for the given path which could resolve the attribute by passing data object.

soar.arrayResolver(path)

The resolver counter part of resolveArray.

  • params:
    • path - simple dot path expression
  • return:
    • a resolver function for the given path which could resolve the attribute as an Array by passing data object.

Notes

  1. Be aware of the side effects. As the return value could be the reference to the elements in the data object, modifying the attribute of the reference could affect the source data (shallow copy).
  2. This module is intented for quickly resolving a value from an input with the given path without having to worry about null, undefined or non-existing element along the path. If it is desired to find out which part of the path is causing undefined result, either print out both of the input and path to figure it out manually or just execute the actual de-referencing with proper Javascript syntax to cause an Error to be thrown.

License

MIT