Inputs, variables and results are JavaScript's own JSON values: null, booleans, numbers, strings,
arrays and objects. The API converts them to jqlite values on the way in and back on the way out.
The types are JsonInput (what an evaluation takes) and JsonValue (what it returns).
| jqlite | Into jqlite | Out of jqlite |
|---|---|---|
| null | null |
null |
| boolean | true, false |
true, false |
| number | any number, as is |
a number (see Numbers) |
| string | a string; a lone surrogate becomes U+FFFD |
a string |
| array | an array; an undefined element is null |
a new array |
| object | a plain object, or a Map with string keys; an undefined property is left out |
a new plain object |
Anything else is a TypeError before the evaluation starts: a function, a symbol, a bigint,
undefined as the whole input, a Map with a key that is not a string. Any other object is read as
a plain object, through its own enumerable keys: a Date or a Set becomes {}.
import { Jq } from "@devlooped/jqlite";
console.log(Jq.evaluate("[.[] | type]", [1, "s", null, true, [], {}]));
console.log(JSON.stringify(Jq.evaluate(".", { a: undefined, b: [1] })));
[ [ 'number', 'string', 'null', 'boolean', 'array', 'object' ] ]
[{"b":[1]}]
The JsonInput type accepts undefined in readonly arrays and object properties. An array element
with that value becomes null, and an object property with that value is omitted.
Every jqlite number is an IEEE-754 double, as a JavaScript number is. There is no separate integer
type: 1.0 and 1 are the same value, and an integer comes out as a number whose text has no
fraction. Integers are exact up to 2^53; beyond, they round as doubles do.
Inside a program, NaN, the infinities and -0 exist. A result is what its canonical text would read
back as:
| Inside jqlite | Result |
|---|---|
| NaN | null |
| +Infinity, -Infinity | 1.7976931348623157e308, -1.7976931348623157e308 (±Number.MAX_VALUE) |
-0 |
-0 |
import { Jq } from "@devlooped/jqlite";
console.log(Jq.evaluate("[nan, infinite, -infinite, 1.0, 3/2]", null));
console.log(Jq.evaluate("[., isnan]", NaN));
[ [ null, 1.7976931348623157e+308, -1.7976931348623157e+308, 1, 1.5 ] ]
[ [ null, true ] ]
Inputs are taken as they are, NaN and infinities included. evaluateText writes numbers in the
canonical layout of spec §5.2: 100000000000000000000,
1e+21, 1e-7.
A jqlite string is a sequence of Unicode code points. length counts code points, not UTF-16 units:
"😀é" | length is 2. A lone surrogate in a string or a key of the input, of a variable or of the
program becomes U+FFFD, so every result string is well-formed UTF-16.
import { Jq } from "@devlooped/jqlite";
console.log(Jq.evaluate("explode", "a\ud800b"));
[ [ 97, 65533, 98 ] ]
A jqlite object keeps its keys in insertion order (spec §4.1). A
plain JavaScript object does too, except for keys that look like array indexes ("0", "42"):
JavaScript always enumerates those first, in numeric order. So the order of such keys is lost at
the boundary, on the way in and on the way out. Every other key keeps its position.
For the exact order:
Map: its entries go in, in order;evaluateText, which takes JSON text and returns the text the engine writes.import { Jq } from "@devlooped/jqlite";
const input = new Map<string, number>([["b", 1], ["10", 2], ["a", 3]]);
console.log(Jq.evaluate("keys_unsorted", { b: 1, "10": 2, a: 3 }));
console.log(Jq.evaluate("keys_unsorted", input));
console.log(Jq.parse(". + {c: 4}").evaluateText('{"b":1,"10":2,"a":3}'));
[ [ '10', 'b', 'a' ] ]
[ [ 'b', '10', 'a' ] ]
[ '{"b":1,"10":2,"a":3,"c":4}' ]
evaluateText throws a JqError when its input is not exactly one JSON value, for example
Unexpected extra JSON values (while parsing '1 2').
A key named __proto__ is an ordinary own property of a result, not its prototype.