Skip to content

Commit

Permalink
objectset.deepValue added
Browse files Browse the repository at this point in the history
  • Loading branch information
Afsin Ustundag committed Mar 4, 2015
1 parent 533f635 commit 1c9453f
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 1 deletion.
4 changes: 4 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# v1.4.1 - TBD

-objectset.deepValue(obj, deepProperty, value)

# v1.4.0 - February 27, 2015

This is the initial release of blue-button-util.js library. It includes javascript utility methods.
Expand Down
19 changes: 19 additions & 0 deletions lib/objectset.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,22 @@ exports.compact = function compact(obj) {
};

exports.merge = lodash.merge;

exports.deepValue = function (obj, deepProperty, value) {
if ((!object.exists(obj)) || (typeof obj !== 'object')) {
return null;
}
var currentObj = obj;
var propertyPieces = deepProperty.split('.');
var lastIndex = propertyPieces.length - 1;
for (var i = 0; i < lastIndex; ++i) {
var propertyPiece = propertyPieces[i];
var nextObj = currentObj[propertyPiece];
if ((!object.exists(nextObj)) || (typeof nextObj !== 'object')) {
currentObj[propertyPiece] = nextObj = {};
}
currentObj = nextObj;
}
currentObj[propertyPieces[lastIndex]] = value;
return obj;
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "blue-button-util",
"version": "1.4.0",
"version": "1.4.1",
"description": "Common methods for Amida-Tech repos",
"main": "./index.js",
"directories": {
Expand Down
106 changes: 106 additions & 0 deletions test/test-objectset.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,109 @@ describe('objectset.compact', function () {
expect(r.inh).to.not.exist();
});
});

describe('objsectset.deepValue', function () {
it('null input', function () {
var r = objectset.deepValue(null, 'primary.secondory', 5);
expect(r).to.equal(null);
});

it('not object input', function () {
var r = objectset.deepValue(null, 'primary.secondary', "value");
expect(r).to.equal(null);
});

it('undefined input', function () {
var r = objectset.deepValue(null, 'primary.secondary', undefined);
expect(r).to.equal(null);
});

it('basic', function () {
var obj = {};
var r = objectset.deepValue(obj, 'a.b', 1);
expect(r).to.equal(obj);
expect(obj).to.deep.equal({
a: {
b: 1
}
});
r = objectset.deepValue(obj, 'a.c.d', {
d: 1
});
expect(r).to.equal(obj);
expect(obj).to.deep.equal({
a: {
b: 1,
c: {
d: {
d: 1
}
}
}
});
r = objectset.deepValue(obj, 'e', "");
expect(r).to.equal(obj);
expect(obj).to.deep.equal({
a: {
b: 1,
c: {
d: {
d: 1
}
}
},
e: ""
});
r = objectset.deepValue(obj, 'a.b.e', "string");
expect(r).to.equal(obj);
expect(obj).to.deep.equal({
a: {
b: {
e: "string"
},
c: {
d: {
d: 1
}
}
},
e: ""
});
r = objectset.deepValue(obj, 'a.b.f', 88);
expect(r).to.equal(obj);
expect(obj).to.deep.equal({
a: {
b: {
e: "string",
f: 88
},
c: {
d: {
d: 1
}
}
},
e: ""
});
r = objectset.deepValue(obj, 'a.b.e', {
e: 1
});
expect(r).to.equal(obj);
expect(obj).to.deep.equal({
a: {
b: {
e: {
e: 1
},
f: 88
},
c: {
d: {
d: 1
}
}
},
e: ""
});
});
});

0 comments on commit 1c9453f

Please sign in to comment.