Skip to content

Commit edcc792

Browse files
Timmmmmourner
authored andcommitted
Use undefined instead of null as the default value (#112)
* Use undefined instead of null as default value This fixes the type issues in #107. Also it makes more sense. I'm unsure about the `return undefined`. * Fix tests
1 parent b4eab64 commit edcc792

2 files changed

Lines changed: 5 additions & 5 deletions

File tree

compile.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ function compileDest(ctx) {
101101
for (var i = 0; i < ctx._proto.fields.length; i++) {
102102
var field = ctx._proto.fields[i];
103103
props[field.name + ': ' + JSON.stringify(ctx._defaults[field.name])] = true;
104-
if (field.oneof) props[field.oneof + ': null'] = true;
104+
if (field.oneof) props[field.oneof + ': undefined'] = true;
105105
}
106106
return '{' + Object.keys(props).join(', ') + '}';
107107
}
@@ -295,7 +295,7 @@ function getDefaultValue(field, value) {
295295
case 'string': return value || '';
296296
case 'bool': return value === 'true';
297297
case 'map': return {};
298-
default: return null;
298+
default: return undefined;
299299
}
300300
}
301301

test/compile.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,8 @@ test('handles all implicit default values', function(t) {
222222
t.equals(data.id, 0);
223223
t.deepEqual(data.tags, []);
224224
t.deepEqual(data.numbers, []);
225-
t.equals(data.bytes, null);
226-
t.equals(data.custom, null);
225+
t.equals(data.bytes, undefined);
226+
t.equals(data.custom, undefined);
227227
t.deepEqual(data.types, []);
228228

229229
t.end();
@@ -237,7 +237,7 @@ test('sets oneof field name', function(t) {
237237
Envelope.write({}, pbf);
238238
var data = Envelope.read(new Pbf(pbf.finish()));
239239

240-
t.equals(data.value, null);
240+
t.equals(data.value, undefined);
241241
t.equals(data.id, 0);
242242

243243
pbf = new Pbf();

0 commit comments

Comments
 (0)