Skip to content

Commit

Permalink
Other: Omit copying of undefined or null in constructors and .create,…
Browse files Browse the repository at this point in the history
… see #743
  • Loading branch information
dcodeIO committed Apr 4, 2017
1 parent 1c4d9d7 commit a66f764
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 74 deletions.
6 changes: 2 additions & 4 deletions src/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,8 @@ Class.generate = function generate(type) { // eslint-disable-line no-unused-vars
else if (field.repeated) gen
("this%s=[]", util.safeProp(field.name));
return gen
("if(p){")
("for(var ks=Object.keys(p),i=0;i<ks.length;++i)")
("this[ks[i]]=p[ks[i]];")
("}");
("if(p)for(var ks=Object.keys(p),i=0;i<ks.length;++i)if(p[ks[i]]!=null)") // omit undefined or null
("this[ks[i]]=p[ks[i]]");
/* eslint-enable no-unexpected-multiline */
};

Expand Down
6 changes: 4 additions & 2 deletions tests/data/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ $root.Test1 = (function() {
function Test1(properties) {
if (properties)
for (var keys = Object.keys(properties), i = 0; i < keys.length; ++i)
this[keys[i]] = properties[keys[i]];
if (properties[keys[i]] != null)
this[keys[i]] = properties[keys[i]];
}

/**
Expand Down Expand Up @@ -244,7 +245,8 @@ $root.Test2 = (function() {
function Test2(properties) {
if (properties)
for (var keys = Object.keys(properties), i = 0; i < keys.length; ++i)
this[keys[i]] = properties[keys[i]];
if (properties[keys[i]] != null)
this[keys[i]] = properties[keys[i]];
}

/**
Expand Down
3 changes: 2 additions & 1 deletion tests/data/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ $root.Message = (function() {
this.int64Map = {};
if (properties)
for (var keys = Object.keys(properties), i = 0; i < keys.length; ++i)
this[keys[i]] = properties[keys[i]];
if (properties[keys[i]] != null)
this[keys[i]] = properties[keys[i]];
}

/**
Expand Down
12 changes: 8 additions & 4 deletions tests/data/mapbox/vector_tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ $root.vector_tile = (function() {
this.layers = [];
if (properties)
for (var keys = Object.keys(properties), i = 0; i < keys.length; ++i)
this[keys[i]] = properties[keys[i]];
if (properties[keys[i]] != null)
this[keys[i]] = properties[keys[i]];
}

/**
Expand Down Expand Up @@ -252,7 +253,8 @@ $root.vector_tile = (function() {
function Value(properties) {
if (properties)
for (var keys = Object.keys(properties), i = 0; i < keys.length; ++i)
this[keys[i]] = properties[keys[i]];
if (properties[keys[i]] != null)
this[keys[i]] = properties[keys[i]];
}

/**
Expand Down Expand Up @@ -587,7 +589,8 @@ $root.vector_tile = (function() {
this.geometry = [];
if (properties)
for (var keys = Object.keys(properties), i = 0; i < keys.length; ++i)
this[keys[i]] = properties[keys[i]];
if (properties[keys[i]] != null)
this[keys[i]] = properties[keys[i]];
}

/**
Expand Down Expand Up @@ -911,7 +914,8 @@ $root.vector_tile = (function() {
this.values = [];
if (properties)
for (var keys = Object.keys(properties), i = 0; i < keys.length; ++i)
this[keys[i]] = properties[keys[i]];
if (properties[keys[i]] != null)
this[keys[i]] = properties[keys[i]];
}

/**
Expand Down
6 changes: 4 additions & 2 deletions tests/data/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ $root.Package = (function() {
this.cliDependencies = [];
if (properties)
for (var keys = Object.keys(properties), i = 0; i < keys.length; ++i)
this[keys[i]] = properties[keys[i]];
if (properties[keys[i]] != null)
this[keys[i]] = properties[keys[i]];
}

/**
Expand Down Expand Up @@ -681,7 +682,8 @@ $root.Package = (function() {
function Repository(properties) {
if (properties)
for (var keys = Object.keys(properties), i = 0; i < keys.length; ++i)
this[keys[i]] = properties[keys[i]];
if (properties[keys[i]] != null)
this[keys[i]] = properties[keys[i]];
}

/**
Expand Down
6 changes: 4 additions & 2 deletions tests/data/rpc-es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ export const MyRequest = $root.MyRequest = (() => {
function MyRequest(properties) {
if (properties)
for (let keys = Object.keys(properties), i = 0; i < keys.length; ++i)
this[keys[i]] = properties[keys[i]];
if (properties[keys[i]] != null)
this[keys[i]] = properties[keys[i]];
}

/**
Expand Down Expand Up @@ -256,7 +257,8 @@ export const MyResponse = $root.MyResponse = (() => {
function MyResponse(properties) {
if (properties)
for (let keys = Object.keys(properties), i = 0; i < keys.length; ++i)
this[keys[i]] = properties[keys[i]];
if (properties[keys[i]] != null)
this[keys[i]] = properties[keys[i]];
}

/**
Expand Down
6 changes: 4 additions & 2 deletions tests/data/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ $root.MyRequest = (function() {
function MyRequest(properties) {
if (properties)
for (var keys = Object.keys(properties), i = 0; i < keys.length; ++i)
this[keys[i]] = properties[keys[i]];
if (properties[keys[i]] != null)
this[keys[i]] = properties[keys[i]];
}

/**
Expand Down Expand Up @@ -258,7 +259,8 @@ $root.MyResponse = (function() {
function MyResponse(properties) {
if (properties)
for (var keys = Object.keys(properties), i = 0; i < keys.length; ++i)
this[keys[i]] = properties[keys[i]];
if (properties[keys[i]] != null)
this[keys[i]] = properties[keys[i]];
}

/**
Expand Down
Loading

0 comments on commit a66f764

Please sign in to comment.