Transform object string dot notation into object reference
const obj = {
foo: {
bar: {
baz: {
val: 'hello world',
},
},
},
};
const val = obj['foo.bar.baz.val'] // hello worldconst obj = {
foo: {
bar: {
baz: class {
},
},
},
};
const baz = new obj['foo.bar.baz'] // baz instanceconst obj = {
foo: {
bar: {
baz: function() {
return 'hello world'
},
},
},
};
const baz = obj['foo.bar.baz']() // hello worldconst obj = {
foo: {
bar: {
baz: 30,
},
},
};
const safe = obj['foo?.quiz?.baz'] // undefinedIn this case, transform-object-dot-notation should be placed before @babel/plugin-proposal-optional-chaining
npm install --save-dev @themgoncalves/transform-object-dot-notationNote: transform-object-dot-notation **should be ** listed in the devDependencies.
.babelrc
{
"plugins": ["@themgoncalves/transform-object-dot-notation"]
}$ babel --plugins @themgoncalves/transform-object-dot-notation script.jsrequire("@babel/core").transform("code", {
plugins: ["@themgoncalves/transform-object-dot-notation"]
});- 0.1.2
- FIX:
optional chainingtransform - DEPRECATE:
optionalChainingoption. Usage no longer requires settings specification.
- FIX:
- 0.1.1
- First release
- NEW:
optionalChainingoption
- 0.0.1
- Work in progress
Marcos Gonçalves – LinkedIn – Website
Distributed under the MIT license. Click here for more information.
https://github.com/themgoncalves/transform-object-dot-notation
- 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