1.0.0 • Published 4 years ago

@thefootonline/dot-object-expander v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

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

Installation

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

Usage

NodeJS

const dotObjectExpander = require ( '../src/parser' );

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 '@thefootonline/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

1.0.0

4 years ago

0.1.0

4 years ago