transform-object-dot-notation
Transform object string dot notation into object reference
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
Optional Chaining
Proposal
With 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
.babelrc
(Recommended)
Via .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.
- FIX:
- 0.1.1
- First release
- NEW:
optionalChaining
option
- 0.0.1
- Work in progress
Meta
Author
Marcos Gonçalves – LinkedIn – Website
License
Distributed under the MIT license. Click here for more information.
https://github.com/themgoncalves/transform-object-dot-notation
Contributing
- Fork it (https://github.com/themgoncalves/transform-object-dot-notation/fork)
- Create your feature branch (
git checkout -b feature/fooBar
) - Commit your changes (
git commit -m ':zap: Add some fooBar'
) - Push to the branch (
git push origin feature/fooBar
) - Create a new Pull Request