char/fjson

friendly JSON implementation in TypeScript

git clone https://git.t4t.associates/char/fjson

Charlotte Somrestructurebb03820

main
723 B22 linesraw
1/** options accepted by {@linkcode parse}, {@linkcode parseObject} & co. */
2export interface Options {
3  /**
4   * maximum nesting limit (default 128)
5   * containers deeper than this fail with a `"nesting"` error
6   */
7  maxDepth?: number;
8}
9
10/** default `maxDepth` if not provided */
11export const DEFAULT_MAX_DEPTH = 128;
12
13/** safely retrieve the `maxDepth` value from {@linkcode Options} */
14export function resolveMaxDepth(options?: Options): number {
15  const maxDepth = options?.maxDepth ?? DEFAULT_MAX_DEPTH;
16  if (!Number.isInteger(maxDepth) || (maxDepth as number) < 0) {
17    throw new TypeError(
18      `maxDepth must be a non-negative integer, got ${(maxDepth as number)}`,
19    );
20  }
21  return maxDepth as number;
22}