Skip to content

Commit

Permalink
improve API doc, rename new function to setIn
Browse files Browse the repository at this point in the history
  • Loading branch information
dfsp-spirit committed Mar 6, 2017
1 parent 904b3f3 commit ffebc55
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
30 changes: 16 additions & 14 deletions object_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ ObjectUtil._stringRepresentsPositiveIntegerIncludingZero = function(str) {

/**
* Checks whether a nested object key exists and is not null or undefined.
* @param obj the JSON object in which to check for the nested key
* @param path_components an array of strings defining the path (of key names) to the key in question
* @returns {boolean} whether the key exists and is neither undefined nor null
* @param obj The JSON object in which to check for the nested key
* @param path_components An array of strings defining the path (of key names) to the key in question
* @returns {boolean} Whether the key exists and is neither undefined nor null
*/
ObjectUtil.hasDefinedAndNonNullIn = function(obj, path_components) {
if(ObjectUtil.hasIn(obj, path_components)) {
Expand All @@ -95,10 +95,10 @@ ObjectUtil.hasDefinedAndNonNullIn = function(obj, path_components) {

/**
* Retrieves the nested obj from the given one. Note that the path has to exist, Check first with hasIn if in doubt.
* @param obj the input JSON object
* @param path_components the path to the nested object as an array of strings
* @param alternate an alternate return value that will be used if the given path does not exist in the object
* @returns {Object} the nested object
* @param obj The input JSON object
* @param path_components The path to the nested object as an array of strings
* @param alternate An alternate return value that will be used if the given path does not exist in the object
* @returns {Object} The nested object
*/
ObjectUtil.getIn = function(obj, path_components, alternate) {
if(!ObjectUtil.hasIn(obj, path_components)) {
Expand All @@ -119,13 +119,15 @@ ObjectUtil.getIn = function(obj, path_components, alternate) {

/**
* Creates a new object based on the old one that has the given path added.
* @param obj the input JSON object
* @param path_components the path to the nested object as an array of strings and/or numbers. You have to use numbers (integers >= 0 to be more precise) if you want to set array values.
* @param value_to_use_for_leaf the value to set for the final path element
* @param value_to_use_for_missing_array_elements a value to push into arrays to fill them to the required length, if needed. Optional, defaults to undefined.
* @returns {Object} the new JSON object, which includes the requested path
* @param obj The input JSON object
* @param path_components The path to the nested object as an array of strings and/or numbers. Numbers are treated as array indices.
If you set an array index n while n-1 does not exist, this function will push values into the array to make the index word.
You can determine what will be pushed by using the last, optional parameter of this function. If you omit it, undefined will be used.
* @param value The target value to set for the final path element.
* @param value_to_use_for_missing_array_elements A value to push into arrays to fill them to the required length, if needed. Optional, defaults to undefined.
* @returns {Object} The new JSON object, which includes the requested path.
**/
ObjectUtil.addPathIn = function(obj, path_components, value_to_use_for_leaf, value_to_use_for_missing_array_elements) {
ObjectUtil.setIn = function(obj, path_components, value, value_to_use_for_missing_array_elements) {

var obj_cp = Object.assign({}, obj);

Expand Down Expand Up @@ -170,7 +172,7 @@ ObjectUtil.addPathIn = function(obj, path_components, value_to_use_for_leaf, val
}
sub_obj[path_component] = {};
if(i === (path_components.length - 1)) {
sub_obj[path_component] = value_to_use_for_leaf;
sub_obj[path_component] = value;
}
}
}
Expand Down
24 changes: 12 additions & 12 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,10 @@ describe('Check alternate return value of getIn', function () {
});
});

describe('Check createPathIn functionality', function () {
describe('Check setIn functionality', function () {
it('properly creates a path in an object', function () {
expect(ObjectUtil.hasIn(empty_book, ['publisher', 'address', 'city'])).toBe(false);
var new_book = ObjectUtil.addPathIn(empty_book, ['publisher', 'address', 'city'], {});
var new_book = ObjectUtil.setIn(empty_book, ['publisher', 'address', 'city'], {});
var expected_book = {
publisher: {
address: {
Expand All @@ -199,7 +199,7 @@ describe('Check createPathIn functionality', function () {

it('properly creates a path in an object and does not mess with existing paths', function () {
expect(ObjectUtil.hasIn(almost_empty_book, ['publisher', 'address', 'city'])).toBe(false);
var new_book = ObjectUtil.addPathIn(almost_empty_book, ['publisher', 'address', 'city'], {});
var new_book = ObjectUtil.setIn(almost_empty_book, ['publisher', 'address', 'city'], {});
var expected_book = {
publisher: {
address: {
Expand All @@ -215,7 +215,7 @@ describe('Check createPathIn functionality', function () {

it('properly handles null input objects', function () {

var new_book = ObjectUtil.addPathIn(null, ['publisher', 'address', 'city'], {});
var new_book = ObjectUtil.setIn(null, ['publisher', 'address', 'city'], {});
var expected_book = {
publisher: {
address: {
Expand All @@ -230,7 +230,7 @@ describe('Check createPathIn functionality', function () {

it('properly handles undefined input objects', function () {

var new_book = ObjectUtil.addPathIn(undefined, ['publisher', 'address', 'city'], {});
var new_book = ObjectUtil.setIn(undefined, ['publisher', 'address', 'city'], {});
var expected_book = {
publisher: {
address: {
Expand All @@ -245,7 +245,7 @@ describe('Check createPathIn functionality', function () {

it('properly handles arrays which do not exist in the input objects', function () {

var new_book = ObjectUtil.addPathIn(almost_empty_book, ['readers', 3, 'address'], {});
var new_book = ObjectUtil.setIn(almost_empty_book, ['readers', 3, 'address'], {});
var expected_book = {
readers: [
undefined,
Expand All @@ -264,7 +264,7 @@ describe('Check createPathIn functionality', function () {

it('properly handles arrays which do not exist in the input objects and uses the given value for missing elements', function () {

var new_book = ObjectUtil.addPathIn(almost_empty_book, ['readers', 3, 'address'], {}, null);
var new_book = ObjectUtil.setIn(almost_empty_book, ['readers', 3, 'address'], {}, null);
var expected_book = {
readers: [
null,
Expand All @@ -282,13 +282,13 @@ describe('Check createPathIn functionality', function () {
});

it('properly handles paths which already exist', function () {
var new_book = ObjectUtil.addPathIn(almost_empty_book, ['is_cc_licensed'], true);
var new_book = ObjectUtil.setIn(almost_empty_book, ['is_cc_licensed'], true);
expect(ObjectUtil.hasIn(new_book, ['is_cc_licensed'])).toBe(true);
expect(new_book).toEqual(almost_empty_book);
});

it('can cope with arrays at the root', function () {
var new_book = ObjectUtil.addPathIn(empty_book, [1, 'is_cc_licensed'], {});
var new_book = ObjectUtil.setIn(empty_book, [1, 'is_cc_licensed'], {});
expect(ObjectUtil.hasIn(new_book, [1, 'is_cc_licensed'])).toBe(true);
var expected_book = [
undefined,
Expand All @@ -301,7 +301,7 @@ describe('Check createPathIn functionality', function () {
});

it('overwrites other values with arrays if required by the new path', function () {
var new_book = ObjectUtil.addPathIn(almost_empty_book, ['is_cc_licensed', 1, 'age'], {});
var new_book = ObjectUtil.setIn(almost_empty_book, ['is_cc_licensed', 1, 'age'], {});
expect(ObjectUtil.hasIn(new_book, ['is_cc_licensed', 1, 'age'])).toBe(true);
var expected_book = {
is_cc_licensed: [
Expand All @@ -316,8 +316,8 @@ describe('Check createPathIn functionality', function () {
});

it('does not erase existing array data', function () {
var new_book = ObjectUtil.addPathIn(book, ['readers', 2, 'age'], 14);
var new_book = ObjectUtil.addPathIn(book, ['readers', 2, 'sex'], 'somethinginbetween');
var new_book = ObjectUtil.setIn(book, ['readers', 2, 'age'], 14);
var new_book = ObjectUtil.setIn(book, ['readers', 2, 'sex'], 'somethinginbetween');
expect(ObjectUtil.getIn(new_book, ['readers', 1, 'name'])).toBe('Brad');
expect(ObjectUtil.getIn(new_book, ['readers', 2, 'sex'])).toBe('somethinginbetween');

Expand Down

0 comments on commit ffebc55

Please sign in to comment.