Skip to content

Commit b0bd194

Browse files
author
sttk
committed
Modify to create a parent empty object if its end property value is undefined
1 parent 3f53d16 commit b0bd194

File tree

4 files changed

+114
-12
lines changed

4 files changed

+114
-12
lines changed

index.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ module.exports = function(src, dst, fromto, converter, reverse) {
6060

6161
if (fromto) {
6262
eachProps(src, copyWithFromto, opts);
63+
setParentEmptyObject(dst, fromto);
6364
} else {
6465
eachProps(src, copyWithoutFromto, opts);
6566
}
@@ -76,16 +77,15 @@ function copyWithFromto(value, keyChain, nodeInfo) {
7677
if (!dstKeyChains) {
7778
return;
7879
}
80+
delete nodeInfo.fromto[keyChain];
7981

8082
if (!Array.isArray(dstKeyChains)) {
8183
dstKeyChains = [dstKeyChains];
8284
}
8385

8486
for (var i = 0, n = dstKeyChains.length; i < n; i++) {
8587
var dstValue = nodeInfo.convert(value, keyChain, dstKeyChains[i]);
86-
if (dstValue !== undefined) {
87-
setDeep(nodeInfo.dest, dstKeyChains[i], dstValue);
88-
}
88+
setDeep(nodeInfo.dest, dstKeyChains[i], dstValue);
8989
}
9090
}
9191

@@ -99,9 +99,7 @@ function copyWithoutFromto(value, keyChain, nodeInfo) {
9999
}
100100

101101
var dstValue = nodeInfo.convert(value, keyChain, keyChain);
102-
if (dstValue !== undefined) {
103-
setDeep(nodeInfo.dest, keyChain, dstValue);
104-
}
102+
setDeep(nodeInfo.dest, keyChain, dstValue);
105103
}
106104

107105
function noop(v) {
@@ -149,7 +147,9 @@ function setDeep(obj, keyChain, value) {
149147
function _setDeep(obj, keyElems, value) {
150148
var key = keyElems.shift();
151149
if (!keyElems.length) {
152-
obj[key] = value;
150+
if (value !== undefined) {
151+
obj[key] = value;
152+
}
153153
return;
154154
}
155155

@@ -158,3 +158,16 @@ function _setDeep(obj, keyElems, value) {
158158
}
159159
_setDeep(obj[key], keyElems, value);
160160
}
161+
162+
function setParentEmptyObject(obj, fromto) {
163+
for (var srcKeyChain in fromto) {
164+
var dstKeyChains = fromto[srcKeyChain];
165+
if (!Array.isArray(dstKeyChains)) {
166+
dstKeyChains = [dstKeyChains];
167+
}
168+
169+
for (var i = 0, n = dstKeyChains.length; i < n; i++) {
170+
setDeep(obj, dstKeyChains[i], undefined);
171+
}
172+
}
173+
}

test/copy-props-proc.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,28 @@ describe('Processing', function() {
137137
done();
138138
});
139139

140+
it('Should copy properties until parent object if value is undefined',
141+
function(done) {
142+
var src = {
143+
a: undefined,
144+
b: { c: undefined },
145+
e: { f: { g: undefined } },
146+
h: {},
147+
i: { j: {} },
148+
};
149+
var dst = {};
150+
var result = copyProps(src, dst);
151+
var expected = { b: {}, e: { f: {} }, h: {}, i: { j: {} } };
152+
expect(result).to.deep.equal(expected);
153+
expect(result.a).to.be.undefined;
154+
expect(result.b.c).to.be.undefined;
155+
expect(result.b.d).to.be.undefined;
156+
expect(result.e.f.g).to.be.undefined;
157+
expect(result.h.xxx).to.be.undefined;
158+
expect(result.i.j.yyy).to.be.undefined;
159+
done();
160+
});
161+
140162
});
141163

142164
describe('About fromto special cases', function() {
@@ -211,6 +233,21 @@ describe('Processing', function() {
211233
done();
212234
});
213235

236+
it('Should copy properties until parent object if value is undefined',
237+
function(done) {
238+
var src = {};
239+
var dst = {};
240+
var fromto = ['a', 'b.c', 'b.d', 'e.f.g'];
241+
var result = copyProps(src, dst, fromto);
242+
var expected = { b: {}, e: { f: {} } };
243+
expect(result).to.deep.equal(expected);
244+
expect(result.a).to.be.undefined;
245+
expect(result.b.c).to.be.undefined;
246+
expect(result.b.d).to.be.undefined;
247+
expect(result.e.f.g).to.be.undefined;
248+
done();
249+
});
250+
214251
});
215252

