propperties

1.0.1 • Public • Published

propper

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); // {}

Readme

Keywords

Package Sidebar

Install

npm i propperties

Weekly Downloads

2

Version

1.0.1

License

ISC

Unpacked Size

2.62 kB

Total Files

3

Last publish

Collaborators

  • gk803