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

jsobjects

v1.0.11

Published

A js library to access and manipulate objects

Downloads

41

Readme

JSObjects

This small library allows you to manipulate objects via string path. It can be used in node.js or browser or any place where javascript can be run.

License: EUPL v1.1 NPM version Downloads Build Status Coverage Status

Node.js

npm i jsobjects --save
import { expandPath, getObjectByPath, updateObjectByPath } from 'jsobjects';

// Will use this objects to show ussage in examples
const arrstr = ['string 1', 'string 2'];
const arrobj = [
    {
        arrobjkey1: 'arrobj1key1',
        'arr obj key 2': 'arr obj 1 key 2',
    },
    {
        arrobjkey1: 'arrobj2key1',
        'arr obj key 2': 'arr obj 2 key 2',
    },
];
const object = {
    key1: 'key1',
    'key 2': 'key 2',
    arrstr,
    arrobj,
    obj: {
        objkey1: 'objkey1',
        'obj key 2': 'obj key 2',
        objarrstr: arrstr,
        objarrobj: arrobj,
    },
    arr: [
        {
            arrkey1: 'arr1key1',
            'arr key 2': 'arr 1 key 2',
            arrarrstr: arrstr,
            arrarrobj: arrobj,
        },
        {
            arrkey1: 'arr2key1',
            'arr key 2': 'arr 2 key 2',
            arrarrstr: arrstr,
            arrarrobj: arrobj,
        },
    ],
};

browser

bower install jsobjects --save

See demo in ./demo folder.

<script src="bower_components/dist/bundle.js"></script>
// This module exposes require from browserify
var jsobjects = require('jsobjects');

// Will use this objects to show usage in examples
var arrstr = ['string 1', 'string 2'];
var arrobj = [
    {
        arrobjkey1: 'arrobj1key1',
        'arr obj key 2': 'arr obj 1 key 2',
    },
    {
        arrobjkey1: 'arrobj2key1',
        'arr obj key 2': 'arr obj 2 key 2',
    },
];
var object = {
    key1: 'key1',
    'key 2': 'key 2',
    arrstr: arrstr,
    arrobj: arrobj,
    obj: {
        objkey1: 'objkey1',
        'obj key 2': 'obj key 2',
        objarrstr: arrstr,
        objarrobj: arrobj,
    },
    arr: [
        {
            arrkey1: 'arr1key1',
            'arr key 2': 'arr 1 key 2',
            arrarrstr: arrstr,
            arrarrobj: arrobj,
        },
        {
            arrkey1: 'arr2key1',
            'arr key 2': 'arr 2 key 2',
            arrarrstr: arrstr,
            arrarrobj: arrobj,
        },
    ],
};

API

getObjectByPath

getObjectByPath(object, path, pathReplacements, offset)

Given an object and a path or generic path, returns the value contained in the object at the specified path.

  • object: The object to search for path
  • path: The path to be expanded, accepts .* notation
  • pathReplacements: Every * in path will be replaced by corresponding in pathReplacements array: ('arr.*.arrarrobj.*.arrkey1', [0, 1]) will be assimilated to arr.0.arrarrobj.1.arrkey1
  • offset: Number of paths to skip at the end: ('arr.*.arrarrobj.*.arrkey1', [0, 1], 3) will be assimilated to arr.0

Examples


// These statements evals to true
getObjectByPath(object, 'arr.0.arrarrobj.0.arrobjkey1')                   === object.arr[0].arrarrobj[0].arrobjkey1;
getObjectByPath(object, 'arr.*.arrarrobj.*.["arr obj key 2"]', [0, 1])    === object.arr[0].arrarrobj[1]['arr obj key 2'];
getObjectByPath(object, "arr.*.arrarrobj.*.['arr obj key 2']", [0, 1])    === object.arr[0].arrarrobj[1]['arr obj key 2'];
getObjectByPath(object, 'arr.*.arrarrobj.*.["arr obj key 2"]', [1, 1], 1) === object.arr[1].arrarrobj[1];

expandPath

expandPath(object, path, callback)

Given an object and a generic path, returns an array with all the paths found in the object. Complex keys (not single word) allows to be refferenced with ["name of the key"] or ['name of the key'].

  • object: The object to search for path
  • path: The path to be expanded, accepts .* notation
  • callback: (Optional) a callback function that will be called for each entry found

