Skip to content

Commit

Permalink
Other: Shortened !== undefined && !== null to equivalent != null in s…
Browse files Browse the repository at this point in the history
…tatic code
  • Loading branch information
dcodeIO committed Mar 24, 2017
1 parent afefa3d commit 8401a47
Show file tree
Hide file tree
Showing 9 changed files with 793 additions and 798 deletions.
4 changes: 2 additions & 2 deletions src/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ converter.fromObject = function fromObject(mtype) {
// Non-repeated fields
} else {
if (!(field.resolvedType instanceof Enum)) gen // no need to test for null/undefined if an enum (uses switch)
("if(d%s!==undefined&&d%s!==null){", prop, prop);
("if(d%s!=null){", prop); // !== undefined && !== null
genValuePartial_fromObject(gen, field, /* not sorted */ i, prop);
if (!(field.resolvedType instanceof Enum)) gen
("}");
Expand Down Expand Up @@ -268,7 +268,7 @@ converter.toObject = function toObject(mtype) {
genValuePartial_toObject(gen, field, /* sorted */ index, prop + "[j]")
("}");
} else { gen
("if(m%s!==undefined&&m%s!==null&&m.hasOwnProperty(%j)){", prop, prop, field.name);
("if(m%s!=null&&m.hasOwnProperty(%j)){", prop, field.name); // !== undefined && !== null
genValuePartial_toObject(gen, field, /* sorted */ index, prop);
if (field.partOf) gen
("if(o.oneofs)")
Expand Down
23 changes: 9 additions & 14 deletions src/encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,43 +57,38 @@ function encoder(mtype) {
("}")
("}");

// Repeated fields
} else if (field.repeated) {
// Repeated fields
} else if (field.repeated) { gen
("if(%s&&%s.length&&m.hasOwnProperty(%j)){", ref, ref, field.name);

// Packed repeated
if (field.packed && types.packed[type] !== undefined) { gen

("if(%s&&%s.length&&m.hasOwnProperty(%j)){", ref, ref, field.name)
("w.uint32(%d).fork()", (field.id << 3 | 2) >>> 0)
("for(var i=0;i<%s.length;++i)", ref)
("w.%s(%s[i])", type, ref)
("w.ldelim()")
("}");
("w.ldelim()");

// Non-packed
} else { gen

("if(%s!==undefined&&m.hasOwnProperty(%j)){", ref, field.name)
("for(var i=0;i<%s.length;++i)", ref);
if (wireType === undefined)
genTypePartial(gen, field, index, ref + "[i]");
else gen
("w.uint32(%d).%s(%s[i])", (field.id << 3 | wireType) >>> 0, type, ref);
gen
("}");

}
} gen
("}");

// Non-repeated
} else {
if (!field.required) {
if (field.optional) {

if (field.long) gen
("if(%s!==undefined&&%s!==null&&m.hasOwnProperty(%j))", ref, ref, field.name);
else if (field.bytes || field.resolvedType && !(field.resolvedType instanceof Enum)) gen
if (field.bytes || field.resolvedType && !(field.resolvedType instanceof Enum)) gen
("if(%s&&m.hasOwnProperty(%j))", ref, field.name);
else gen
("if(%s!==undefined&&%s!==null&&m.hasOwnProperty(%j))", ref, ref, field.name);
("if(%s!=null&&m.hasOwnProperty(%j))", ref, field.name); // !== undefined && !== null

}

Expand Down
10 changes: 5 additions & 5 deletions src/verifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ function verifier(mtype) {

// map fields
if (field.map) { gen
("if(%s!==undefined){", ref)
("if(%s!=null){", ref) // !== undefined && !== null
("if(!util.isObject(%s))", ref)
("return%j", invalid(field, "object"))
("var k=Object.keys(%s)", ref)
Expand All @@ -144,7 +144,7 @@ function verifier(mtype) {

// repeated fields
} else if (field.repeated) { gen
("if(%s!==undefined){", ref)
("if(%s!=null){", ref) // !== undefined && !== null
("if(!Array.isArray(%s))", ref)
("return%j", invalid(field, "array"))
("for(var i=0;i<%s.length;++i){", ref);
Expand All @@ -154,8 +154,8 @@ function verifier(mtype) {

// required or present fields
} else {
if (!field.required) gen
("if(%s!==undefined&&%s!==null){", ref, ref);
if (field.optional) gen
("if(%s!=null){", ref); // !== undefined && !== null
if (field.partOf) {
var oneofProp = util.safeProp(field.partOf.name);
if (seenFirstField[field.partOf.name] === 1) gen
Expand All @@ -166,7 +166,7 @@ function verifier(mtype) {
("p%s=1", oneofProp);
}
genVerifyValue(gen, field, i, ref);
if (!field.required) gen
if (field.optional) gen
("}");
}
} return gen
Expand Down
24 changes: 12 additions & 12 deletions tests/data/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ $root.Test1 = (function() {
Test1.encode = function encode(message, writer) {
if (!writer)
writer = $Writer.create();
if (message.field1 !== undefined && message.field1 !== null && message.hasOwnProperty("field1"))
if (message.field1 != null && message.hasOwnProperty("field1"))
writer.uint32(/* id 1, wireType 2 =*/10).string(message.field1);
if (message.field2 !== undefined && message.field2 !== null && message.hasOwnProperty("field2"))
if (message.field2 != null && message.hasOwnProperty("field2"))
writer.uint32(/* id 2, wireType 0 =*/16).uint32(message.field2);
if (message.field3 !== undefined && message.field3 !== null && message.hasOwnProperty("field3"))
if (message.field3 != null && message.hasOwnProperty("field3"))
writer.uint32(/* id 3, wireType 0 =*/24).bool(message.field3);
return writer;
};
Expand Down Expand Up @@ -142,13 +142,13 @@ $root.Test1 = (function() {
Test1.verify = function verify(message) {
if (typeof message !== "object" || message === null)
return "object expected";
if (message.field1 !== undefined && message.field1 !== null)
if (message.field1 != null)
if (!$util.isString(message.field1))
return "field1: string expected";
if (message.field2 !== undefined && message.field2 !== null)
if (message.field2 != null)
if (!$util.isInteger(message.field2))
return "field2: integer expected";
if (message.field3 !== undefined && message.field3 !== null)
if (message.field3 != null)
if (typeof message.field3 !== "boolean")
return "field3: boolean expected";
return null;
Expand All @@ -163,11 +163,11 @@ $root.Test1 = (function() {
if (object instanceof $root.Test1)
return object;
var message = new $root.Test1();
if (object.field1 !== undefined && object.field1 !== null)
if (object.field1 != null)
message.field1 = String(object.field1);
if (object.field2 !== undefined && object.field2 !== null)
if (object.field2 != null)
message.field2 = object.field2 >>> 0;
if (object.field3 !== undefined && object.field3 !== null)
if (object.field3 != null)
message.field3 = Boolean(object.field3);
return message;
};
Expand Down Expand Up @@ -196,11 +196,11 @@ $root.Test1 = (function() {
object.field2 = 0;
object.field3 = false;
}
if (message.field1 !== undefined && message.field1 !== null && message.hasOwnProperty("field1"))
if (message.field1 != null && message.hasOwnProperty("field1"))
object.field1 = message.field1;
if (message.field2 !== undefined && message.field2 !== null && message.hasOwnProperty("field2"))
if (message.field2 != null && message.hasOwnProperty("field2"))
object.field2 = message.field2;
if (message.field3 !== undefined && message.field3 !== null && message.hasOwnProperty("field3"))
if (message.field3 != null && message.hasOwnProperty("field3"))
object.field3 = message.field3;
return object;
};
Expand Down
42 changes: 21 additions & 21 deletions tests/data/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ $root.Message = (function() {
Message.encode = function encode(message, writer) {
if (!writer)
writer = $Writer.create();
if (message.stringVal !== undefined && message.stringVal !== null && message.hasOwnProperty("stringVal"))
if (message.stringVal != null && message.hasOwnProperty("stringVal"))
writer.uint32(/* id 1, wireType 2 =*/10).string(message.stringVal);
if (message.stringRepeated !== undefined && message.hasOwnProperty("stringRepeated"))
if (message.stringRepeated && message.stringRepeated.length && message.hasOwnProperty("stringRepeated"))
for (var i = 0; i < message.stringRepeated.length; ++i)
writer.uint32(/* id 2, wireType 2 =*/18).string(message.stringRepeated[i]);
if (message.uint64Val !== undefined && message.uint64Val !== null && message.hasOwnProperty("uint64Val"))
if (message.uint64Val != null && message.hasOwnProperty("uint64Val"))
writer.uint32(/* id 3, wireType 0 =*/24).uint64(message.uint64Val);
if (message.uint64Repeated && message.uint64Repeated.length && message.hasOwnProperty("uint64Repeated")) {
writer.uint32(/* id 4, wireType 2 =*/34).fork();
Expand All @@ -122,10 +122,10 @@ $root.Message = (function() {
}
if (message.bytesVal && message.hasOwnProperty("bytesVal"))
writer.uint32(/* id 5, wireType 2 =*/42).bytes(message.bytesVal);
if (message.bytesRepeated !== undefined && message.hasOwnProperty("bytesRepeated"))
if (message.bytesRepeated && message.bytesRepeated.length && message.hasOwnProperty("bytesRepeated"))
for (var i = 0; i < message.bytesRepeated.length; ++i)
writer.uint32(/* id 6, wireType 2 =*/50).bytes(message.bytesRepeated[i]);
if (message.enumVal !== undefined && message.enumVal !== null && message.hasOwnProperty("enumVal"))
if (message.enumVal != null && message.hasOwnProperty("enumVal"))
writer.uint32(/* id 7, wireType 0 =*/56).uint32(message.enumVal);
if (message.enumRepeated && message.enumRepeated.length && message.hasOwnProperty("enumRepeated")) {
writer.uint32(/* id 8, wireType 2 =*/66).fork();
Expand Down Expand Up @@ -243,45 +243,45 @@ $root.Message = (function() {
Message.verify = function verify(message) {
if (typeof message !== "object" || message === null)
return "object expected";
if (message.stringVal !== undefined && message.stringVal !== null)
if (message.stringVal != null)
if (!$util.isString(message.stringVal))
return "stringVal: string expected";
if (message.stringRepeated !== undefined) {
if (message.stringRepeated != null) {
if (!Array.isArray(message.stringRepeated))
return "stringRepeated: array expected";
for (var i = 0; i < message.stringRepeated.length; ++i)
if (!$util.isString(message.stringRepeated[i]))
return "stringRepeated: string[] expected";
}
if (message.uint64Val !== undefined && message.uint64Val !== null)
if (message.uint64Val != null)
if (!$util.isInteger(message.uint64Val) && !(message.uint64Val && $util.isInteger(message.uint64Val.low) && $util.isInteger(message.uint64Val.high)))
return "uint64Val: integer|Long expected";
if (message.uint64Repeated !== undefined) {
if (message.uint64Repeated != null) {
if (!Array.isArray(message.uint64Repeated))
return "uint64Repeated: array expected";
for (var i = 0; i < message.uint64Repeated.length; ++i)
if (!$util.isInteger(message.uint64Repeated[i]) && !(message.uint64Repeated[i] && $util.isInteger(message.uint64Repeated[i].low) && $util.isInteger(message.uint64Repeated[i].high)))
return "uint64Repeated: integer|Long[] expected";
}
if (message.bytesVal !== undefined && message.bytesVal !== null)
if (message.bytesVal != null)
if (!(message.bytesVal && typeof message.bytesVal.length === "number" || $util.isString(message.bytesVal)))
return "bytesVal: buffer expected";
if (message.bytesRepeated !== undefined) {
if (message.bytesRepeated != null) {
if (!Array.isArray(message.bytesRepeated))
return "bytesRepeated: array expected";
for (var i = 0; i < message.bytesRepeated.length; ++i)
if (!(message.bytesRepeated[i] && typeof message.bytesRepeated[i].length === "number" || $util.isString(message.bytesRepeated[i])))
return "bytesRepeated: buffer[] expected";
}
if (message.enumVal !== undefined && message.enumVal !== null)
if (message.enumVal != null)
switch (message.enumVal) {
default:
return "enumVal: enum value expected";
case 1:
case 2:
break;
}
if (message.enumRepeated !== undefined) {
if (message.enumRepeated != null) {
if (!Array.isArray(message.enumRepeated))
return "enumRepeated: array expected";
for (var i = 0; i < message.enumRepeated.length; ++i)
Expand All @@ -293,7 +293,7 @@ $root.Message = (function() {
break;
}
}
if (message.int64Map !== undefined) {
if (message.int64Map != null) {
if (!$util.isObject(message.int64Map))
return "int64Map: object expected";
var key = Object.keys(message.int64Map);
Expand All @@ -313,7 +313,7 @@ $root.Message = (function() {
if (object instanceof $root.Message)
return object;
var message = new $root.Message();
if (object.stringVal !== undefined && object.stringVal !== null)
if (object.stringVal != null)
message.stringVal = String(object.stringVal);
if (object.stringRepeated) {
if (!Array.isArray(object.stringRepeated))
Expand All @@ -322,7 +322,7 @@ $root.Message = (function() {
for (var i = 0; i < object.stringRepeated.length; ++i)
message.stringRepeated[i] = String(object.stringRepeated[i]);
}
if (object.uint64Val !== undefined && object.uint64Val !== null)
if (object.uint64Val != null)
if ($util.Long)
(message.uint64Val = $util.Long.fromValue(object.uint64Val)).unsigned = true;
else if (typeof object.uint64Val === "string")
Expand All @@ -345,7 +345,7 @@ $root.Message = (function() {
else if (typeof object.uint64Repeated[i] === "object")
message.uint64Repeated[i] = new $util.LongBits(object.uint64Repeated[i].low >>> 0, object.uint64Repeated[i].high >>> 0).toNumber(true);
}
if (object.bytesVal !== undefined && object.bytesVal !== null)
if (object.bytesVal != null)
if (typeof object.bytesVal === "string")
$util.base64.decode(object.bytesVal, message.bytesVal = $util.newBuffer($util.base64.length(object.bytesVal)), 0);
else if (object.bytesVal.length)
Expand Down Expand Up @@ -441,14 +441,14 @@ $root.Message = (function() {
object.bytesVal = options.bytes === String ? "" : [];
object.enumVal = options.enums === String ? "ONE" : 1;
}
if (message.stringVal !== undefined && message.stringVal !== null && message.hasOwnProperty("stringVal"))
if (message.stringVal != null && message.hasOwnProperty("stringVal"))
object.stringVal = message.stringVal;
if (message.stringRepeated && message.stringRepeated.length) {
object.stringRepeated = [];
for (var j = 0; j < message.stringRepeated.length; ++j)
object.stringRepeated[j] = message.stringRepeated[j];
}
if (message.uint64Val !== undefined && message.uint64Val !== null && message.hasOwnProperty("uint64Val"))
if (message.uint64Val != null && message.hasOwnProperty("uint64Val"))
if (typeof message.uint64Val === "number")
object.uint64Val = options.longs === String ? String(message.uint64Val) : message.uint64Val;
else
Expand All @@ -461,14 +461,14 @@ $root.Message = (function() {
else
object.uint64Repeated[j] = options.longs === String ? $util.Long.prototype.toString.call(message.uint64Repeated[j]) : options.longs === Number ? new $util.LongBits(message.uint64Repeated[j].low >>> 0, message.uint64Repeated[j].high >>> 0).toNumber(true) : message.uint64Repeated[j];
}
if (message.bytesVal !== undefined && message.bytesVal !== null && message.hasOwnProperty("bytesVal"))
if (message.bytesVal != null && message.hasOwnProperty("bytesVal"))
object.bytesVal = options.bytes === String ? $util.base64.encode(message.bytesVal, 0, message.bytesVal.length) : options.bytes === Array ? Array.prototype.slice.call(message.bytesVal) : message.bytesVal;
if (message.bytesRepeated && message.bytesRepeated.length) {
object.bytesRepeated = [];
for (var j = 0; j < message.bytesRepeated.length; ++j)
object.bytesRepeated[j] = options.bytes === String ? $util.base64.encode(message.bytesRepeated[j], 0, message.bytesRepeated[j].length) : options.bytes === Array ? Array.prototype.slice.call(message.bytesRepeated[j]) : message.bytesRepeated[j];
}
if (message.enumVal !== undefined && message.enumVal !== null && message.hasOwnProperty("enumVal"))
if (message.enumVal != null && message.hasOwnProperty("enumVal"))
object.enumVal = options.enums === String ? $root.Message.SomeEnum[message.enumVal] : message.enumVal;
if (message.enumRepeated && message.enumRepeated.length) {
object.enumRepeated = [];
Expand Down
Loading

0 comments on commit 8401a47

Please sign in to comment.