dotject - Quick JS Object Composition

dotject

dotject provides a quick and simple way to compose JavaScript objects using familiar dot notation.

The code supports object creation at n levels (nesting) along with same level objects. There’s also functionality to support (multiple) value assignment and value overriding. It might sound complicated but it’s designed to make life easier, the examples below will showcase this.

dotject harnesses the power of functional.js and it’s λ.reduce function to achieve the “dot notation to object” implementation in very few lines of code.

Getting started

Install via npm:

1
npm install dotject # --save

Require the module in the usual way:

1
var dotject = require("dotject");

Using dotject

There is a comprehensive list of programatic examples in the specs, for the project, but I’ll run through examples here:

Simple object composition

Separating property names with dots (like JavaScript dot notation) will create nested objects:

1
2
3
4
5
6
7
8
9
dotject("a.b.c");
{
"a": {
"b": {
"c": {}
}
}
}

Separating property names with a comma (similar to JavaScript objects) will create properties at the same level:

1
2
3
4
5
6
dotject("a,b");
{
"a": {},
"b": {}
}

This notation can be combined with the dot style notation:

1
2
3
4
5
6
7
8
dotject("a,b.c");
{
"a": {},
"b": {
"c": {}
}
}

Extend an existing object

Extend an existing object with new properties by passing the existing object as a second (optional) argument:

1
2
3
4
5
6
7
8
dotject("b.c", {"a":{}});
{
"a": {},
"b": {
"c": {}
}
}

Existing object values are retained:

1
2
3
4
5
6
7
8
dotject("b.c", {"a": true});
{
"a": true,
"b": {
"c": {}
}
}

Existing nested object properties are retained:

1
2
3
4
5
6
7
8
dotject("a.c", {"a":{"b":{}}});
{
"a": {
"b": {},
"c": {}
}
}

Assign a value

You can assign a value to the last nested property:

1
2
3
4
5
6
7
dotject("a.b", {}, 10);
{
"a": {
"b": 10
}
}

Assign multiple values

You can assign a values to the last nested properties:

1
2
3
4
5
6
7
8
9
dotject("a,b.c,d", {}, true, 10, "test");
{
"a": true,
"b": {
"c": 10
},
"d": "test"
}

Assign multiple values and override existing

You can assign a values to the last nested properties:

1
2
3
4
5
6
dotject("a,b", {"a": false}, true, "test")
{
"a": true,
"b": "test"
}

“Real world” example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var camera = {isEnabled: true};
camera = dotject("settings.encoding.format,output.data.base64", camera, "JPEG", true);
{
"isEnabled": true,
"settings": {
"encoding": {
"format": "JPEG"
}
},
"output": {
"data": {
"base64": true
}
}
}

Other thoughts

I did consider not implementing the ability to extend existing objects, as this would provide cleaner usage and the base library (functional.js) does already have support for this with the λ.assign (λ.extend) function. I’d be interested to hear your feedback on this, as personally I’m almost always passing a new object, which seems silly.

Issues and pull requests

Please raise any issues / pull requests directly on github.