Skip to content

borvelt/object-maker

Repository files navigation

object-maker

create object with string and map string to object

Installation

$ npm install --save object-maker

Usage

Create Object

If you want to generate deep complex object dynamically, you should use this library

import objectMaker from 'object-maker'
import merge from 'lodash.merge'

const info1 = objectMaker('package.author.email', '[email protected]')
const info2 = objectMaker('package.author.gitRepo', 'https://github.com/borvelt')
merge(info1, info2)

// merged object
// Object{
//   package: {
//     author: {
//       email: "[email protected]",
//       gitRepo: "https://github.com/borvelt",
//     }
//   }
// }

Map string to object

When you have a deep complex object and you need to access the deepest part it's good solution to make string and get equivalent of mapped string to object. see this example:

import {mapStringToObject} from 'object-maker'
const info = {
  package: {
      author: {
        email: "[email protected]",
        gitRepo: "https://github.com/borvelt",
      }
  }
}
console.log(mapStringToObject('package.author.gitRepo', info))
// -> https://github.com/borvelt

Careful mapStringToObject works case-sensitive.

Test

run npm test it will pass all test cases.

license

MIT

About

Create object with dotted string

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published