char/fjson
friendly JSON implementation in TypeScript
git clone https://git.t4t.associates/char/fjson
bb03820
main
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 ( 25source :string , 26 ...overrides :unknown [] 27) :string { 28return applyWith ( source , undefined , ...overrides ); 29} 30 31/** 32* like {@linkcode apply}, 33* but the nesting limit is configurable in `options` 34*/ 35export function applyWith ( 36source :string , 37options :Options | undefined , 38 ...overrides :unknown [] 39) :string { 40const document = Document . parseObject ( source , options ); 41document . apply ( ...overrides ); 42return document . source ; 43}