216253
describe('About patterns of converter returns', function() {

test/web/copy-props.test.js

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ module.exports = function(src, dst, fromto, converter, reverse) {
6161

6262
if (fromto) {
6363
eachProps(src, copyWithFromto, opts);
64+
setParentEmptyObject(dst, fromto);
6465
} else {
6566
eachProps(src, copyWithoutFromto, opts);
6667
}
@@ -77,6 +78,7 @@ function copyWithFromto(value, keyChain, nodeInfo) {
7778
if (!dstKeyChains) {
7879
return;
7980
}
81+
delete nodeInfo.fromto[keyChain];
8082

8183
if (!Array.isArray(dstKeyChains)) {
8284
dstKeyChains = [dstKeyChains];
@@ -100,9 +102,7 @@ function copyWithoutFromto(value, keyChain, nodeInfo) {
100102
}
101103

102104
var dstValue = nodeInfo.convert(value, keyChain, keyChain);
103-
if (dstValue !== undefined) {
104-
setDeep(nodeInfo.dest, keyChain, dstValue);
105-
}
105+
setDeep(nodeInfo.dest, keyChain, dstValue);
106106
}
107107

108108
function noop(v) {
@@ -150,7 +150,9 @@ function setDeep(obj, keyChain, value) {
150150
function _setDeep(obj, keyElems, value) {
151151
var key = keyElems.shift();
152152
if (!keyElems.length) {
153-
obj[key] = value;
153+
if (value !== undefined) {
154+
obj[key] = value;
155+
}
154156
return;
155157
}
156158

@@ -160,6 +162,19 @@ function _setDeep(obj, keyElems, value) {
160162
_setDeep(obj[key], keyElems, value);
161163
}
162164

165+
function setParentEmptyObject(obj, fromto) {
166+
for (var srcKeyChain in fromto) {
167+
var dstKeyChains = fromto[srcKeyChain];
168+
if (!Array.isArray(dstKeyChains)) {
169+
dstKeyChains = [dstKeyChains];
170+
}
171+
172+
for (var i = 0, n = dstKeyChains.length; i < n; i++) {
173+
setDeep(obj, dstKeyChains[i], undefined);
174+
}
175+
}
176+
}
177+
163178
},{"each-props":38,"is-plain-object":40}],2:[function(require,module,exports){
164179
/*!
165180
* assertion-error
@@ -8863,6 +8878,28 @@ describe('Processing', function() {
88638878
done();
88648879
});
88658880

8881+
it('Should copy properties until parent object if value is undefined',
8882+
function(done) {
8883+
var src = {
8884+
a: undefined,
8885+
b: { c: undefined },
8886+
e: { f: { g: undefined } },
8887+
h: {},
8888+
i: { j: {} },
8889+
};
8890+
var dst = {};
8891+
var result = copyProps(src, dst);
8892+
var expected = { b: {}, e: { f: {} }, h: {}, i: { j: {} } };
8893+
expect(result).to.deep.equal(expected);
8894+
expect(result.a).to.be.undefined;
8895+
expect(result.b.c).to.be.undefined;
8896+
expect(result.b.d).to.be.undefined;
8897+
expect(result.e.f.g).to.be.undefined;
8898+
expect(result.h.xxx).to.be.undefined;
8899+
expect(result.i.j.yyy).to.be.undefined;
8900+
done();
8901+
});
8902+
88668903
});
88678904

88688905
describe('About fromto special cases', function() {
@@ -8937,6 +8974,21 @@ describe('Processing', function() {
89378974
done();
89388975
});
89398976

8977+
it('Should copy properties until parent object if value is undefined',
8978+
function(done) {
8979+
var src = {};
8980+
var dst = {};
8981+
var fromto = ['a', 'b.c', 'b.d', 'e.f.g'];
8982+
var result = copyProps(src, dst, fromto);
8983+
var expected = { b: {}, e: { f: {} } };
8984+
expect(result).to.deep.equal(expected);
8985+
expect(result.a).to.be.undefined;
8986+
expect(result.b.c).to.be.undefined;
8987+
expect(result.b.d).to.be.undefined;
8988+
expect(result.e.f.g).to.be.undefined;
8989+
done();
8990+
});
8991+
89408992
});
89418993

89428994
describe('About patterns of converter returns', function() {

web/copy-props.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)