A small module that expands object keys with dot notation into full objects.
It is a simple recursive wrapper around the dot-object library dot.object
method. It also checks for circular references before attempting to parse.
This was built primarily to parse API nested object queries into proper format for MongoDB, as some versions of MongoDB drivers do not support dot notation in key names.
npm i @thefoot/dot-object-expander --save
const dotObjectExpander = require ( '@thefoot/dot-object-expander' );
console.log( dotObjectExpander ( {
string : 'bar',
int : 123,
float : 1.234,
boolean : true,
'obj.foo' : {
'bar.baz': 123
},
'org.user.type': {
admin: {
'has.permissions.admin': true
},
ary : [ 1, 2, 3 ]
}
} ) );
// Result:
{
"string": "bar",
"int": 123,
"float": 1.234,
"boolean": true,
"obj": {
"foo": {
"bar": {
"baz": 123
}
}
},
"org": {
"user": {
"type": {
"admin": {
"has": {
"permissions": {
"admin": true
}
}
},
"ary": [
1,
2,
3
]
}
}
}
}
ES Module-aware environments such as Webpack and Rollup bundlers should automatically use the dist/parser.esm.js
export.
import dotObjectExpander from '@thefoot/dot-object-expander';
dotObjectExpander({
'org.user.type': {
admin: {
'has.permissions.admin': true
},
ary : [ 1, 2, 3 ]
}
});
<script src="https://unpkg.com/dot-object-expander/dist/parser.js"></script>
<script>
var parsed = dotObjectExpander({
'org.user.type': {
admin: {
'has.permissions.admin': true
},
ary : [ 1, 2, 3 ]
}
});
// ...
</script>
<script src="https://unpkg.com/dot-object-expander/dist/parser.min.js"></script>
<script>
var parsed = dotObjectExpander({
'org.user.type': {
admin: {
'has.permissions.admin': true
},
ary : [ 1, 2, 3 ]
}
});
// ...
</script>
Contributions welcome, please read CONTRIBUTING and CODING-STANDARDS.
Rob Halff for his dot-object library.
- .