Skip to content

Commit

Permalink
fix(struct): add encoding check to not pass undefined values (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fleker authored and callmehiphop committed Jun 14, 2019
1 parent ead6908 commit 6034685
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ export const struct = {
encode(json: JsonObject): Struct {
const fields = {};
Object.keys(json).forEach(key => {
// If value is undefined, do not encode it.
if (!json[key]) return;
fields[key] = value.encode(json[key]);
});
return {fields};
Expand Down
11 changes: 11 additions & 0 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ const obj = {
yes: true
};

const objWithUndefined = {
foo: 'bar',
yes: true,
isUndefined: undefined
};

const listValue = {
values: [{
kind: 'nullValue',
Expand Down Expand Up @@ -172,6 +178,11 @@ test('struct.encode', t => {
t.deepEqual(actual, structValue);
});

test('struct.encode - undefined value', t => {
const actual = struct.encode(objWithUndefined);
t.deepEqual(actual, structValue);
});

test('struct.decode', t => {
const actual = struct.decode(structValue);
t.deepEqual(actual, obj);
Expand Down

0 comments on commit 6034685

Please sign in to comment.