TypeScript#

ts/ is an implementation of Tony written in TypeScript. It reads and writes Tony, and patches, matches, trims and diffs it, keeping comments or dropping them. It has no runtime dependencies: built, it is about 160 KB of JavaScript, 37 KB gzipped, and every call is synchronous.

It is held to the conformance corpus, the cases go-tony passes, read from the same files, and it passes all of them. It does not yet have every operation go-tony has: What is missing lists them. For go-tony itself in JavaScript, every operation included, see WebAssembly.

Building#

The package is not published to npm yet. From a checkout:

cd ts
npm install     # typescript, its one dev dependency
npm run build   # dist/: JavaScript and .d.ts
npm test        # runs the conformance corpus

The tests run the .ts sources directly, which needs a Node that strips types; they are run with Node 26.

Use#

import { parse, encode, patch, diff, match } from "tony-format";

const doc = parse("name: web\nreplicas: 2\n");
const next = patch(doc, parse("replicas: 3\nimage: nginx\n"));

encode(next);
// "image: nginx\nname: web\nreplicas: 3\n"

encode(diff(doc, next));
// image: !insert nginx
// replicas: !replace
//   from: 2
//   to: 3

match(next, parse("replicas: !or [3, 4]\n"));   // true

A value is a node of the IR as a plain object: type, tag, fields, values, string, bool, int, float, number, lines and comment, as docs/ir.md names them. An int is a bigint, so no integer loses digits. fromJSON and toJSON read and write the IR's JSON form.

function answers
parse(text, {comments}) the document's value, or undefined for a document holding none (empty, or only comments). A document Tony refuses throws TonyError.
encode(node, {comments}) Tony text which reads back as the same value. It is not held to go-tony's exact layout.
patch(doc, patch, {comments, pipe}) doc patched, or undefined where the patch deletes the document. Neither argument is changed.
match(doc, pattern) whether doc matches the pattern.
trim(pattern, doc) the part of doc the pattern names.
diff(a, b, {comments}) a patch taking a to b, or undefined when they are the same data.
equalValue, sameData, sameDocument the corpus's three equalities.

comments is off by default, as in go-tony: a patch then answers with no comment at all, and a diff neither compares comments nor carries them (see Diff and Patch).

!pipe runs a command, and the library runs none itself: a patch using it is refused unless pipe is given, a function taking the command and its input and answering its output.

What is missing#

Each of these throws an Error naming what is not implemented, rather than answering something else:

  • in a match: !lt, !le, !gt, !ge, !ir, !get-path and !list-path;
  • in a patch: !get-path, !nullify, !quote, !unquote and !json-patch;
  • in !let: a .[...] reference holding an expression rather than a name, and $[...] interpolation in a string.

Two more are refused as documents or patterns Tony does not accept:

  • a regular expression using what RE2 has and JavaScript does not -- \A, \z, \p{...}, [[:alpha:]] and the U flag -- is refused with a TonyError;
  • parse reads one document, so a --- stream is refused.

Where it differs from go-tony#

The format bounds neither the magnitude nor the precision of a number. go-tony reads an integer as int64 and anything else as float64, and refuses what neither holds. The TypeScript port reads any integer, as a bigint, and a number past float64 into number, the IR's field for a number as written.

The conformance corpus#

conformance/ states what a Tony reader accepts and refuses and what Tony's operations do, in files any implementation can read: one txtar file per subject, each case a document with the answer it must give. Its README is the contract, and names the three equalities a case is judged by: the same value, the same data, and the same document. go-tony, this port and the WebAssembly build all run it; npm test here runs every case.