propper

1.0.4 • Public • Published

propper

NPM version License

A tool for getting and setting object properties using dot notation, path notation, and arrays to speficy properties.

Installation

npm install propper

Usage

var prop = require('propper')

var obj = {a: 1, b: {c: 'hello'}};

// getters
console.log(prop(obj, 'b.c'));      // 'hello'
console.log(prop(obj, '/b/c'));     // 'hello'
console.log(prop(obj, ['b', 'c'])); // 'hello'

// setters
console.log(prop(obj, 'b.c', 1));      // 1
console.log(prop(obj, '/b/c', 2));     // 2
console.log(prop(obj, ['b', 'c'], 3)); // 3

// setters can also be used to delete keys from an object by passing in undefined as the value
prop(obj, 'b.c', undefined);
console.log(obj); // {a: 1}

// If the deletion of a key would yield, an empty object, then that object will be removed too.

obj = {a: {b: {c: true}}};
prop(obj, 'a.b.c', undefined);
console.log(obj); // {}

ISC

Readme

Keywords

Package Sidebar

Install

npm i propper

Weekly Downloads

85

Version

1.0.4

License

ISC

Unpacked Size

6.81 kB

Total Files

5

Last publish

Collaborators

  • allain