char/avril

(wip) why'd you have to go and make things so complicaaaaated?

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

Charlotte Somexperiment: port fjson module to ts and generate .d.ts via compiler8ee750c

main
930 B34 linesraw
1import {
2  JSONStringify,
3  RangeError,
4  TypeError,
5} from "ext:core/primordials";
6import {
7  op_fjson_apply,
8  op_fjson_parse,
9  op_fjson_stringify,
10} from "ext:core/ops";
11
12const JSONParse = JSON.parse;
13const ParseError = SyntaxError;
14
15export function parse(source: string): unknown {
16  const result = op_fjson_parse(`${source}`);
17  if (result[1] !== null) throw new ParseError(result[1]);
18  return JSONParse(result[0]);
19}
20
21export function stringify(value: unknown): string {
22  const json = JSONStringify(value);
23  if (json === undefined) throw new TypeError("Value is not JSON serializable");
24  const result = op_fjson_stringify(json);
25  if (result[1] !== null) {
26    const ErrorClass = result[2] ? RangeError : TypeError;
27    throw new ErrorClass(result[1]);
28  }
29  return result[0];
30}
31
32export function apply(source: string, ...overrides: Record<string, unknown>[]): string {
33  return op_fjson_apply(`${source}`, overrides);
34}