Skip to content
This repository has been archived by the owner on Dec 19, 2023. It is now read-only.

Latest commit

 

History

History
47 lines (33 loc) · 1.18 KB

README.md

File metadata and controls

47 lines (33 loc) · 1.18 KB

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