All files / src/internal json.ts

100% Statements 12/12
100% Branches 15/15
100% Functions 6/6
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36    2125x           149x 149x       65x 65x       36x 36x         25x 25x 23x     2x   2x        
/** Reads a property of parsed JSON without trusting its shape. */
export function jsonProperty(value: unknown, key: string): unknown {
  return typeof value === 'object' && value !== null && key in value
    ? Object.getOwnPropertyDescriptor(value, key)?.value
    : undefined
}
 
export function readString(value: unknown, key: string): string | undefined {
  const property = jsonProperty(value, key)
  return typeof property === 'string' ? property : undefined
}
 
export function readNumber(value: unknown, key: string): number | undefined {
  const property = jsonProperty(value, key)
  return typeof property === 'number' ? property : undefined
}
 
export function readBoolean(value: unknown, key: string): boolean | undefined {
  const property = jsonProperty(value, key)
  return typeof property === 'boolean' ? property : undefined
}
 
/** String-valued entries of a JSON object, such as resource tags. */
export function readStringRecord(value: unknown, key: string): Record<string, string> | undefined {
  const property = jsonProperty(value, key)
  if (typeof property !== 'object' || property === null) {
    return undefined
  }
 
  return Object.fromEntries(
    Object.entries(property).filter(
      (entry): entry is [string, string] => typeof entry[1] === 'string',
    ),
  )
}