1.3.0 • Published 5 years ago

@destinationstransfers/object-properties v1.3.0

Weekly downloads
28
License
MIT
Repository
github
Last release
5 years ago

object-properties codecov

Browser / Node (>=8) lightweight, fast and well tested library to check, get and set object properties via dot notation

Motivation

There are bunch of libraries like this on NPM, but most of them either offer too much functionality (array support, a lot of methods), not tree-shaking compatible, has a bad name choice for named import (always do like this import { get as getDeepProperty } from 'dotty' is a pain and error-prone) or little outdated. So, we've created our own to use on our production apps.

Requirements

Library is designed to be browser / tree-shaking compatible, but uses modern ES7 features (Array.isArray, Object.entries, default arguments and array destruction assignment), so, must be polyfilled/transpilled for browser use if target browsers are below that level of language support.

Usage

import { hasDeepProperty, getDeepProperty, setDeepProperty } from '@destinationstransfers/object-properties';

const obj = {};

setDeepProperty(obj, 'a.b.c.d', 'test1');
// obj is now { a: { b: { c: { d: 'test1' }} } }

// also supports Mongoose-like set
setDeepProperty(obj, { e: 1, 'a.b.f': 2 });
// obj is now { a: { b: { c: { d: 'test1' }, f: 2 } }, e: 1 }

// DEPRECATED since TypeScript 3.7/babel - use optional chaining operator
hasDeepProperty(obj, 'a.b.c'); // => true
hasDeepProperty(obj, 'a.d'); // => false

// DEPRECATED since TypeScript 3.7/babel - use optional chaining operator
getDeepProperty(obj, 'a.b.f'); // => 2
getDeepProperty(obj, 'a.b.f.g'); // => undefined

// mutates object in place
deleteDeepProperty(obj, 'a.b.c'); // { a: { b: { f: 2 }}, e: 1 }
deleteDeepProperty(obj, 'a.b.f'); // trims all empty objects way up too -> { e: 1 }

License

MIT

1.3.0

5 years ago

1.2.0

5 years ago

1.1.0

6 years ago

1.0.0

6 years ago