Coder Social home page Coder Social logo

gkmr507 / object-dot Goto Github PK

View Code? Open in Web Editor NEW

This project forked from 418sec/object-dot

0.0 0.0 0.0 284 KB

Easily use dot notation to safely get, or set a property of a nested object. A Node module.

License: ISC License

JavaScript 100.00%

object-dot's Introduction

๐Ÿ“ฆ object-dot

CircleCI codecov npm

Easily use dot notation to get, or set a property of a nested object. A node module.

๐Ÿ’ฅ 100% code coverage, zero dependencies and flexible APIs.

Usage

set

Create the nested chain of objects with set and dot notation with one simple statement.

const objectd = require('object-dot')

console.log(
  objectd.set({ object: {}, path: 'a.b.c', value: 'foo' })
)
// => { a: { b: { c: 'foo' } } }

// Array of the property chain will work too!
console.log(
  objectd.set({ object: {}, path: ['a', 'b', 'c'], value: 'foo' })
)

// Alternatively you may use arguments as parameters instead of an object.
console.log(
  objectd.set({}, 'a.b.c', 'foo')
)

By default any property with values that exists in the path of the chained property is overwritten. The API can be instructed to not overwrite but instead do nothing when this scenario is found. Consider the following:

require('object-dot').extend()
let obj = { a: { b: { c: 'foo' } } }

// By default, the value for `c` is overwritten
console.log(
  Object.set(obj, 'a.b.c.d', 'foo')
)
//=> { a: { b: { c: { d: 'foo' } } } }

// Tell the API not to overwrite
console.log(
  Object.set(obj, 'a.b.c.d', 'foo', false)
)
//=> { a: { b: { c: 'foo' } } }

// Alternatively, you may call it with an object as a parameter
Object.set({ object: obj, path: 'a.b.c.d', value: 'foo', overwrite: false })

get

Get the value of a nested chain of objects without checking each object in the chain for its existence.

const objectd = require('object-dot')

// when one of the properties in the chain is undefined. Safely return undefined.
let object = { foo: { bar: 'you!' }}
console.log(
  objectd.get({ object, path: 'foo.bar.c.d'})
)
//=> undefined

// return a default value if property is undefined
object = { foo: { bar: 'you!' }}
console.log(
  objectd.get({ object, path: 'foo.bar.c.d', value: 'my default value'})
)
//=> 'my default value'

// When the property exist.
object = { a: { foo: { bar: 'you!' } }}
console.log(
  objectd.get({ object, path: 'a.foo'})
)
//=> { bar: 'you!' }

// Plain arguments as parameters will work too!
console.log(
  objectd.get(object, 'a.foo.bar')
)
// => 'you!'

// Using arrays instead of dot notation is also supported.
console.log(
  objectd.get(object, ['a', 'foo', 'bar'])
)
// => 'you!'

exists

The exists method determines if a chained object exist.

const objectd = require('object-dot')

// when one of the properties in the chain is undefined. Safely return undefined.
let object = { foo: { bar: 'you!' }}
console.log(
  objectd.exists({ object, path: 'foo.bar'})
)
//=> true

// alternatively using plain old arguments work too.
console.log(
  objectd.exists(object, 'foo.bar')
)

extend

Use the extend method to add the methods, get, set and exists to the Object prototype chain:

require('object-dot').extend()

let object = { foo: { bar: 'you!' }}
console.log(
  Object.exists(object, 'foo.bar')
)
//=> true

Install

$ npm install object-dot --save

License

ISC

object-dot's People

Contributors

jusx avatar semantic-release-bot avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.