Skip to content

Commit

Permalink
Fix the issue #3
Browse files Browse the repository at this point in the history
  • Loading branch information
sttk committed Apr 16, 2018
1 parent e7b3ef6 commit 6da7d69
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 4 deletions.
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
10 changes: 8 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,15 @@ function _setDeep(obj, keyElems, depth, valueCreator) {
var key = keyElems.shift();
if (!keyElems.length) {
var value = valueCreator(obj, key, depth);
if (value !== undefined) {
obj[key] = value;
if (value === undefined) {
return;
}
if (isObject(value)) { // value is always an empty object.
if (isObject(obj[key])) {
return;
}
}
obj[key] = value;
return;
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"test": "mocha",
"coverage": "istanbul cover _mocha",
"coveralls": "istanbul cover _mocha && istanbul-coveralls",
"web:install": "npm i phantomjs-prebuilt mocha-phantomjs",
"web:install": "npm install --no-save phantomjs-prebuilt mocha-phantomjs",
"web:build": "browserify index.js --standalone copyProps | uglifyjs --compress --mangle -o web/copy-props.js && node test/web/make.js",
"web:test": "mocha-phantomjs -p node_modules/.bin/phantomjs test/web/copy-props.test.html"
},
Expand Down
13 changes: 13 additions & 0 deletions test/copy-props-proc.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ describe('Processing', function() {
done();
});

it('Should do nothing when src prop is an empty object and dst is an ' +
'object', function() {
var src = { a: { b: {} } };
var dst = { a: { b: { c: 1 } } };
expect(copyProps(src, dst)).to.deep.equal({ a: { b: { c: 1 } } });
});
});

describe('About fromto special cases', function() {
Expand Down Expand Up @@ -249,6 +255,13 @@ describe('Processing', function() {
done();
});

it('Should do nothing when src prop is an empty object and dst is an ' +
'object', function() {
var src = { a: { b: {} } };
var dst = { a: { b: { c: 1 } } };
var fromto = ['a.b'];
expect(copyProps(src, dst, fromto)).to.deep.equal({ a: { b: { c: 1 } } });
});
});

describe('About patterns of converter returns', function() {
Expand Down
13 changes: 13 additions & 0 deletions test/web/copy-props-proc.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ describe('Processing', function() {
done();
});

it('Should do nothing when src prop is an empty object and dst is an ' +
'object', function() {
var src = { a: { b: {} } };
var dst = { a: { b: { c: 1 } } };
expect(copyProps(src, dst)).to.deep.equal({ a: { b: { c: 1 } } });
});
});

describe('About fromto special cases', function() {
Expand Down Expand Up @@ -249,6 +255,13 @@ describe('Processing', function() {
done();
});

it('Should do nothing when src prop is an empty object and dst is an ' +
'object', function() {
var src = { a: { b: {} } };
var dst = { a: { b: { c: 1 } } };
var fromto = ['a.b'];
expect(copyProps(src, dst, fromto)).to.deep.equal({ a: { b: { c: 1 } } });
});
});

describe('About patterns of converter returns', function() {
Expand Down
2 changes: 1 addition & 1 deletion web/copy-props.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6da7d69

Please sign in to comment.