Skip to content

Commit

Permalink
fix(null): avoid indexing null rawValue, adjusted exists method
Browse files Browse the repository at this point in the history
  • Loading branch information
giann committed Feb 23, 2022
1 parent 74914f4 commit 7a34698
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
Expand All @@ -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 {
Expand Down

0 comments on commit 7a34698

Please sign in to comment.