From 7a346981f917ffe56826d424c7bed81f6b551ff3 Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Wed, 23 Feb 2022 11:34:55 +0100 Subject: [PATCH] fix(null): avoid indexing null rawValue, adjusted exists method --- src/index.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index b1b24ca..14b6f3e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -104,7 +104,7 @@ export default class TypedJson { } return new TypedJson(array[index]) - } else if (typeof this.rawValue === 'object') { + } else if (typeof this.rawValue === 'object' && this.rawValue !== null) { const map = this.rawValue as { [property: string]: JsonValue } let index: string = typeof keyOrPath === 'string' ? keyOrPath : `${keyOrPath}` @@ -126,8 +126,11 @@ export default class TypedJson { return new TypedJson(null, new JsonException(JsonError.wrongType)) } - exists (key: JsonKey): boolean { - return this.get(key).exception === undefined + exists (key: JsonKey, notNull: boolean = true): boolean { + return ( + this.get(key).exception === undefined && + (!notNull || this.get(key).rawValue !== null) + ) } string (): string | undefined {