From 55f7bea84344a04e1bb0a7dcbf96c6932eeab129 Mon Sep 17 00:00:00 2001 From: Piotr Monwid-Olechnowicz Date: Thu, 8 Aug 2019 19:33:24 +0200 Subject: [PATCH] fix(struct): encode false and null fields correctly (#4) --- index.ts | 2 +- test.ts | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/index.ts b/index.ts index 34d0bb2..1b27db2 100644 --- a/index.ts +++ b/index.ts @@ -128,7 +128,7 @@ export const struct = { const fields = {}; Object.keys(json).forEach(key => { // If value is undefined, do not encode it. - if (!json[key]) return; + if (typeof json[key] === 'undefined') return; fields[key] = value.encode(json[key]); }); return {fields}; diff --git a/test.ts b/test.ts index 88bd9e7..b414cbf 100644 --- a/test.ts +++ b/test.ts @@ -5,12 +5,14 @@ const arr = [null, 10]; const obj = { foo: 'bar', - yes: true + no: false, + nil: null }; const objWithUndefined = { foo: 'bar', - yes: true, + no: false, + nil: null, isUndefined: undefined }; @@ -30,9 +32,13 @@ const structValue = { kind: 'stringValue', stringValue: 'bar' }, - yes: { + no: { kind: 'boolValue', - boolValue: true + boolValue: false + }, + nil: { + kind: 'nullValue', + nullValue: 0 } } };