char/fjson

friendly JSON implementation in TypeScript

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

Charlotte Somrestructurebb03820

main
1.1 KiB43 linesraw
1import { Document } from "./document.ts";
2import type { Options } from "./options.ts";
3
4/**
5 * update an fJSON object document's properties in place
6 * without clobbering commits or key ordering.
7 *
8 * overrides are plain objects and get applied left-to-right.
9 * you may use explicit `undefined` to remove a property:
10 *
11 * ```ts
12 * import { apply } from "./mod.ts";
13 *
14 * apply("x: 0, y: { a: 1, b: 2 }", { y: { c: 3 } });
15 * // => "x: 0, y: { a: 1, b: 2, c: 3 }"
16 *
17 * apply("x: 0, y: { a: 1, b: 2 }", { y: undefined }, { y: { c: 3 } });
18 * // => "x: 0, y: { c: 3 }"
19 * ```
20 *
21 * invalid overrides throw `TypeError`;
22 * nesting beyond the limit throws `RangeError` (see {@linkcode applyWith})
23 */
24export function apply(
25  source: string,
26  ...overrides: unknown[]
27): string {
28  return applyWith(source, undefined, ...overrides);
29}
30
31/**
32 * like {@linkcode apply},
33 * but the nesting limit is configurable in `options`
34 */
35export function applyWith(
36  source: string,
37  options: Options | undefined,
38  ...overrides: unknown[]
39): string {
40  const document = Document.parseObject(source, options);
41  document.apply(...overrides);
42  return document.source;
43}