Skip to content

Commit

Permalink
refactor according to coverage analysis, added tests, versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
vardars committed Dec 16, 2016
1 parent 8acade5 commit 8ae0d97
Show file tree
Hide file tree
Showing 7 changed files with 328 additions and 287 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
bower_components/
bower_components/
coverage/
5 changes: 1 addition & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"dotize"
],
"authors": [
"Vardar <https://twitter.com/vardars>"
"Suleyman Vardar <https://twitter.com/vardars>"
],
"license": "MIT",
"ignore": [
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
53 changes: 15 additions & 38 deletions src/dotize.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
};
Expand Down
9 changes: 8 additions & 1 deletion test/test-results.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)

Loading

0 comments on commit 8ae0d97

Please sign in to comment.