diff --git a/package.json b/package.json index 931c25d..2975df6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dotize", - "version": "0.1.27", + "version": "0.1.28", "description": "Convert complex Js object to dot notation Js object", "main": "src/dotize.js", "directories": { diff --git a/src/dotize.js b/src/dotize.js index cf26e65..e629bd8 100644 --- a/src/dotize.js +++ b/src/dotize.js @@ -1,4 +1,7 @@ -// https://github.com/vardars/dotize +// Convert complex js object to dot notation js object +// url: https://github.com/vardars/dotize +// author: vardars + var dotize = dotize || {}; dotize.convert = function(obj, prefix) { @@ -27,7 +30,7 @@ dotize.convert = function(obj, prefix) { } function isEmptyArray(o) { - if (Array.isArray(o) && o.length == 0) + if (Array.isArray(o) && o.length === 0) return true; return false; } @@ -41,36 +44,39 @@ dotize.convert = function(obj, prefix) { return (prefix ? prefix + "." : "") + field; } - return function recurse(o, p, isArrayItem, isRoot) { + return function recurse(o, p, isRoot) { + var isArrayItem = Array.isArray(o); for (var f in o) { - var currentProp = o[f]; - if (currentProp && typeof currentProp === "object") { - if (Array.isArray(currentProp)) { - if (isEmptyArray(currentProp)) { - newObj[getFieldName(f, p, isRoot, true)] = currentProp; // empty array - } else { - newObj = recurse(currentProp, getFieldName(f, p, isRoot, false, true), true); // array - } - } else { - if (isArrayItem) { - if (isEmptyObj(currentProp)) { - newObj[getFieldName(f, p, isRoot, true)] = currentProp; // empty object + if (o.hasOwnProperty(f)) { + var currentProp = o[f]; + if (currentProp && typeof currentProp === "object") { + if (Array.isArray(currentProp)) { + if (isEmptyArray(currentProp)) { + newObj[getFieldName(f, p, isRoot, true)] = currentProp; // empty array } else { - newObj = recurse(currentProp, getFieldName(f, p, isRoot, true)); // array item object + newObj = recurse(currentProp, getFieldName(f, p, isRoot, false, true), isArrayItem); // array } } else { - if (isEmptyObj(currentProp)) { - newObj[getFieldName(f, p, isRoot)] = currentProp; // empty object + if (isArrayItem) { + if (isEmptyObj(currentProp)) { + newObj[getFieldName(f, p, isRoot, true)] = currentProp; // empty object + } else { + newObj = recurse(currentProp, getFieldName(f, p, isRoot, true)); // array item object + } } else { - newObj = recurse(currentProp, getFieldName(f, p, isRoot)); // object + if (isEmptyObj(currentProp)) { + newObj[getFieldName(f, p, isRoot)] = currentProp; // empty object + } else { + newObj = recurse(currentProp, getFieldName(f, p, isRoot)); // object + } } } - } - } else { - if (isArrayItem || isNumber(f)) { - newObj[getFieldName(f, p, isRoot, true)] = currentProp; // array item primitive } else { - newObj[getFieldName(f, p, isRoot)] = currentProp; // primitive + if (isArrayItem || isNumber(f)) { + newObj[getFieldName(f, p, isRoot, true)] = currentProp; // array item primitive + } else { + newObj[getFieldName(f, p, isRoot)] = currentProp; // primitive + } } } } @@ -79,9 +85,9 @@ dotize.convert = function(obj, prefix) { return obj; return newObj; - }(obj, prefix, Array.isArray(obj), true); + }(obj, prefix, true); }; if (typeof module != "undefined") { module.exports = dotize; -} +} \ No newline at end of file diff --git a/test/test.js b/test/test.js index d7361c7..75508f9 100644 --- a/test/test.js +++ b/test/test.js @@ -2,247 +2,247 @@ var assert = require("assert"); var dotize = require("../src/dotize.js"); var testArray = [ - { - "name": "primitive", - "tests": [ - { - "name": "number", - "source": 1, - "target": 1 - }, - { - "name": "string", - "source": "foo", - "target": "foo" - }, - { - "name": "boolean", - "source": true, - "target": true - } - ] - }, - { - "name": "primitive with prefix", - "tests": [ - { - "name": "number", - "prefix": "foo", - "source": 1, - "target": { "foo": 1 } - }, - { - "name": "string", - "prefix": "foo", - "source": "foo", - "target": { "foo": "foo" } - }, - { - "name": "boolean", - "prefix": "foo", - "source": true, - "target": { "foo": true } - } - ] - }, - { - "name": "null", - "tests": [ - { - "name" : "without prefix", - "source": null, - "target": null - }, - { - "name" : "with prefix", - "prefix": "foo", - "source": null, - "target": { "foo": null } - } - ] - }, - { - "name": "object", - "tests": [ - { - "name" : "basic", - "source": { - "a": 1 - }, - "target": { - "a": 1 - } - }, - { - "name" : "complex with child", - "source": { - "a": 1, - "b": { - "b2": 1 - } - }, - "target": { - "a": 1, - "b.b2": 1 - } - }, - { - "name" : "complex with Array", - "source": { - "a": 1, - "b": { - "b2": [1] - } - }, - "target": { - "a": 1, - "b.b2[0]": 1 - } - } - ] - }, - { - "name": "object with prefix", - "tests": [ - { - "name" : "basic", - "prefix": "foo", - "source": { - "a": 1 - }, - "target": { - "foo.a": 1 - } - }, - { - "name" : "complex with child", - "prefix": "foo", - "source": { - "a": 1, - "b": { - "b2": 1 - } - }, - "target": { - "foo.a": 1, - "foo.b.b2": 1 - } - }, - { - "name" : "complex with Array", - "prefix": "foo", - "source": { - "a": 1, - "b": { - "b2": [1] - } - }, - "target": { - "foo.a": 1, - "foo.b.b2[0]": 1 - } - } - ] - }, - { - "name": "Array", - "tests": [ - { - "name" : "empty", - "source": [], - "target": [] - }, - { - "name" : "basic", - "source": [1], - "target": { - "[0]": 1 - } - }, - { - "name" : "with prefix", - "prefix": "foo", - "source": { - "a": [1] - }, - "target": { - "foo.a[0]": 1 - } - }, - { - "name" : "Array of Arrays", - "prefix": "foo", - "source": [[1]], - "target": { - "foo[0][0]": 1, - } - } - ] - }, - { - "name": "issues", - "tests": [ - { - "name": "#6 - weird array", - "source": [ - { - "foo": "bar" - }, - 0, - null, - { - "null": null, - "array": [ - {}, - [] - ], - } - ], - "target": { - "[0].foo": "bar", - "[1]": 0, - "[2]": null, - "[3].null": null, - "[3].array[0]": {}, - "[3].array[1]": [], - } - }, - { - "name": "#10 - Keys prefixed with dot when used with arrays", - "source": { - "data": [ - { - "obj0key": "obj0val" + { + "name": "primitive", + "tests": [ + { + "name": "number", + "source": 1, + "target": 1 + }, + { + "name": "string", + "source": "foo", + "target": "foo" + }, + { + "name": "boolean", + "source": true, + "target": true + } + ] + }, + { + "name": "primitive with prefix", + "tests": [ + { + "name": "number", + "prefix": "foo", + "source": 1, + "target": { "foo": 1 } + }, + { + "name": "string", + "prefix": "foo", + "source": "foo", + "target": { "foo": "foo" } + }, + { + "name": "boolean", + "prefix": "foo", + "source": true, + "target": { "foo": true } + } + ] + }, + { + "name": "null", + "tests": [ + { + "name" : "without prefix", + "source": null, + "target": null + }, + { + "name" : "with prefix", + "prefix": "foo", + "source": null, + "target": { "foo": null } + } + ] + }, + { + "name": "object", + "tests": [ + { + "name" : "basic", + "source": { + "a": 1 + }, + "target": { + "a": 1 + } + }, + { + "name" : "complex with child", + "source": { + "a": 1, + "b": { + "b2": 1 + } + }, + "target": { + "a": 1, + "b.b2": 1 + } + }, + { + "name" : "complex with Array", + "source": { + "a": 1, + "b": { + "b2": [1] + } + }, + "target": { + "a": 1, + "b.b2[0]": 1 + } + } + ] + }, + { + "name": "object with prefix", + "tests": [ + { + "name" : "basic", + "prefix": "foo", + "source": { + "a": 1 + }, + "target": { + "foo.a": 1 + } + }, + { + "name" : "complex with child", + "prefix": "foo", + "source": { + "a": 1, + "b": { + "b2": 1 + } + }, + "target": { + "foo.a": 1, + "foo.b.b2": 1 + } + }, + { + "name" : "complex with Array", + "prefix": "foo", + "source": { + "a": 1, + "b": { + "b2": [1] + } + }, + "target": { + "foo.a": 1, + "foo.b.b2[0]": 1 + } + } + ] + }, + { + "name": "Array", + "tests": [ + { + "name" : "empty", + "source": [], + "target": [] + }, + { + "name" : "basic", + "source": [1], + "target": { + "[0]": 1 + } + }, + { + "name" : "with prefix", + "prefix": "foo", + "source": { + "a": [1] + }, + "target": { + "foo.a[0]": 1 + } + }, + { + "name" : "Array of Arrays", + "prefix": "foo", + "source": [[1]], + "target": { + "foo[0][0]": 1, + } + } + ] + }, + { + "name": "issues", + "tests": [ + { + "name": "#6 - weird array", + "source": [ + { + "foo": "bar" + }, + 0, + null, + { + "null": null, + "array": [ + {}, + [] + ], + } + ], + "target": { + "[0].foo": "bar", + "[1]": 0, + "[2]": null, + "[3].null": null, + "[3].array[0]": {}, + "[3].array[1]": [], + } }, { - "obj1key": "obj1val" + "name": "#10 - Keys prefixed with dot when used with arrays", + "source": { + "data": [ + { + "obj0key": "obj0val" + }, + { + "obj1key": "obj1val" + } + ] + }, + "target": { + "data[0].obj0key": "obj0val", + "data[1].obj1key": "obj1val" + } } - ] - }, - "target": { - "data[0].obj0key": "obj0val", - "data[1].obj1key": "obj1val" - } - } - ] - }, + ] + }, ]; for (var i = 0; i < testArray.length; i++) { - var testGroup = testArray[i]; + var testGroup = testArray[i]; - describe(testGroup.name, function() { - for (var j = 0; j < testGroup.tests.length; j++) { - var testItem = testGroup.tests[j]; + describe(testGroup.name, function() { + for (var j = 0; j < testGroup.tests.length; j++) { + var testItem = testGroup.tests[j]; - it(testItem.name, function () { - var result = null; - if (testItem.prefix) - result = dotize.convert(testItem.source, testItem.prefix); - else - result = dotize.convert(testItem.source); - assert.deepEqual(testItem.target, result); - }); - }; - }); + it(testItem.name, function () { + var result = null; + if (testItem.prefix) + result = dotize.convert(testItem.source, testItem.prefix); + else + result = dotize.convert(testItem.source); + assert.deepEqual(testItem.target, result); + }); + }; + }); };