@themgoncalves/transform-object-dot-notation

0.1.2 • Public • Published

transform-object-dot-notation

Transform object string dot notation into object reference

NPM CircleCI GitHub All Releases GitHub stars Known Vulnerabilities GitHub issues Awesome

Example

Accessing deeply nested properties

const obj = {
  foo: {
    bar: {
      baz: {
        val: 'hello world',
      },
    },
  },
};

const val = obj['foo.bar.baz.val']  // hello world

Calling deeply nested functions

const obj = {
  foo: {
    bar: {
      baz: class {
      },
    },
  },
};

const baz = new obj['foo.bar.baz'] // baz instance

Constructing deeply nested classes

const obj = {
  foo: {
    bar: {
      baz: function() {
        return 'hello world'
      },
    },
  },
};

const baz = obj['foo.bar.baz']() // hello world

With Optional Chaining Proposal

const obj = {
  foo: {
    bar: {
      baz: 30,
    },
  },
};

const safe = obj['foo?.quiz?.baz'] // undefined

In this case, transform-object-dot-notation should be placed before @babel/plugin-proposal-optional-chaining


Installation

npm install --save-dev @themgoncalves/transform-object-dot-notation

Note: transform-object-dot-notation **should be ** listed in the devDependencies.


Usage

Via .babelrc (Recommended)

.babelrc

{
 "plugins": ["@themgoncalves/transform-object-dot-notation"]
}

Via CLI

$ babel --plugins @themgoncalves/transform-object-dot-notation script.js

Via Node API

require("@babel/core").transform("code", {
  plugins: ["@themgoncalves/transform-object-dot-notation"]
});

Release History

  • 0.1.2
    • FIX: optional chaining transform
    • DEPRECATE: optionalChaining option. Usage no longer requires settings specification.
  • 0.1.1
    • First release
    • NEW: optionalChaining option
  • 0.0.1
    • Work in progress

Meta

Author

Marcos GonçalvesLinkedInWebsite

License

Distributed under the MIT license. Click here for more information.

https://github.com/themgoncalves/transform-object-dot-notation

Contributing

  1. Fork it (https://github.com/themgoncalves/transform-object-dot-notation/fork)
  2. Create your feature branch (git checkout -b feature/fooBar)
  3. Commit your changes (git commit -m ':zap: Add some fooBar')
  4. Push to the branch (git push origin feature/fooBar)
  5. Create a new Pull Request

Package Sidebar

Install

npm i @themgoncalves/transform-object-dot-notation

Weekly Downloads

1

Version

0.1.2

License

MIT

Unpacked Size

14.4 kB

Total Files

17

Last publish

Collaborators

  • themgoncalves