From 8ae0d97cf5c92a7d2b74c8b77203cf48b0b348a6 Mon Sep 17 00:00:00 2001 From: vardars Date: Sat, 17 Dec 2016 01:47:26 +0300 Subject: [PATCH] refactor according to coverage analysis, added tests, versioning --- .gitignore | 3 +- .travis.yml | 5 +- bower.json | 2 +- package.json | 2 +- src/dotize.js | 53 ++--- test/test-results.txt | 9 +- test/test.js | 541 +++++++++++++++++++++++------------------- 7 files changed, 328 insertions(+), 287 deletions(-) diff --git a/.gitignore b/.gitignore index 4a688ff..66c0ee0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules/ -bower_components/ \ No newline at end of file +bower_components/ +coverage/ \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 46c6849..9d65acc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,4 @@ before_script: - npm install -g mocha script: mocha test/test.js after_success: - - npm install istanbul -g - - istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec - - npm install -g codecov - - codecov + - bash <(curl -s https://codecov.io/bash) diff --git a/bower.json b/bower.json index e40dfa5..4586b25 100644 --- a/bower.json +++ b/bower.json @@ -7,7 +7,7 @@ "dotize" ], "authors": [ - "Vardar " + "Suleyman Vardar " ], "license": "MIT", "ignore": [ diff --git a/package.json b/package.json index 2975df6..48b8ba4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dotize", - "version": "0.1.28", + "version": "0.2.0", "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 e629bd8..1f5c673 100644 --- a/src/dotize.js +++ b/src/dotize.js @@ -25,14 +25,6 @@ dotize.convert = function(obj, prefix) { if (Object.hasOwnProperty.call(obj, prop)) return false; } - - return true; - } - - function isEmptyArray(o) { - if (Array.isArray(o) && o.length === 0) - return true; - return false; } function getFieldName(field, prefix, isRoot, isArrayItem, isArray) { @@ -47,43 +39,28 @@ dotize.convert = function(obj, prefix) { return function recurse(o, p, isRoot) { var isArrayItem = Array.isArray(o); for (var f in o) { - 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, false, true), isArrayItem); // array - } - } else { - 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 { - if (isEmptyObj(currentProp)) { - newObj[getFieldName(f, p, isRoot)] = currentProp; // empty object - } else { - newObj = recurse(currentProp, getFieldName(f, p, isRoot)); // object - } - } - } + var currentProp = o[f]; + if (currentProp && typeof currentProp === "object") { + if (Array.isArray(currentProp)) { + newObj = recurse(currentProp, getFieldName(f, p, isRoot, false, true), isArrayItem); // array } else { - if (isArrayItem || isNumber(f)) { - newObj[getFieldName(f, p, isRoot, true)] = currentProp; // array item primitive + if (isArrayItem && isEmptyObj(currentProp) == false) { + newObj = recurse(currentProp, getFieldName(f, p, isRoot, true)); // array item object + } else if (isEmptyObj(currentProp) == false) { + newObj = recurse(currentProp, getFieldName(f, p, isRoot)); // object } else { - newObj[getFieldName(f, p, isRoot)] = currentProp; // primitive + // } } + } else { + if (isArrayItem || isNumber(f)) { + newObj[getFieldName(f, p, isRoot, true)] = currentProp; // array item primitive + } else { + newObj[getFieldName(f, p, isRoot)] = currentProp; // primitive + } } } - if (isEmptyObj(newObj)) - return obj; - return newObj; }(obj, prefix, true); }; diff --git a/test/test-results.txt b/test/test-results.txt index e964ec3..fc06339 100644 --- a/test/test-results.txt +++ b/test/test-results.txt @@ -15,7 +15,10 @@ √ with prefix object + √ empty √ basic + √ basic with empty object field + √ basic with empty array field √ complex with child √ complex with Array @@ -34,6 +37,10 @@ √ #6 - weird array √ #10 - Keys prefixed with dot when used with arrays + json.org examples + √ example 1 + √ example 2 + - 20 passing (16ms) + 25 passing (15ms) diff --git a/test/test.js b/test/test.js index 75508f9..6f582af 100644 --- a/test/test.js +++ b/test/test.js @@ -1,248 +1,307 @@ 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" - }, - { - "obj1key": "obj1val" - } - ] - }, - "target": { - "data[0].obj0key": "obj0val", - "data[1].obj1key": "obj1val" - } - } - ] - }, -]; +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": "empty", + "source": {}, + "target": {} + }, { + "name": "basic", + "source": { + "a": 1 + }, + "target": { + "a": 1 + } + }, { + "name": "basic with empty object field", + "source": { + "a": {} + }, + "target": { + "a": {} + } + }, { + "name": "basic with empty array field", + "source": { + "a": [] + }, + "target": { + "a": [] + } + }, { + "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" + }, { + "obj1key": "obj1val" + }] + }, + "target": { + "data[0].obj0key": "obj0val", + "data[1].obj1key": "obj1val" + } + }] +}, { + "name": "json.org examples", + "tests": [{ + "name": "example 1", + "source": { + "glossary": { + "title": "example glossary", + "GlossDiv": { + "title": "S", + "GlossList": { + "GlossEntry": { + "ID": "SGML", + "SortAs": "SGML", + "GlossTerm": "Standard Generalized Markup Language", + "Acronym": "SGML", + "Abbrev": "ISO 8879:1986", + "GlossDef": { + "para": "A meta-markup language, used to create markup languages such as DocBook.", + "GlossSeeAlso": ["GML", "XML"] + }, + "GlossSee": "markup" + } + } + } + } + }, + "target": { + "glossary.GlossDiv.GlossList.GlossEntry.Abbrev": "ISO 8879:1986", + "glossary.GlossDiv.GlossList.GlossEntry.Acronym": "SGML", + "glossary.GlossDiv.GlossList.GlossEntry.GlossDef.GlossSeeAlso[0]": "GML", + "glossary.GlossDiv.GlossList.GlossEntry.GlossDef.GlossSeeAlso[1]": "XML", + "glossary.GlossDiv.GlossList.GlossEntry.GlossDef.para": "A meta-markup language, used to create markup languages such as DocBook.", + "glossary.GlossDiv.GlossList.GlossEntry.GlossSee": "markup", + "glossary.GlossDiv.GlossList.GlossEntry.GlossTerm": "Standard Generalized Markup Language", + "glossary.GlossDiv.GlossList.GlossEntry.ID": "SGML", + "glossary.GlossDiv.GlossList.GlossEntry.SortAs": "SGML", + "glossary.GlossDiv.title": "S", + "glossary.title": "example glossary" + } + }, { + "name": "example 2", + "source": { + "menu": { + "id": "file", + "value": "File", + "popup": { + "menuitem": [{ + "value": "New", + "onclick": "CreateNewDoc()" + }, { + "value": "Open", + "onclick": "OpenDoc()" + }, { + "value": "Close", + "onclick": "CloseDoc()" + }] + } + } + }, + "target": { + "menu.id": "file", + "menu.value": "File", + "menu.popup.menuitem[0].value": "New", + "menu.popup.menuitem[0].onclick": "CreateNewDoc()", + "menu.popup.menuitem[1].value": "Open", + "menu.popup.menuitem[1].onclick": "OpenDoc()", + "menu.popup.menuitem[2].value": "Close", + "menu.popup.menuitem[2].onclick": "CloseDoc()" + } + }] +}, ]; 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); + }); + }; + }); +}; \ No newline at end of file