@thefoot/dot-object-expander

1.0.2 • Public • Published

Dot Object Expander

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.

CI Build GitHub release (latest by date) npm (scoped)

Installation

npm i @thefoot/dot-object-expander --save

Usage

NodeJS

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

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 ]
    }
});

Browser

Unminified with comments

<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>

Minified with source map

<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>

Contributing

Contributions welcome, please read CONTRIBUTING and CODING-STANDARDS.

Credits

Rob Halff for his dot-object library.

Author

Contributors

  • .

Licence

MIT

Package Sidebar

Install

npm i @thefoot/dot-object-expander

Weekly Downloads

20

Version

1.0.2

License

MIT

Unpacked Size

142 kB

Total Files

23

Last publish

Collaborators

  • thefoot