Skip to content

Commit be9e216

Browse files
authored
fix(value): use kind as ultimate source of truth (#6)
1 parent 92d357d commit be9e216

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

index.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,16 @@ export const value = {
101101
* @returns {*}
102102
*/
103103
decode(value: Value): JsonValue {
104-
if (value.listValue) {
105-
return list.decode(value.listValue);
104+
switch(value.kind) {
105+
case 'listValue':
106+
return list.decode(value.listValue);
107+
case 'structValue':
108+
return struct.decode(value.structValue);
109+
case 'nullValue':
110+
return null;
111+
default:
112+
return value[value.kind] as JsonValue;
106113
}
107-
if (value.structValue) {
108-
return struct.decode(value.structValue);
109-
}
110-
if (typeof value.nullValue !== 'undefined') {
111-
return null;
112-
}
113-
return value[value.kind] as JsonValue;
114114
}
115115
};
116116

test.ts

+12
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,18 @@ test('value.decode - boolValue', t => {
169169
t.is(actual, true);
170170
});
171171

172+
// https://github.com/callmehiphop/pb-util/issues/5
173+
test('value.decode - multiple values', t => {
174+
const encodedValue = {
175+
kind: 'stringValue',
176+
stringValue: 'foo',
177+
nullValue: 0
178+
};
179+
180+
const actual = value.decode(encodedValue);
181+
t.is(actual, 'foo');
182+
});
183+
172184
test('list.encode', t => {
173185
const actual = list.encode(arr);
174186
t.deepEqual(actual, listValue);

0 commit comments

Comments
 (0)