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

deep-getter-setter

v1.0.4

Published

Deep getter and setter for JavaScript. Minimal.

Downloads

5

Readme

deep-getter-setter

1. Introduction

The package deep-getter-setter is a very small but powerful tool.

1.1. What is it for?

Basically:

Understanding that...

  1. a nesteable is any object or array.

...then it is used to...

  1. get deep values from a nesteable.
  2. set deep values to a nesteable.
  3. modify deep values of a nesteable.
  4. check deep values of a nesteable.
  5. ensure deep values of a nesteable.

...or in other words...

  1. From this:

    {a:{b:{c:{d:{e:{f:{g:{h:{i:{j:{k:{l:{m:888}}}}}}}}}}}}}

  2. And this:

    'a b c d e f g h i j k l m'.split(' ')

  3. You can get this:

    888

And from this simple idea, you can:

· get

· set

· modify

· check

· ensure

Note: Pssst!!! All of them are done by the modify method under the hood!

2. Installation

Just:

~$ npm install deep-getter-setter

In Node.js (include literally to compatibilize the examples):

const DeepGetterSetter = require("deep-getter-setter");

In browsers:

<script src="./node_modules/deep-getter-setter/src/deep-getter-setter.js"></script>

3. API comment

For the next examples:

Consider this line included:

var {get,set,modify,exists,ensure} = DeepGetterSetter;

It just decouples the whole API by its properties.

The API

The API to master here:

  1. get (data, selector):

Returns the item value.

  1. set (data, selector, value):

Redefines the item value.

  1. modify (data, selector, modifier):

Modifies the item value through a function.

  1. exists (data, selector, success, error):

Do different things depending on its existence.

  1. ensure (data, selector, value):

Same than set but when value is not provided, it sets a {}.

That is all.

3.1. How does it work?

The 2 common parameters:

  1. The data: nestable object or array with the data to be searched in.

  2. The selector: array with strings for each nested property to be accessed.

The other parameters:

  1. The modifier at modify (data, selector, modifier):

Function that can return whatever in the last moment of the function.

modify(someData, someSelector, function forLastNode (
	value /* any */,
	key /* string */, 
	parent /* object */, 
	index /* integer */,
	selector /* array of strings */
	accumulated /* array */,
	data /* object */,
	) {
	// Our modifier's code.
} 
[, function forEachNode (
	value /* any */,
	key /* string */, 
	parent /* object */, 
	index /* integer */,
	selector /* array of strings */
	accumulated /* array */,
	data /* object */,
) {
	// Our modifiers's iterative (from parents to leaves) code.
}]
  1. The value at set (data, selector, value):

Value (of any type) that is going to set at the selected holder.

set(x, ["a"], 4) // Then x.a === 4
  1. The success and error at exists(data, selector, success, error):

Both are functions, and none obligatory.

By default, it will return true when exists, and false when it does not exist.

exists(x, ["a"]) // Then true if a is in x.

6, The value at ensure (data, selector, value)

When the selected node does not exist, it is created with this provided value.

ensure(x, ["a"]) // Then x.a will be something, or {}.
ensure(x, ["a"], 50) // Then x.a will be something, or 50.

4. Usage

a) Start retrieving the API from the master object:

var {get,set,modify,exists,ensure} = DeepGetterSetter;

b) Test the example (found at test/deep-getter-setter-spec.js):

~$ npm run test

Which will execute some examples that demonstrate what we can expect from the API:


// In Node.js:
const DeepGetterSetter = require("../src/deep-getter-setter.js");

// Retrieve the 3 functions:
var {get,set,modify,exists,ensure} = DeepGetterSetter;

// Some random data:
var data = {a:{b:[0,5,10]}};

// Check that that the getter works okay:
console.log(get(data, ["a","b","1"]) === data["a"]["b"]["1"] ? "Passed!" : "Failed -  getter 1!");

// Use the setter:
set(data, ["a","b","1"], 6);

console.log(get(data, ["a","b","1"]));

// Check that the setter worked good:
console.log(get(data, ["a","b","1"]) === 6 ? "Passed!" : "Failed -  setter 1!");

// Use the modifier:
modify(data, ["a","b","1"], function(parent, key) {
 return ++parent[key];
});

// Check that the modifiers worked fine:
console.log(get(data, ["a","b","1"]) === data["a"]["b"]["1"] ? "Passed!" : "Failed -  modifier 1!");

// Use the exists:
console.log(exists(data, ["a", "b", "5"]) === false ? "Passed!":"Failed -  exists 1!");

// Check that the exists worked fine:
console.log( (!(5 in data["a"]["b"])) ? "Passed!" : "Failed -  exists 2!");

// Use the ensurer:
ensure(data, ["a", "b", "5"]);

// Check that the ensurer worked fine:
console.log(exists(data, ["a", "b", "5"]) === true ? "Passed!" : "Failed -  ensurer 1!");
console.log(typeof (get(data, ["a", "b", "5"])) === "object" ? "Passed!" : "Failed -  ensurer 2!");

It must print everything "Passed!", and nothing "Failed - ...".

5. Conclusion

This is a small tool that you can use in any type of your projects, to easily retrieve and reset values, among other operations.