Examples

expandPath(object, 'arrstr'); // will output: ['arrstr.0', 'arrstr.1'];
expandPath(object, 'arrobj.*'); // will output: ['arrstr.0', 'arrstr.1'];

expandPath(object, 'arrstr.*'); // will output: ['arrstr.0', 'arrstr.1']
expandPath(object, 'arrobj.*'); // will output: ['arrobj.0', 'arrobj.1']

expandPath(object, 'obj.objarrstr.*'); // will output: ['obj.objarrstr.0', 'obj.objarrstr.1']
expandPath(object, 'obj.objarrobj.*'); // will output: ['obj.objarrobj.0', 'obj.objarrobj.1']

expandPath(object, 'obj.objarrobj.*.arrobjkey1'); // will output: ['obj.objarrobj.0.arrobjkey1', 'obj.objarrobj.1.arrobjkey1']
expandPath(object, 'obj.objarrobj.*.["arr obj key 2"]'); // will output: ['obj.objarrobj.0.["arr obj key 2"]', 'obj.objarrobj.1.["arr obj key 2"]']

expandPath(object, 'arr.*'); // will output: ['arr.0', 'arr.1']

expandPath(object, 'arr.*.arrkey1'); // will output: ['arr.0.arrkey1', 'arr.1.arrkey1']
expandPath(object, 'arr.*.["arr key 2"]'); // will output: ['arr.0.["arr key 2"]', 'arr.1.["arr key 2"]']

expandPath(object, 'arr.*.arrarrstr'); // will output: ['arr.0.arrarrstr', 'arr.1.arrarrstr']
expandPath(object, 'arr.*.arrarrstr.*'); // will output: ['arr.0.arrarrstr.0', 'arr.0.arrarrstr.1', 'arr.1.arrarrstr.0', 'arr.1.arrarrstr.1']
expandPath(object, 'arr.*.arrarrobj.*'); // will output: ['arr.0.arrarrobj.0', 'arr.0.arrarrobj.1', 'arr.1.arrarrobj.0', 'arr.1.arrarrobj.1']

expandPath(object, 'arr.*.arrarrobj.*.arrkey1'); // will output: ['arr.0.arrarrobj.0.arrkey1', 'arr.0.arrarrobj.1.arrkey1', 'arr.1.arrarrobj.0.arrkey1', 'arr.1.arrarrobj.1.arrkey1']
expandPath(object, 'arr.*.arrarrobj.*.["arr key 2"]'); // will output: ['arr.0.arrarrobj.0.["arr key 2"]', 'arr.0.arrarrobj.1.["arr key 2"]', 'arr.1.arrarrobj.0.["arr key 2"]', 'arr.1.arrarrobj["arr key 2"]']));

expandPath(object); // will output: []
expandPath(object, ''); // will output: []
expandPath(object, 'unexistingkey'); // will output: []
expandPath(object, 'unexistingkey.*'); // will output: []
expandPath(object, 'arr.*.unexistingkey'); // will output: ['arr.0.unexistingkey', 'arr.1.unexistingkey']
expandPath(object, 'arr.*.unexistingkey.*'); // will output: []


expandPath(object, 'arr.*.arrarrstr', cb); // will output: ['arr.0.arrarrstr', 'arr.1.arrarrstr']
// cb function will be called twice with the followint arguments:
// First call: cb('arr.0.arrarrstr', ['string 1', 'string 2'])
// Second call: cb('arr.1.arrarrstr', ['string 1', 'string 2'])

updateObjectByPath

updateObjectByPath(object, path, newValue, pathReplacements)

Update the object at a given path with a specified value.

  • object: The object to search for path
  • path: The path to be expanded, accepts .* notation
  • newValue: The new value to be assigned
  • pathReplacements: Every * in path will be replaced by corresponding in pathReplacements array: ('arr.*.arrarrobj.*.arrkey1', [0, 1]) will be assimilated to arr.0.arrarrobj.1.arrkey1

Examples

updateObjectByPath(object, 'arrobj.*.arrobjkey1', 'new value', [1]);

// This statement evals to true
getObjectByPath(object, 'arrobj.*.arrobjkey1', [1]) === 'new value';