@mark1russell7/rigor-generators 0.1.19 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -139,6 +139,8 @@ const points = familyPoints(family, latinHypercube(200, 3, random("design")), ra
139
139
 
140
140
  A distribution of a dimension uses its `cdf` and its `quantile`, from `distributionOf`. The value of a coordinate `u` is the quantile of `u` in the probability of the range. Thus a Latin hypercube also gives one value in each stratum of the probability.
141
141
 
142
+ `oneAtATimePoints(family, n, base, r)` gives points of the family, not of the unit cube. It gives one block of `n` points for each dimension. In a block, one dimension takes one value in each of `n` strata, and each other dimension keeps its value in `base`. A base value can be out of the range, for example 0 for a log dimension. Thus each block gives the law of one dimension, with the others held. The column of a dimension comes from `r.fork("column:" + name)`, thus a new dimension does not change the blocks of the others.
143
+
142
144
  The parameters of a dimension come from `r.fork("dimension:" + name)`. The same seed gives the same points, and a new dimension does not change the values of the others.
143
145
 
144
146
  For the mean of a sum of 3 inputs from 50 points, the Latin hypercube gives 0.00042 of the variance of the random design. A test holds the ratio below 1/1000.
@@ -224,7 +226,17 @@ The default form of `canonicalJson` is RFC 8785 (JCS). Three options change it:
224
226
  | --- | --- | --- |
225
227
  | `form` | `"jcs"`: `NaN`, an infinite number and a lone surrogate cause a `RangeError` | `"json"`: the form of `JSON.stringify`, so these values become `null` or an escape |
226
228
  | `bigint` | `"error"`: a `bigint` causes a `TypeError` | `"suffix"`: the digits and `n`, for example `12n`. This text is not JSON, so use it only for a key |
227
- | `tags` | `"none"`: the form of `JSON.stringify`. A `Date` gives its ISO text. A `RegExp`, a `Map` and a `Set` give `{}` | `"typed"`: a tag with the type and the value. This text is not JSON, so use it only for a key or a hash |
229
+ | `tags` | `"none"`: a `Date` gives its ISO text. A `RegExp`, a `Map` and a `Set` cause a `TypeError` | `"typed"`: a tag with the type and the value. This text is not JSON, so use it only for a key or a hash |
230
+
231
+ By default, `canonicalJson` refuses each object that keeps its content out of its own keys. `JSON.stringify` writes such an object as `{}`, so two of them with other contents get the same hash:
232
+
233
+ - A `Map`, a `Set`, a `RegExp`, a `WeakMap`, a `WeakSet`, an `Error`, a `Promise`, an `ArrayBuffer` and a `DataView` cause a `TypeError`. Its text names the fix.
234
+ - A `Date` gives its ISO text through its `toJSON`. A boxed `Number`, `String` or `Boolean` gives its primitive value, as in `JSON.stringify`.
235
+ - A plain object, an array, an `arguments` object, a typed array and an instance of a class give their own enumerable keys.
236
+ - The type of an object comes from its internal slots. Thus a value from another realm, for example from `node:vm`, gets the same form.
237
+ - An object with its own `Symbol.toStringTag` needs a method `toJSON`. The form does not see private fields.
238
+
239
+ Before 0.2.0, the default wrote these objects as `{}`. From 0.2.0, a program that hashes one of them gets a `TypeError`. Use `tags: "typed"`, or give the object a method `toJSON`.
228
240
 
229
241
  With `tags: "typed"`, two values that differ only in a `Date`, a `RegExp`, a `Map` or a `Set` get different keys:
230
242
 
@@ -55,4 +55,13 @@ export declare function stratifiedDesign(strata: number, d: number, r: Random, l
55
55
  * from `r.fork("B")`. With `base` `"latin"`, each is a Latin hypercube.
56
56
  */
57
57
  export declare function saltelliDesign(n: number, d: number, r: Random, base?: "random" | "latin"): SaltelliDesign;
58
+ /**
59
+ * This function gives a design of one dimension at a time: one block of `n` points for each dimension, in the order of the
60
+ * dimensions. In the block of a dimension, the dimension takes the values of a Latin column of `n` strata. Each other
61
+ * dimension keeps its value in `base`. The column of a dimension comes from `r.fork("column:" + name)`, and the map comes
62
+ * from `familyMap(family, r)`. Thus a new dimension does not change the blocks of the others. A base value can be out of
63
+ * the range of its dimension, for example 0 for a log dimension. A base with no finite value for a dimension is a `RangeError`.
64
+ * A fraction for an integer dimension and a base value with no dimension are also a `RangeError`.
65
+ */
66
+ export declare function oneAtATimePoints(family: InputFamily, n: number, base: Point, r: Random): Point[];
58
67
  //# sourceMappingURL=designs.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"designs.d.ts","names":[],"sources":["../src/designs.ts"],"mappings":";;;iBAOiB;;WAEN;;WAEA;;WAEA;;WAEA,eAAe;;WAEf;;WAEA;;;iBAIM;WACN;WACA,qBAAqB;;;YAIpB,QAAQ,SAAS;;iBAGZ;WACN;WACA;;WAEA;;;;;;;wBA0FK,UAAU,QAAQ,aAAa,GAAG,UAAU,4BAA4B;;wBAgBxE,aAAa,QAAQ,aAAa,sCAAsC,GAAG,SAAS;;wBAMpF,aAAa,WAAW,WAAW,GAAG;;;;;wBAUtC,eAAe,WAAW,WAAW,GAAG;;;;;wBAgBxC,iBAAiB,gBAAgB,WAAW,GAAG,QAAQ;;;;;;wBAoBvD,eAAe,WAAW,WAAW,GAAG,QAAQ,4BAAsC"}
1
+ {"version":3,"file":"designs.d.ts","names":[],"sources":["../src/designs.ts"],"mappings":";;;iBAOiB;;WAEN;;WAEA;;WAEA;;WAEA,eAAe;;WAEf;;WAEA;;;iBAIM;WACN;WACA,qBAAqB;;;YAIpB,QAAQ,SAAS;;iBAGZ;WACN;WACA;;WAEA;;;;;;;wBA0FK,UAAU,QAAQ,aAAa,GAAG,UAAU,4BAA4B;;wBAgBxE,aAAa,QAAQ,aAAa,sCAAsC,GAAG,SAAS;;wBAMpF,aAAa,WAAW,WAAW,GAAG;;;;;wBAUtC,eAAe,WAAW,WAAW,GAAG;;;;;wBAgBxC,iBAAiB,gBAAgB,WAAW,GAAG,QAAQ;;;;;;wBAoBvD,eAAe,WAAW,WAAW,GAAG,QAAQ,4BAAsC;;;;;;;;;wBAgBtF,iBAAiB,QAAQ,aAAa,WAAW,MAAM,OAAO,GAAG,SAAS"}
package/build/designs.js CHANGED
@@ -145,6 +145,31 @@ function saltelliDesign(n, d, r, base = "random") {
145
145
  ab: Array.from({ length: d }, (_, i) => a.map((row, k) => row.map((x, j) => j === i ? b[k][j] : x)))
146
146
  };
147
147
  }
148
- export { checkDimension, familyMap, familyNames, familyPoints, latinHypercube, randomDesign, saltelliDesign, stratifiedDesign };
148
+ /**
149
+ * This function gives a design of one dimension at a time: one block of `n` points for each dimension, in the order of the
150
+ * dimensions. In the block of a dimension, the dimension takes the values of a Latin column of `n` strata. Each other
151
+ * dimension keeps its value in `base`. The column of a dimension comes from `r.fork("column:" + name)`, and the map comes
152
+ * from `familyMap(family, r)`. Thus a new dimension does not change the blocks of the others. A base value can be out of
153
+ * the range of its dimension, for example 0 for a log dimension. A base with no finite value for a dimension is a `RangeError`.
154
+ * A fraction for an integer dimension and a base value with no dimension are also a `RangeError`.
155
+ */
156
+ function oneAtATimePoints(family, n, base, r) {
157
+ count("oneAtATimePoints", "n", n);
158
+ const map = familyMap(family, r);
159
+ const names = familyNames(family);
160
+ const extra = Object.keys(base).find((name) => !names.includes(name));
161
+ if (extra !== void 0) throw new RangeError(`${family.name}: the base gives "${extra}", and the family has no dimension with this name.`);
162
+ for (const dim of family.dimensions) {
163
+ const value = base[dim.name];
164
+ if (value === void 0 || !Number.isFinite(value) || dim.integer === true && !Number.isInteger(value)) throw new RangeError(`${family.name}.${dim.name}: the base value ${value} is not a value of the dimension.`);
165
+ }
166
+ return names.flatMap((name, i) => latinHypercube(n, 1, r.fork(`column:${name}`)).map(([u]) => {
167
+ const unit = names.map(() => 0);
168
+ unit[i] = u;
169
+ const moved = map(unit)[name];
170
+ return Object.fromEntries(names.map((each) => [each, each === name ? moved : base[each]]));
171
+ }));
172
+ }
173
+ export { checkDimension, familyMap, familyNames, familyPoints, latinHypercube, oneAtATimePoints, randomDesign, saltelliDesign, stratifiedDesign };
149
174
 
150
175
  //# sourceMappingURL=designs.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"designs.js","names":[],"sources":["../src/designs.ts"],"sourcesContent":["// Input families and the designs that draw their points (R5). A design gives points of the unit cube. `familyMap`\n// takes each point to a point of the family: a value from the range of each dimension.\nimport type { DistributionSpec } from \"./distributions.ts\";\nimport { distributionOf } from \"./quantiles.ts\";\nimport type { Random } from \"./random.ts\";\n\n/** One dimension of an input family. */\nexport interface Dimension {\n /** The name of the dimension. It is a key of each point. */\n readonly name: string;\n /** The smallest value. */\n readonly min: number;\n /** The largest value. A uniform or log dimension that is not `integer` gets values below `max`. */\n readonly max: number;\n /** The distribution of the values, cut to the range. Without it, the values are uniform in the range. */\n readonly distribution?: DistributionSpec;\n /** With `log`, the logarithm of the value is uniform. `min` must be above 0. A dimension with a distribution has no scale. */\n readonly scale?: \"linear\" | \"log\";\n /** With `true`, each value is an integer. `min` and `max` must be integers. */\n readonly integer?: boolean;\n}\n\n/** An input family: its name and its dimensions. */\nexport interface InputFamily {\n readonly name: string;\n readonly dimensions: readonly Dimension[];\n}\n\n/** One point of a family: one value for each dimension, by name. */\nexport type Point = Readonly<Record<string, number>>;\n\n/** The Saltelli design: the matrices `a` and `b`, and for each dimension `i` the matrix `ab[i]`. */\nexport interface SaltelliDesign {\n readonly a: number[][];\n readonly b: number[][];\n /** `ab[i]` is `a` with the column `i` of `b`. */\n readonly ab: number[][][];\n}\n\nconst BELOW_ONE = 1 - Number.EPSILON / 2;\n\n/** This function gives the next double toward minus infinity. */\nfunction nextDown(x: number): number {\n if (x === 0) return -Number.MIN_VALUE;\n const f = new Float64Array([x]);\n const bits = new BigInt64Array(f.buffer);\n bits[0] = x > 0 ? bits[0]! - 1n : bits[0]! + 1n;\n return f[0]!;\n}\n\n/** This function gives the next double toward plus infinity. */\nfunction nextUp(x: number): number {\n return -nextDown(-x);\n}\n\n/** This function keeps a value below 1. A sum of a stratum and a draw can round up to 1. */\nfunction belowOne(x: number): number {\n return x < 1 ? x : BELOW_ONE;\n}\n\nfunction count(fn: string, name: string, value: number): void {\n if (!Number.isSafeInteger(value) || value < 1) throw new RangeError(`${fn}: ${name} is ${value}, and it must be an integer from 1.`);\n}\n\n/**\n * This function examines a dimension of a family. A range that is not finite, an integer dimension without integer ends and\n * a bad scale are each a `RangeError`.\n */\nexport function checkDimension(family: string, dim: Dimension): void {\n const where = `${family}.${dim.name}`;\n if (!Number.isFinite(dim.min) || !Number.isFinite(dim.max) || dim.min > dim.max) {\n throw new RangeError(`${where}: the range ${dim.min} to ${dim.max} is not a finite range.`);\n }\n if (dim.integer === true && (!Number.isSafeInteger(dim.min) || !Number.isSafeInteger(dim.max))) {\n throw new RangeError(`${where}: an integer dimension needs integer ends, not ${dim.min} and ${dim.max}.`);\n }\n if (dim.distribution !== undefined && dim.scale !== undefined) throw new RangeError(`${where}: a dimension with a distribution has no scale.`);\n if (dim.scale === \"log\" && !(dim.min > 0)) throw new RangeError(`${where}: a log scale needs a minimum above 0, not ${dim.min}.`);\n}\n\n/** This function examines a dimension and gives the map from a uniform number in [0, 1) to a value of the dimension. */\nfunction dimensionMap(family: string, dim: Dimension, r: Random): (u: number) => number {\n const where = `${family}.${dim.name}`;\n checkDimension(family, dim);\n // An integer dimension takes the floor of a value from [min, max + 1).\n const end = dim.integer === true ? dim.max + 1 : dim.max;\n const top = dim.integer === true ? nextDown(end) : end;\n const whole = (x: number): number => (dim.integer === true ? Math.min(dim.max, Math.floor(x)) : x);\n const clamp = (x: number): number => Math.min(top, Math.max(dim.min, x));\n if (dim.distribution !== undefined) {\n const d = distributionOf(dim.distribution, r);\n const low = d.cdf(nextDown(dim.min));\n const high = d.cdf(top);\n if (!(high > low)) throw new RangeError(`${where}: the distribution has no probability from ${dim.min} to ${dim.max}.`);\n // A discrete quantile at `low` can be a value below `min`. The smallest value above `low` is then the first value in the range.\n const first = d.quantile(nextUp(low));\n return (u) => {\n const x = d.quantile(low + u * (high - low));\n return whole(clamp(x < dim.min ? first : x));\n };\n }\n if (dim.scale === \"log\") {\n // The form `min * exp(u * span)` gives `min` exactly at 0.\n const span = Math.log(end / dim.min);\n return (u) => whole(clamp(dim.min * Math.exp(u * span)));\n }\n return (u) => whole(clamp(dim.min + u * (end - dim.min)));\n}\n\n/**\n * This function gives the names of the dimensions of a family. A family with no dimension is a `RangeError`. A name that\n * is empty or not unique is also a `RangeError`.\n */\nexport function familyNames(family: InputFamily): string[] {\n if (family.dimensions.length === 0) throw new RangeError(`${family.name}: a family needs one dimension or more.`);\n const names = family.dimensions.map((d) => d.name);\n const twice = names.find((n, i) => n === \"\" || names.indexOf(n) !== i);\n if (twice !== undefined) throw new RangeError(`${family.name}: the dimension name \"${twice}\" is empty or not unique.`);\n return names;\n}\n\n/**\n * This function gives the map from a point of the unit cube to a point of the family. Coordinate `i` gives the value of\n * dimension `i`. The parameters of each distribution come from `r.fork(\"dimension:\" + name)` one time. Thus the\n * same seed gives the same map, and a new dimension does not change the others.\n */\nexport function familyMap(family: InputFamily, r: Random): (unit: readonly number[]) => Point {\n const names = familyNames(family);\n const maps = family.dimensions.map((d) => dimensionMap(family.name, d, r.fork(`dimension:${d.name}`)));\n return (unit) => {\n if (unit.length !== maps.length) throw new RangeError(`${family.name}: a point needs ${maps.length} coordinates, not ${unit.length}.`);\n const point: Record<string, number> = {};\n maps.forEach((map, i) => {\n const u = unit[i]!;\n if (!(u >= 0 && u < 1)) throw new RangeError(`${family.name}: coordinate ${i} is ${u}, and it must be from 0 to below 1.`);\n point[names[i]!] = map(u);\n });\n return point;\n };\n}\n\n/** This function gives the points of the family at the points of a unit design. */\nexport function familyPoints(family: InputFamily, unit: readonly (readonly number[])[], r: Random): Point[] {\n const map = familyMap(family, r);\n return unit.map(map);\n}\n\n/** This function gives `n` independent uniform points of the unit cube in `d` dimensions. The draws go row by row. */\nexport function randomDesign(n: number, d: number, r: Random): number[][] {\n count(\"randomDesign\", \"n\", n);\n count(\"randomDesign\", \"d\", d);\n return Array.from({ length: n }, () => Array.from({ length: d }, () => r.next()));\n}\n\n/**\n * This function gives a Latin hypercube of `n` points in `d` dimensions. Each column has exactly one point in each of\n * the `n` strata `[k/n, (k+1)/n)`. For each column, the draws are one shuffle of the strata, then one draw in each point.\n */\nexport function latinHypercube(n: number, d: number, r: Random): number[][] {\n count(\"latinHypercube\", \"n\", n);\n count(\"latinHypercube\", \"d\", d);\n const rows = Array.from({ length: n }, () => new Array<number>(d));\n const strata = Array.from({ length: n }, (_, k) => k);\n for (let j = 0; j < d; j++) {\n const order = r.shuffle(strata);\n for (let i = 0; i < n; i++) rows[i]![j] = belowOne((order[i]! + r.next()) / n);\n }\n return rows;\n}\n\n/**\n * This function gives a stratified grid: `strata` strata on each axis, so `strata^d` cells, and one uniform point in\n * each cell. The cells go in the order of their index in base `strata`, with the first dimension as the last digit.\n */\nexport function stratifiedDesign(strata: number, d: number, r: Random, limit = 1_000_000): number[][] {\n count(\"stratifiedDesign\", \"strata\", strata);\n count(\"stratifiedDesign\", \"d\", d);\n const n = strata ** d;\n if (n > limit) throw new RangeError(`stratifiedDesign: ${strata}^${d} = ${n} cells is above the limit ${limit}.`);\n return Array.from({ length: n }, (_, cell) => {\n let rest = cell;\n return Array.from({ length: d }, () => {\n const k = rest % strata;\n rest = Math.floor(rest / strata);\n return belowOne((k + r.next()) / strata);\n });\n });\n}\n\n/**\n * This function gives the Saltelli design for the Sobol indices of a model with `d` inputs: `a`, `b` and the `d`\n * matrices `ab`. The design has `n * (d + 2)` points for the model. `a` comes from `r.fork(\"A\")`, and `b` comes\n * from `r.fork(\"B\")`. With `base` `\"latin\"`, each is a Latin hypercube.\n */\nexport function saltelliDesign(n: number, d: number, r: Random, base: \"random\" | \"latin\" = \"random\"): SaltelliDesign {\n const make = base === \"latin\" ? latinHypercube : randomDesign;\n const a = make(n, d, r.fork(\"A\"));\n const b = make(n, d, r.fork(\"B\"));\n const ab = Array.from({ length: d }, (_, i) => a.map((row, k) => row.map((x, j) => (j === i ? b[k]![j]! : x))));\n return { a, b, ab };\n}\n"],"mappings":";;AAuCA,MAAM,YAAY,IAAI,OAAO,UAAU;;AAGvC,SAAS,SAAS,GAAmB;CACnC,IAAI,MAAM,GAAG,OAAO,CAAC,OAAO;CAC5B,MAAM,IAAI,IAAI,aAAa,CAAC,CAAC,CAAC;CAC9B,MAAM,OAAO,IAAI,cAAc,EAAE,MAAM;CACvC,KAAK,KAAK,IAAI,IAAI,KAAK,KAAM,KAAK,KAAK,KAAM;CAC7C,OAAO,EAAE;AACX;;AAGA,SAAS,OAAO,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C9B,KAAK,IAAI,IAAI,GAAG,IAAI,GAAG,KAAK,KAAK,EAAE,CAAE,KAAK,UAAU,MAAM,KAAM,EAAE,KAAK,KAAK,CAAC;CAC/E;CACA,OAAO;AACT;;;;;AAMA,SAAgB,iBAAiB,QAAgB,GAAW,GAAW,QAAQ,KAAuB;CACpG,MAAM,oBAAoB,UAAU,MAAM;CAC1C,MAAM,oBAAoB,KAAK,CAAC;CAChC,MAAM,IAAI,UAAU;CACpB,IAAI,IAAI,OAAO,MAAM,IAAI,WAAW,qBAAqB,OAAO,GAAG,EAAE,KAAK,EAAE,4BAA4B,MAAM,EAAE;CAChH,OAAO,MAAM,KAAK,EAAE,QAAQ,EAAE,IAAI,GAAG,SAAS;EAC5C,IAAI,OAAO;EACX,OAAO,MAAM,KAAK,EAAE,QAAQ,EAAE,SAAS;GACrC,MAAM,IAAI,OAAO;GACjB,OAAO,KAAK,MAAM,OAAO,MAAM;GAC/B,OAAO,UAAU,IAAI,EAAE,KAAK,KAAK,MAAM;EACzC,CAAC;CACH,CAAC;AACH;;;;;;AAOA,SAAgB,eAAe,GAAW,GAAW,GAAW,OAA2B,UAA0B;CACnH,MAAM,OAAO,SAAS,UAAU,iBAAiB;CACjD,MAAM,IAAI,KAAK,GAAG,GAAG,EAAE,KAAK,GAAG,CAAC;CAChC,MAAM,IAAI,KAAK,GAAG,GAAG,EAAE,KAAK,GAAG,CAAC;CAEhC,OAAO;EAAE;EAAG;EAAG,IADJ,MAAM,KAAK,EAAE,QAAQ,EAAE,IAAI,GAAG,MAAM,EAAE,KAAK,KAAK,MAAM,IAAI,KAAK,GAAG,MAAO,MAAM,IAAI,EAAE,EAAE,CAAE,KAAM,CAAE,CAAC,CAC7F;CAAE;AACpB"}
1
+ {"version":3,"file":"designs.js","names":[],"sources":["../src/designs.ts"],"sourcesContent":["// Input families and the designs that draw their points (R5). A design gives points of the unit cube. `familyMap`\n// takes each point to a point of the family: a value from the range of each dimension.\nimport type { DistributionSpec } from \"./distributions.ts\";\nimport { distributionOf } from \"./quantiles.ts\";\nimport type { Random } from \"./random.ts\";\n\n/** One dimension of an input family. */\nexport interface Dimension {\n /** The name of the dimension. It is a key of each point. */\n readonly name: string;\n /** The smallest value. */\n readonly min: number;\n /** The largest value. A uniform or log dimension that is not `integer` gets values below `max`. */\n readonly max: number;\n /** The distribution of the values, cut to the range. Without it, the values are uniform in the range. */\n readonly distribution?: DistributionSpec;\n /** With `log`, the logarithm of the value is uniform. `min` must be above 0. A dimension with a distribution has no scale. */\n readonly scale?: \"linear\" | \"log\";\n /** With `true`, each value is an integer. `min` and `max` must be integers. */\n readonly integer?: boolean;\n}\n\n/** An input family: its name and its dimensions. */\nexport interface InputFamily {\n readonly name: string;\n readonly dimensions: readonly Dimension[];\n}\n\n/** One point of a family: one value for each dimension, by name. */\nexport type Point = Readonly<Record<string, number>>;\n\n/** The Saltelli design: the matrices `a` and `b`, and for each dimension `i` the matrix `ab[i]`. */\nexport interface SaltelliDesign {\n readonly a: number[][];\n readonly b: number[][];\n /** `ab[i]` is `a` with the column `i` of `b`. */\n readonly ab: number[][][];\n}\n\nconst BELOW_ONE = 1 - Number.EPSILON / 2;\n\n/** This function gives the next double toward minus infinity. */\nfunction nextDown(x: number): number {\n if (x === 0) return -Number.MIN_VALUE;\n const f = new Float64Array([x]);\n const bits = new BigInt64Array(f.buffer);\n bits[0] = x > 0 ? bits[0]! - 1n : bits[0]! + 1n;\n return f[0]!;\n}\n\n/** This function gives the next double toward plus infinity. */\nfunction nextUp(x: number): number {\n return -nextDown(-x);\n}\n\n/** This function keeps a value below 1. A sum of a stratum and a draw can round up to 1. */\nfunction belowOne(x: number): number {\n return x < 1 ? x : BELOW_ONE;\n}\n\nfunction count(fn: string, name: string, value: number): void {\n if (!Number.isSafeInteger(value) || value < 1) throw new RangeError(`${fn}: ${name} is ${value}, and it must be an integer from 1.`);\n}\n\n/**\n * This function examines a dimension of a family. A range that is not finite, an integer dimension without integer ends and\n * a bad scale are each a `RangeError`.\n */\nexport function checkDimension(family: string, dim: Dimension): void {\n const where = `${family}.${dim.name}`;\n if (!Number.isFinite(dim.min) || !Number.isFinite(dim.max) || dim.min > dim.max) {\n throw new RangeError(`${where}: the range ${dim.min} to ${dim.max} is not a finite range.`);\n }\n if (dim.integer === true && (!Number.isSafeInteger(dim.min) || !Number.isSafeInteger(dim.max))) {\n throw new RangeError(`${where}: an integer dimension needs integer ends, not ${dim.min} and ${dim.max}.`);\n }\n if (dim.distribution !== undefined && dim.scale !== undefined) throw new RangeError(`${where}: a dimension with a distribution has no scale.`);\n if (dim.scale === \"log\" && !(dim.min > 0)) throw new RangeError(`${where}: a log scale needs a minimum above 0, not ${dim.min}.`);\n}\n\n/** This function examines a dimension and gives the map from a uniform number in [0, 1) to a value of the dimension. */\nfunction dimensionMap(family: string, dim: Dimension, r: Random): (u: number) => number {\n const where = `${family}.${dim.name}`;\n checkDimension(family, dim);\n // An integer dimension takes the floor of a value from [min, max + 1).\n const end = dim.integer === true ? dim.max + 1 : dim.max;\n const top = dim.integer === true ? nextDown(end) : end;\n const whole = (x: number): number => (dim.integer === true ? Math.min(dim.max, Math.floor(x)) : x);\n const clamp = (x: number): number => Math.min(top, Math.max(dim.min, x));\n if (dim.distribution !== undefined) {\n const d = distributionOf(dim.distribution, r);\n const low = d.cdf(nextDown(dim.min));\n const high = d.cdf(top);\n if (!(high > low)) throw new RangeError(`${where}: the distribution has no probability from ${dim.min} to ${dim.max}.`);\n // A discrete quantile at `low` can be a value below `min`. The smallest value above `low` is then the first value in the range.\n const first = d.quantile(nextUp(low));\n return (u) => {\n const x = d.quantile(low + u * (high - low));\n return whole(clamp(x < dim.min ? first : x));\n };\n }\n if (dim.scale === \"log\") {\n // The form `min * exp(u * span)` gives `min` exactly at 0.\n const span = Math.log(end / dim.min);\n return (u) => whole(clamp(dim.min * Math.exp(u * span)));\n }\n return (u) => whole(clamp(dim.min + u * (end - dim.min)));\n}\n\n/**\n * This function gives the names of the dimensions of a family. A family with no dimension is a `RangeError`. A name that\n * is empty or not unique is also a `RangeError`.\n */\nexport function familyNames(family: InputFamily): string[] {\n if (family.dimensions.length === 0) throw new RangeError(`${family.name}: a family needs one dimension or more.`);\n const names = family.dimensions.map((d) => d.name);\n const twice = names.find((n, i) => n === \"\" || names.indexOf(n) !== i);\n if (twice !== undefined) throw new RangeError(`${family.name}: the dimension name \"${twice}\" is empty or not unique.`);\n return names;\n}\n\n/**\n * This function gives the map from a point of the unit cube to a point of the family. Coordinate `i` gives the value of\n * dimension `i`. The parameters of each distribution come from `r.fork(\"dimension:\" + name)` one time. Thus the\n * same seed gives the same map, and a new dimension does not change the others.\n */\nexport function familyMap(family: InputFamily, r: Random): (unit: readonly number[]) => Point {\n const names = familyNames(family);\n const maps = family.dimensions.map((d) => dimensionMap(family.name, d, r.fork(`dimension:${d.name}`)));\n return (unit) => {\n if (unit.length !== maps.length) throw new RangeError(`${family.name}: a point needs ${maps.length} coordinates, not ${unit.length}.`);\n const point: Record<string, number> = {};\n maps.forEach((map, i) => {\n const u = unit[i]!;\n if (!(u >= 0 && u < 1)) throw new RangeError(`${family.name}: coordinate ${i} is ${u}, and it must be from 0 to below 1.`);\n point[names[i]!] = map(u);\n });\n return point;\n };\n}\n\n/** This function gives the points of the family at the points of a unit design. */\nexport function familyPoints(family: InputFamily, unit: readonly (readonly number[])[], r: Random): Point[] {\n const map = familyMap(family, r);\n return unit.map(map);\n}\n\n/** This function gives `n` independent uniform points of the unit cube in `d` dimensions. The draws go row by row. */\nexport function randomDesign(n: number, d: number, r: Random): number[][] {\n count(\"randomDesign\", \"n\", n);\n count(\"randomDesign\", \"d\", d);\n return Array.from({ length: n }, () => Array.from({ length: d }, () => r.next()));\n}\n\n/**\n * This function gives a Latin hypercube of `n` points in `d` dimensions. Each column has exactly one point in each of\n * the `n` strata `[k/n, (k+1)/n)`. For each column, the draws are one shuffle of the strata, then one draw in each point.\n */\nexport function latinHypercube(n: number, d: number, r: Random): number[][] {\n count(\"latinHypercube\", \"n\", n);\n count(\"latinHypercube\", \"d\", d);\n const rows = Array.from({ length: n }, () => new Array<number>(d));\n const strata = Array.from({ length: n }, (_, k) => k);\n for (let j = 0; j < d; j++) {\n const order = r.shuffle(strata);\n for (let i = 0; i < n; i++) rows[i]![j] = belowOne((order[i]! + r.next()) / n);\n }\n return rows;\n}\n\n/**\n * This function gives a stratified grid: `strata` strata on each axis, so `strata^d` cells, and one uniform point in\n * each cell. The cells go in the order of their index in base `strata`, with the first dimension as the last digit.\n */\nexport function stratifiedDesign(strata: number, d: number, r: Random, limit = 1_000_000): number[][] {\n count(\"stratifiedDesign\", \"strata\", strata);\n count(\"stratifiedDesign\", \"d\", d);\n const n = strata ** d;\n if (n > limit) throw new RangeError(`stratifiedDesign: ${strata}^${d} = ${n} cells is above the limit ${limit}.`);\n return Array.from({ length: n }, (_, cell) => {\n let rest = cell;\n return Array.from({ length: d }, () => {\n const k = rest % strata;\n rest = Math.floor(rest / strata);\n return belowOne((k + r.next()) / strata);\n });\n });\n}\n\n/**\n * This function gives the Saltelli design for the Sobol indices of a model with `d` inputs: `a`, `b` and the `d`\n * matrices `ab`. The design has `n * (d + 2)` points for the model. `a` comes from `r.fork(\"A\")`, and `b` comes\n * from `r.fork(\"B\")`. With `base` `\"latin\"`, each is a Latin hypercube.\n */\nexport function saltelliDesign(n: number, d: number, r: Random, base: \"random\" | \"latin\" = \"random\"): SaltelliDesign {\n const make = base === \"latin\" ? latinHypercube : randomDesign;\n const a = make(n, d, r.fork(\"A\"));\n const b = make(n, d, r.fork(\"B\"));\n const ab = Array.from({ length: d }, (_, i) => a.map((row, k) => row.map((x, j) => (j === i ? b[k]![j]! : x))));\n return { a, b, ab };\n}\n\n/**\n * This function gives a design of one dimension at a time: one block of `n` points for each dimension, in the order of the\n * dimensions. In the block of a dimension, the dimension takes the values of a Latin column of `n` strata. Each other\n * dimension keeps its value in `base`. The column of a dimension comes from `r.fork(\"column:\" + name)`, and the map comes\n * from `familyMap(family, r)`. Thus a new dimension does not change the blocks of the others. A base value can be out of\n * the range of its dimension, for example 0 for a log dimension. A base with no finite value for a dimension is a `RangeError`.\n * A fraction for an integer dimension and a base value with no dimension are also a `RangeError`.\n */\nexport function oneAtATimePoints(family: InputFamily, n: number, base: Point, r: Random): Point[] {\n count(\"oneAtATimePoints\", \"n\", n);\n const map = familyMap(family, r);\n const names = familyNames(family);\n const extra = Object.keys(base).find((name) => !names.includes(name));\n if (extra !== undefined) throw new RangeError(`${family.name}: the base gives \"${extra}\", and the family has no dimension with this name.`);\n for (const dim of family.dimensions) {\n const value = base[dim.name];\n if (value === undefined || !Number.isFinite(value) || (dim.integer === true && !Number.isInteger(value))) {\n throw new RangeError(`${family.name}.${dim.name}: the base value ${value} is not a value of the dimension.`);\n }\n }\n return names.flatMap((name, i) =>\n latinHypercube(n, 1, r.fork(`column:${name}`)).map(([u]) => {\n const unit = names.map(() => 0);\n unit[i] = u!;\n const moved = map(unit)[name]!;\n return Object.fromEntries(names.map((each) => [each, each === name ? moved : base[each]!]));\n }),\n );\n}\n"],"mappings":";;AAuCA,MAAM,YAAY,IAAI,OAAO,UAAU;;AAGvC,SAAS,SAAS,GAAmB;CACnC,IAAI,MAAM,GAAG,OAAO,CAAC,OAAO;CAC5B,MAAM,IAAI,IAAI,aAAa,CAAC,CAAC,CAAC;CAC9B,MAAM,OAAO,IAAI,cAAc,EAAE,MAAM;CACvC,KAAK,KAAK,IAAI,IAAI,KAAK,KAAM,KAAK,KAAK,KAAM;CAC7C,OAAO,EAAE;AACX;;AAGA,SAAS,OAAO,GAAmB;CACjC,OAAO,CAAC,SAAS,CAAC,CAAC;AACrB;;AAGA,SAAS,SAAS,GAAmB;CACnC,OAAO,IAAI,IAAI,IAAI;AACrB;AAEA,SAAS,MAAM,IAAY,MAAc,OAAqB;CAC5D,IAAI,CAAC,OAAO,cAAc,KAAK,KAAK,QAAQ,GAAG,MAAM,IAAI,WAAW,GAAG,GAAG,IAAI,KAAK,MAAM,MAAM,oCAAoC;AACrI;;;;;AAMA,SA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AO,SAAS,KAAK,KAAM,IAAI,YAAY,QAAQ,CAAC,OAAO,UAAU,KAAK,GACpG,MAAM,IAAI,WAAW,GAAG,OAAO,KAAK,GAAG,IAAI,KAAK,mBAAmB,MAAM,kCAAkC;CAE/G;CACA,OAAO,MAAM,SAAS,MAAM,MAC1B,eAAe,GAAG,GAAG,EAAE,KAAK,UAAU,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO;EAC1D,MAAM,OAAO,MAAM,UAAU,CAAC;EAC9B,KAAK,KAAK;EACV,MAAM,QAAQ,IAAI,IAAI,CAAC,CAAC;EACxB,OAAO,OAAO,YAAY,MAAM,KAAK,SAAS,CAAC,MAAM,SAAS,OAAO,QAAQ,KAAK,KAAM,CAAC,CAAC;CAC5F,CAAC,CACH;AACF"}
package/build/hash.d.ts CHANGED
@@ -32,8 +32,8 @@ export interface CanonicalOptions {
32
32
  /**
33
33
  * The form of a `Date`, a `RegExp`, a `Map`, a `Set` and a `bigint`:
34
34
  *
35
- * - `"none"` (the default): the form of `JSON.stringify`. A `Date` gives the text of its `toJSON`.
36
- * A `RegExp`, a `Map` and a `Set` give `{}`, so two of them with other contents get the same hash.
35
+ * - `"none"` (the default): a `Date` gives the text of its `toJSON`, as in `JSON.stringify`.
36
+ * A `RegExp`, a `Map` and a `Set` cause a `TypeError`, because `JSON.stringify` writes them as `{}` and loses their content.
37
37
  * - `"typed"`: each value gets a tag with its type and its value, for example `Date(0)`, `RegExp("/a/g")`, `Map([1,"a"])`, `Set(1,2)` and `12n`.
38
38
  *
39
39
  * The entries of a `Map` and the items of a `Set` are in the order of their text. Thus the order of insertion does not change the form.
@@ -48,9 +48,16 @@ export interface CanonicalOptions {
48
48
  * - The numbers and the strings have the form of `JSON.stringify`, which is the form of RFC 8785.
49
49
  * - A key with the value `undefined` is not in the form. An `undefined` in an array, or as the value, becomes `null`.
50
50
  * - An object with a method `toJSON`, for example a `Date`, gives the form of the result of that method.
51
+ * - A boxed `Number`, `String`, `Boolean` or `BigInt` gives the form of its primitive value, as in `JSON.stringify`.
52
+ * - A plain object, an array, an `arguments` object and a typed array give the form of their own enumerable keys.
53
+ * - Each other object causes a `TypeError`, because `JSON.stringify` writes it as `{}` and loses its content.
54
+ * Examples are a `Map`, a `Set`, a `RegExp`, an `Error`, a `WeakMap` and a `Promise`.
51
55
  * - A function, a symbol and a cycle cause a `TypeError`.
52
56
  * - With the option `tags: "typed"`, a `Date`, a `RegExp`, a `Map` and a `Set` get a tag with their value. The option has the forms.
53
57
  *
58
+ * The type of an object comes from `Object.prototype.toString` and from its internal slots. Thus a value from another realm gets the same form.
59
+ * An object with its own `Symbol.toStringTag` needs a method `toJSON`. The form does not see private fields.
60
+ *
54
61
  * `canonicalJson(JSON.parse(canonicalJson(v)))` is equal to `canonicalJson(v)`, with no tags.
55
62
  */
56
63
  export declare function canonicalJson(value: unknown, options?: CanonicalOptions): string;
@@ -1 +1 @@
1
- {"version":3,"file":"hash.d.ts","names":[],"sources":["../src/hash.ts"],"mappings":";iBAiBiB;;;;;;;;;WASN;;;;;;wBAmDK,UAAU,cAAc,UAAU;;iBAwDjC;;;;;;;WAON;;;;;WAKA;;;;;;;;;;;WAWA;;;;;;;;;;;;;;wBAeK,cAAc,gBAAgB,UAAU;;wBAsExC,cAAc,gBAAgB,UAAU"}
1
+ {"version":3,"file":"hash.d.ts","names":[],"sources":["../src/hash.ts"],"mappings":";iBAiBiB;;;;;;;;;WASN;;;;;;wBAmDK,UAAU,cAAc,UAAU;;iBAwDjC;;;;;;;WAON;;;;;WAKA;;;;;;;;;;;WAWA;;;;;;;;;;;;;;;;;;;;;wBA2DK,cAAc,gBAAgB,UAAU;;wBAyFxC,cAAc,gBAAgB,UAAU"}
package/build/hash.js CHANGED
@@ -179,6 +179,41 @@ function sha256Hex(text, options) {
179
179
  }
180
180
  return h.map((x) => x.toString(16).padStart(8, "0")).join("");
181
181
  }
182
+ /** The getter of `RegExp.prototype.source`. It causes a `TypeError` for an object with no slot of a `RegExp`. */
183
+ const sourceOf = Object.getOwnPropertyDescriptor(RegExp.prototype, "source").get;
184
+ /** The getter of the `Symbol.toStringTag` of the typed arrays. It gives `undefined` for an object that is not a typed array. */
185
+ const typedArrayName = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(Uint8Array.prototype), Symbol.toStringTag).get;
186
+ /** The types that the option `tags: "typed"` writes with a tag. */
187
+ const tagged = /* @__PURE__ */ new Set([
188
+ "Date",
189
+ "RegExp",
190
+ "Map",
191
+ "Set"
192
+ ]);
193
+ /** This function is true for an object, and false for `null` and each primitive. */
194
+ const isObject = (v) => v !== null && typeof v === "object";
195
+ /** This function gives the brand of an object: the tag of `Object.prototype.toString`. The brand is the same in each realm. */
196
+ const brandOf = (o) => Object.prototype.toString.call(o).slice(8, -1);
197
+ /** This function is true when `method` accepts `o` as its `this`. Thus it tests an internal slot, the same in each realm. */
198
+ function hasSlot(method, o) {
199
+ try {
200
+ Reflect.apply(method, o, []);
201
+ return true;
202
+ } catch {
203
+ return false;
204
+ }
205
+ }
206
+ /**
207
+ * This function gives the primitive value of a boxed `Number`, `String`, `Boolean` or `BigInt`, as `JSON.stringify` does.
208
+ * For each other object, it gives the object.
209
+ */
210
+ function unboxed(o, brand) {
211
+ if (brand === "Number" && hasSlot(Number.prototype.valueOf, o)) return Number(o);
212
+ if (brand === "String" && hasSlot(String.prototype.valueOf, o)) return String(o);
213
+ if (brand === "Boolean" && hasSlot(Boolean.prototype.valueOf, o)) return Boolean.prototype.valueOf.call(o);
214
+ if (brand === "BigInt" && hasSlot(BigInt.prototype.valueOf, o)) return BigInt.prototype.valueOf.call(o);
215
+ return o;
216
+ }
182
217
  /**
183
218
  * This function gives the canonical form of a value. The default form is RFC 8785 (JCS):
184
219
  *
@@ -186,9 +221,16 @@ function sha256Hex(text, options) {
186
221
  * - The numbers and the strings have the form of `JSON.stringify`, which is the form of RFC 8785.
187
222
  * - A key with the value `undefined` is not in the form. An `undefined` in an array, or as the value, becomes `null`.
188
223
  * - An object with a method `toJSON`, for example a `Date`, gives the form of the result of that method.
224
+ * - A boxed `Number`, `String`, `Boolean` or `BigInt` gives the form of its primitive value, as in `JSON.stringify`.
225
+ * - A plain object, an array, an `arguments` object and a typed array give the form of their own enumerable keys.
226
+ * - Each other object causes a `TypeError`, because `JSON.stringify` writes it as `{}` and loses its content.
227
+ * Examples are a `Map`, a `Set`, a `RegExp`, an `Error`, a `WeakMap` and a `Promise`.
189
228
  * - A function, a symbol and a cycle cause a `TypeError`.
190
229
  * - With the option `tags: "typed"`, a `Date`, a `RegExp`, a `Map` and a `Set` get a tag with their value. The option has the forms.
191
230
  *
231
+ * The type of an object comes from `Object.prototype.toString` and from its internal slots. Thus a value from another realm gets the same form.
232
+ * An object with its own `Symbol.toStringTag` needs a method `toJSON`. The form does not see private fields.
233
+ *
192
234
  * `canonicalJson(JSON.parse(canonicalJson(v)))` is equal to `canonicalJson(v)`, with no tags.
193
235
  */
194
236
  function canonicalJson(value, options) {
@@ -206,24 +248,39 @@ function canonicalJson(value, options) {
206
248
  return JSON.stringify(s);
207
249
  };
208
250
  const tagOf = (o) => {
209
- if (o instanceof Date) return `Date(${o.getTime()})`;
210
- if (o instanceof RegExp) return `RegExp(${text(String(o))})`;
211
- if (o instanceof Map || o instanceof Set) {
212
- if (open.has(o)) throw new TypeError("canonicalJson: the value has a cycle.");
213
- open.add(o);
214
- const parts = [];
215
- if (o instanceof Map) for (const [k, item] of o) parts.push(`[${write(k, "")},${write(item, "")}]`);
216
- else for (const item of o) parts.push(write(item, ""));
217
- open.delete(o);
218
- return `${o instanceof Map ? "Map" : "Set"}(${parts.sort().join(",")})`;
219
- }
251
+ const brand = brandOf(o);
252
+ if (brand === "Date" && hasSlot(Date.prototype.getTime, o)) return `Date(${Date.prototype.getTime.call(o)})`;
253
+ if (brand === "RegExp" && hasSlot(sourceOf, o)) return `RegExp(${text(RegExp.prototype.toString.call(o))})`;
254
+ const map = brand === "Map" && hasSlot(Map.prototype.has, o);
255
+ if (!map && !(brand === "Set" && hasSlot(Set.prototype.has, o))) return void 0;
256
+ if (open.has(o)) throw new TypeError("canonicalJson: the value has a cycle.");
257
+ open.add(o);
258
+ const parts = [];
259
+ if (map) for (const [k, item] of Map.prototype.entries.call(o)) parts.push(`[${write(k, "")},${write(item, "")}]`);
260
+ else for (const item of Set.prototype.values.call(o)) parts.push(write(item, ""));
261
+ open.delete(o);
262
+ return `${map ? "Map" : "Set"}(${parts.sort().join(",")})`;
263
+ };
264
+ const lost = (brand) => {
265
+ return `canonicalJson: the type ${brand} has no JSON form. JSON.stringify keeps only its own keys, so two of them with other contents can get the same hash. ${!typed && tagged.has(brand) ? "Use the option tags: \"typed\", or give the object a method toJSON." : "Give the object a method toJSON."}`;
220
266
  };
221
267
  const write = (v, key) => {
222
- if (typed && v !== null && typeof v === "object") {
268
+ if (typed && isObject(v)) {
223
269
  const tag = tagOf(v);
224
270
  if (tag !== void 0) return tag;
225
271
  }
226
- if (v !== null && typeof v === "object" && typeof v.toJSON === "function") v = v.toJSON(key);
272
+ if (isObject(v) && typeof v.toJSON === "function") {
273
+ v = v.toJSON(key);
274
+ if (typed && isObject(v)) {
275
+ const tag = tagOf(v);
276
+ if (tag !== void 0) return tag;
277
+ }
278
+ }
279
+ let brand = "Object";
280
+ if (isObject(v)) {
281
+ brand = brandOf(v);
282
+ if (brand !== "Object") v = unboxed(v, brand);
283
+ }
227
284
  if (v === void 0 || v === null) return "null";
228
285
  switch (typeof v) {
229
286
  case "boolean": return v ? "true" : "false";
@@ -239,6 +296,7 @@ function canonicalJson(value, options) {
239
296
  default: throw new TypeError(`canonicalJson: a ${typeof v} has no JSON form.`);
240
297
  }
241
298
  const o = v;
299
+ if (brand !== "Object" && brand !== "Arguments" && !Array.isArray(o) && typedArrayName.call(o) === void 0) throw new TypeError(lost(brand));
242
300
  if (open.has(o)) throw new TypeError("canonicalJson: the value has a cycle.");
243
301
  open.add(o);
244
302
  let out;
package/build/hash.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"hash.js","names":[],"sources":["../src/hash.ts"],"sourcesContent":["/**\n * The content hash of a document: SHA-256 of a string, and a canonical JSON form of a value.\n *\n * The two functions use no API of Node or of the browser, so a hash is the same on each platform.\n * `canonicalHash(value)` is `sha256Hex(canonicalJson(value))`: equal values give equal hashes, also when their keys have another order.\n */\n\n/** The round constants of SHA-256. */\nconst K: readonly number[] = [\n 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe,\n 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,\n 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,\n 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,\n 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,\n];\n\n/** The options of `sha256Hex`. */\nexport interface Sha256Options {\n /**\n * The bytes that the hash reads, as in `fnv1a32`:\n *\n * - `\"utf8\"` (the default): the UTF-8 bytes. A lone surrogate becomes the bytes of U+FFFD, as `TextEncoder` writes them.\n * - `\"wtf8\"`: the UTF-8 bytes of each code point. A lone surrogate becomes its own 3 bytes (WTF-8).\n *\n * For a string without a lone surrogate, the two inputs give the same hash.\n */\n readonly input?: \"utf8\" | \"wtf8\";\n}\n\n/**\n * This function writes the bytes of a string into `out` and gives the count of the bytes. With no `out`, it only counts.\n * It reads the UTF-16 code units, so it makes no string for each character.\n */\nfunction writeBytes(text: string, wtf8: boolean, out?: Uint8Array): number {\n let n = 0;\n for (let i = 0; i < text.length; i++) {\n let c = text.charCodeAt(i);\n if (c >= 0xd800 && c < 0xe000) {\n const low = i + 1 < text.length ? text.charCodeAt(i + 1) : 0;\n if (c < 0xdc00 && low >= 0xdc00 && low < 0xe000) {\n c = 0x10000 + ((c - 0xd800) << 10) + (low - 0xdc00);\n i++;\n } else if (!wtf8) c = 0xfffd;\n }\n if (c < 0x80) {\n if (out !== undefined) out[n] = c;\n n += 1;\n } else if (c < 0x800) {\n if (out !== undefined) {\n out[n] = 0xc0 | (c >> 6);\n out[n + 1] = 0x80 | (c & 63);\n }\n n += 2;\n } else if (c < 0x10000) {\n if (out !== undefined) {\n out[n] = 0xe0 | (c >> 12);\n out[n + 1] = 0x80 | ((c >> 6) & 63);\n out[n + 2] = 0x80 | (c & 63);\n }\n n += 3;\n } else {\n if (out !== undefined) {\n out[n] = 0xf0 | (c >> 18);\n out[n + 1] = 0x80 | ((c >> 12) & 63);\n out[n + 2] = 0x80 | ((c >> 6) & 63);\n out[n + 3] = 0x80 | (c & 63);\n }\n n += 4;\n }\n }\n return n;\n}\n\n/**\n * This function gives the SHA-256 digest of the bytes of a string, as 64 lowercase hex characters.\n * The option `input` selects the bytes of a lone surrogate. It is not for passwords: it has no salt and no cost.\n */\nexport function sha256Hex(text: string, options?: Sha256Options): string {\n const input = options?.input ?? \"utf8\";\n if (input !== \"utf8\" && input !== \"wtf8\") throw new RangeError(`sha256Hex: input is \"utf8\" or \"wtf8\", not ${String(input)}.`);\n const wtf8 = input === \"wtf8\";\n // The bytes go straight into the padded buffer, so the digest makes one array, not a list of the bytes and a copy.\n const length = writeBytes(text, wtf8);\n const bits = length * 8;\n const total = Math.ceil((length + 9) / 64) * 64;\n const data = new Uint8Array(total);\n writeBytes(text, wtf8, data);\n data[length] = 0x80;\n const view = new DataView(data.buffer);\n view.setUint32(total - 8, Math.floor(bits / 0x100000000));\n view.setUint32(total - 4, bits >>> 0);\n const h = [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19];\n const w = new Uint32Array(64);\n const rotr = (x: number, n: number): number => (x >>> n) | (x << (32 - n));\n for (let off = 0; off < total; off += 64) {\n for (let i = 0; i < 16; i++) w[i] = view.getUint32(off + 4 * i);\n for (let i = 16; i < 64; i++) {\n const a = w[i - 15]!;\n const b = w[i - 2]!;\n const s0 = rotr(a, 7) ^ rotr(a, 18) ^ (a >>> 3);\n const s1 = rotr(b, 17) ^ rotr(b, 19) ^ (b >>> 10);\n w[i] = (w[i - 16]! + s0 + w[i - 7]! + s1) >>> 0;\n }\n let [a, b, c, d, e, f, g, k] = h as [number, number, number, number, number, number, number, number];\n for (let i = 0; i < 64; i++) {\n const s1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25);\n const ch = (e & f) ^ (~e & g);\n const t1 = (k + s1 + ch + K[i]! + w[i]!) >>> 0;\n const s0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22);\n const maj = (a & b) ^ (a & c) ^ (b & c);\n const t2 = (s0 + maj) >>> 0;\n k = g;\n g = f;\n f = e;\n e = (d + t1) >>> 0;\n d = c;\n c = b;\n b = a;\n a = (t1 + t2) >>> 0;\n }\n h[0] = (h[0]! + a) >>> 0;\n h[1] = (h[1]! + b) >>> 0;\n h[2] = (h[2]! + c) >>> 0;\n h[3] = (h[3]! + d) >>> 0;\n h[4] = (h[4]! + e) >>> 0;\n h[5] = (h[5]! + f) >>> 0;\n h[6] = (h[6]! + g) >>> 0;\n h[7] = (h[7]! + k) >>> 0;\n }\n return h.map((x) => x.toString(16).padStart(8, \"0\")).join(\"\");\n}\n\n/** The options of `canonicalJson` and `canonicalHash`. */\nexport interface CanonicalOptions {\n /**\n * The form of the values that JSON does not have:\n *\n * - `\"jcs\"` (the default): RFC 8785. `NaN`, an infinite number and a string with a lone surrogate cause a `RangeError`.\n * - `\"json\"`: the form of `JSON.stringify`. `NaN` and an infinite number become `null`, and a lone surrogate becomes an escape.\n */\n readonly form?: \"jcs\" | \"json\";\n /**\n * The form of a `bigint`. `\"error\"` (the default) causes a `TypeError`, as `JSON.stringify` does.\n * `\"suffix\"` writes the digits and the letter `n`, for example `12n`. This text is not JSON, so use it only for a key.\n */\n readonly bigint?: \"error\" | \"suffix\";\n /**\n * The form of a `Date`, a `RegExp`, a `Map`, a `Set` and a `bigint`:\n *\n * - `\"none\"` (the default): the form of `JSON.stringify`. A `Date` gives the text of its `toJSON`.\n * A `RegExp`, a `Map` and a `Set` give `{}`, so two of them with other contents get the same hash.\n * - `\"typed\"`: each value gets a tag with its type and its value, for example `Date(0)`, `RegExp(\"/a/g\")`, `Map([1,\"a\"])`, `Set(1,2)` and `12n`.\n *\n * The entries of a `Map` and the items of a `Set` are in the order of their text. Thus the order of insertion does not change the form.\n * With `\"typed\"`, the default of `bigint` is `\"suffix\"`. The text with tags is not JSON, so use it only for a key or a hash.\n */\n readonly tags?: \"none\" | \"typed\";\n}\n\n/**\n * This function gives the canonical form of a value. The default form is RFC 8785 (JCS):\n *\n * - The keys of each object are in the order of their UTF-16 code units, with no whitespace.\n * - The numbers and the strings have the form of `JSON.stringify`, which is the form of RFC 8785.\n * - A key with the value `undefined` is not in the form. An `undefined` in an array, or as the value, becomes `null`.\n * - An object with a method `toJSON`, for example a `Date`, gives the form of the result of that method.\n * - A function, a symbol and a cycle cause a `TypeError`.\n * - With the option `tags: \"typed\"`, a `Date`, a `RegExp`, a `Map` and a `Set` get a tag with their value. The option has the forms.\n *\n * `canonicalJson(JSON.parse(canonicalJson(v)))` is equal to `canonicalJson(v)`, with no tags.\n */\nexport function canonicalJson(value: unknown, options?: CanonicalOptions): string {\n const form = options?.form ?? \"jcs\";\n const tags = options?.tags ?? \"none\";\n if (tags !== \"none\" && tags !== \"typed\") throw new RangeError(`canonicalJson: tags is \"none\" or \"typed\", not ${String(tags)}.`);\n const typed = tags === \"typed\";\n const bigint = options?.bigint ?? (typed ? \"suffix\" : \"error\");\n if (form !== \"jcs\" && form !== \"json\") throw new RangeError(`canonicalJson: form is \"jcs\" or \"json\", not ${String(form)}.`);\n if (bigint !== \"error\" && bigint !== \"suffix\") throw new RangeError(`canonicalJson: bigint is \"error\" or \"suffix\", not ${String(bigint)}.`);\n const jcs = form === \"jcs\";\n const open = new Set<object>();\n const text = (s: string): string => {\n if (jcs && !(s as string & { isWellFormed(): boolean }).isWellFormed()) throw new RangeError(`canonicalJson: the string ${JSON.stringify(s)} has a lone surrogate, which RFC 8785 does not allow.`);\n return JSON.stringify(s);\n };\n // This function gives the tag of a Date, a RegExp, a Map or a Set, and undefined for each other object.\n const tagOf = (o: object): string | undefined => {\n if (o instanceof Date) return `Date(${o.getTime()})`;\n if (o instanceof RegExp) return `RegExp(${text(String(o))})`;\n if (o instanceof Map || o instanceof Set) {\n if (open.has(o)) throw new TypeError(\"canonicalJson: the value has a cycle.\");\n open.add(o);\n const parts: string[] = [];\n if (o instanceof Map) for (const [k, item] of o) parts.push(`[${write(k, \"\")},${write(item, \"\")}]`);\n else for (const item of o) parts.push(write(item, \"\"));\n open.delete(o);\n return `${o instanceof Map ? \"Map\" : \"Set\"}(${parts.sort().join(\",\")})`;\n }\n return undefined;\n };\n const write = (v: unknown, key: string): string => {\n if (typed && v !== null && typeof v === \"object\") {\n const tag = tagOf(v);\n if (tag !== undefined) return tag;\n }\n if (v !== null && typeof v === \"object\" && typeof (v as { toJSON?: unknown }).toJSON === \"function\") v = (v as { toJSON(key: string): unknown }).toJSON(key);\n if (v === undefined || v === null) return \"null\";\n switch (typeof v) {\n case \"boolean\":\n return v ? \"true\" : \"false\";\n case \"number\":\n if (Number.isFinite(v)) return JSON.stringify(v);\n if (jcs) throw new RangeError(`canonicalJson: ${String(v)} is not a JSON number.`);\n return \"null\";\n case \"string\":\n return text(v);\n case \"bigint\":\n if (bigint === \"suffix\") return `${v}n`;\n throw new TypeError(`canonicalJson: the bigint ${v}n has no JSON form. Use the option bigint: \"suffix\" for a key.`);\n case \"object\":\n break;\n default:\n throw new TypeError(`canonicalJson: a ${typeof v} has no JSON form.`);\n }\n const o = v as object;\n if (open.has(o)) throw new TypeError(\"canonicalJson: the value has a cycle.\");\n open.add(o);\n let out: string;\n if (Array.isArray(o)) out = `[${o.map((item: unknown, i) => write(item, String(i))).join(\",\")}]`;\n else {\n const r = o as Record<string, unknown>;\n const keys = Object.keys(r).filter((k) => r[k] !== undefined).sort();\n out = `{${keys.map((k) => `${text(k)}:${write(r[k], k)}`).join(\",\")}}`;\n }\n open.delete(o);\n return out;\n };\n return write(value, \"\");\n}\n\n/** This function gives the content hash of a value: `sha256Hex(canonicalJson(value, options))`. */\nexport function canonicalHash(value: unknown, options?: CanonicalOptions): string {\n return sha256Hex(canonicalJson(value, options));\n}\n"],"mappings":";;;;;;;;AAQA,MAAM,IAAuB;CAC3B;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAC5J;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAC5J;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAC5J;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAC5J;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;AACtF;;;;;AAmBA,SAAS,WAAW,MAAc,MAAe,KAA0B;CACzE,IAAI,IAAI;CACR,KAAK,IAAI,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK;EACpC,IAAI,IAAI,KAAK,WAAW,CAAC;EACzB,IAAI,KAAK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rC,MAAM,IAAI,UAAU,6BAA6B,EAAE,+DAA+D;GACpH,KAAK,UACH;GACF,SACE,MAAM,IAAI,UAAU,oBAAoB,OAAO,EAAE,mBAAmB;EACxE;EACA,MAAM,IAAI;EACV,IAAI,KAAK,IAAI,CAAC,GAAG,MAAM,IAAI,UAAU,uCAAuC;EAC5E,KAAK,IAAI,CAAC;EACV,IAAI;EACJ,IAAI,MAAM,QAAQ,CAAC,GAAG,MAAM,IAAI,EAAE,KAAK,MAAe,MAAM,MAAM,MAAM,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,GAAG,EAAE;OACzF;GACH,MAAM,IAAI;GAEV,MAAM,IADO,OAAO,KAAK,CAAC,CAAC,CAAC,QAAQ,MAAM,EAAE,OAAO,KAAA,CAAS,CAAC,CAAC,KACjD,CAAC,CAAC,KAAK,MAAM,GAAG,KAAK,CAAC,EAAE,GAAG,MAAM,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,KAAK,GAAG,EAAE;EACtE;EACA,KAAK,OAAO,CAAC;EACb,OAAO;CACT;CACA,OAAO,MAAM,OAAO,EAAE;AACxB;;AAGA,SAAgB,cAAc,OAAgB,SAAoC;CAChF,OAAO,UAAU,cAAc,OAAO,OAAO,CAAC;AAChD"}
1
+ {"version":3,"file":"hash.js","names":[],"sources":["../src/hash.ts"],"sourcesContent":["/**\n * The content hash of a document: SHA-256 of a string, and a canonical JSON form of a value.\n *\n * The two functions use no API of Node or of the browser, so a hash is the same on each platform.\n * `canonicalHash(value)` is `sha256Hex(canonicalJson(value))`: equal values give equal hashes, also when their keys have another order.\n */\n\n/** The round constants of SHA-256. */\nconst K: readonly number[] = [\n 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe,\n 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,\n 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,\n 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,\n 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,\n];\n\n/** The options of `sha256Hex`. */\nexport interface Sha256Options {\n /**\n * The bytes that the hash reads, as in `fnv1a32`:\n *\n * - `\"utf8\"` (the default): the UTF-8 bytes. A lone surrogate becomes the bytes of U+FFFD, as `TextEncoder` writes them.\n * - `\"wtf8\"`: the UTF-8 bytes of each code point. A lone surrogate becomes its own 3 bytes (WTF-8).\n *\n * For a string without a lone surrogate, the two inputs give the same hash.\n */\n readonly input?: \"utf8\" | \"wtf8\";\n}\n\n/**\n * This function writes the bytes of a string into `out` and gives the count of the bytes. With no `out`, it only counts.\n * It reads the UTF-16 code units, so it makes no string for each character.\n */\nfunction writeBytes(text: string, wtf8: boolean, out?: Uint8Array): number {\n let n = 0;\n for (let i = 0; i < text.length; i++) {\n let c = text.charCodeAt(i);\n if (c >= 0xd800 && c < 0xe000) {\n const low = i + 1 < text.length ? text.charCodeAt(i + 1) : 0;\n if (c < 0xdc00 && low >= 0xdc00 && low < 0xe000) {\n c = 0x10000 + ((c - 0xd800) << 10) + (low - 0xdc00);\n i++;\n } else if (!wtf8) c = 0xfffd;\n }\n if (c < 0x80) {\n if (out !== undefined) out[n] = c;\n n += 1;\n } else if (c < 0x800) {\n if (out !== undefined) {\n out[n] = 0xc0 | (c >> 6);\n out[n + 1] = 0x80 | (c & 63);\n }\n n += 2;\n } else if (c < 0x10000) {\n if (out !== undefined) {\n out[n] = 0xe0 | (c >> 12);\n out[n + 1] = 0x80 | ((c >> 6) & 63);\n out[n + 2] = 0x80 | (c & 63);\n }\n n += 3;\n } else {\n if (out !== undefined) {\n out[n] = 0xf0 | (c >> 18);\n out[n + 1] = 0x80 | ((c >> 12) & 63);\n out[n + 2] = 0x80 | ((c >> 6) & 63);\n out[n + 3] = 0x80 | (c & 63);\n }\n n += 4;\n }\n }\n return n;\n}\n\n/**\n * This function gives the SHA-256 digest of the bytes of a string, as 64 lowercase hex characters.\n * The option `input` selects the bytes of a lone surrogate. It is not for passwords: it has no salt and no cost.\n */\nexport function sha256Hex(text: string, options?: Sha256Options): string {\n const input = options?.input ?? \"utf8\";\n if (input !== \"utf8\" && input !== \"wtf8\") throw new RangeError(`sha256Hex: input is \"utf8\" or \"wtf8\", not ${String(input)}.`);\n const wtf8 = input === \"wtf8\";\n // The bytes go straight into the padded buffer, so the digest makes one array, not a list of the bytes and a copy.\n const length = writeBytes(text, wtf8);\n const bits = length * 8;\n const total = Math.ceil((length + 9) / 64) * 64;\n const data = new Uint8Array(total);\n writeBytes(text, wtf8, data);\n data[length] = 0x80;\n const view = new DataView(data.buffer);\n view.setUint32(total - 8, Math.floor(bits / 0x100000000));\n view.setUint32(total - 4, bits >>> 0);\n const h = [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19];\n const w = new Uint32Array(64);\n const rotr = (x: number, n: number): number => (x >>> n) | (x << (32 - n));\n for (let off = 0; off < total; off += 64) {\n for (let i = 0; i < 16; i++) w[i] = view.getUint32(off + 4 * i);\n for (let i = 16; i < 64; i++) {\n const a = w[i - 15]!;\n const b = w[i - 2]!;\n const s0 = rotr(a, 7) ^ rotr(a, 18) ^ (a >>> 3);\n const s1 = rotr(b, 17) ^ rotr(b, 19) ^ (b >>> 10);\n w[i] = (w[i - 16]! + s0 + w[i - 7]! + s1) >>> 0;\n }\n let [a, b, c, d, e, f, g, k] = h as [number, number, number, number, number, number, number, number];\n for (let i = 0; i < 64; i++) {\n const s1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25);\n const ch = (e & f) ^ (~e & g);\n const t1 = (k + s1 + ch + K[i]! + w[i]!) >>> 0;\n const s0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22);\n const maj = (a & b) ^ (a & c) ^ (b & c);\n const t2 = (s0 + maj) >>> 0;\n k = g;\n g = f;\n f = e;\n e = (d + t1) >>> 0;\n d = c;\n c = b;\n b = a;\n a = (t1 + t2) >>> 0;\n }\n h[0] = (h[0]! + a) >>> 0;\n h[1] = (h[1]! + b) >>> 0;\n h[2] = (h[2]! + c) >>> 0;\n h[3] = (h[3]! + d) >>> 0;\n h[4] = (h[4]! + e) >>> 0;\n h[5] = (h[5]! + f) >>> 0;\n h[6] = (h[6]! + g) >>> 0;\n h[7] = (h[7]! + k) >>> 0;\n }\n return h.map((x) => x.toString(16).padStart(8, \"0\")).join(\"\");\n}\n\n/** The options of `canonicalJson` and `canonicalHash`. */\nexport interface CanonicalOptions {\n /**\n * The form of the values that JSON does not have:\n *\n * - `\"jcs\"` (the default): RFC 8785. `NaN`, an infinite number and a string with a lone surrogate cause a `RangeError`.\n * - `\"json\"`: the form of `JSON.stringify`. `NaN` and an infinite number become `null`, and a lone surrogate becomes an escape.\n */\n readonly form?: \"jcs\" | \"json\";\n /**\n * The form of a `bigint`. `\"error\"` (the default) causes a `TypeError`, as `JSON.stringify` does.\n * `\"suffix\"` writes the digits and the letter `n`, for example `12n`. This text is not JSON, so use it only for a key.\n */\n readonly bigint?: \"error\" | \"suffix\";\n /**\n * The form of a `Date`, a `RegExp`, a `Map`, a `Set` and a `bigint`:\n *\n * - `\"none\"` (the default): a `Date` gives the text of its `toJSON`, as in `JSON.stringify`.\n * A `RegExp`, a `Map` and a `Set` cause a `TypeError`, because `JSON.stringify` writes them as `{}` and loses their content.\n * - `\"typed\"`: each value gets a tag with its type and its value, for example `Date(0)`, `RegExp(\"/a/g\")`, `Map([1,\"a\"])`, `Set(1,2)` and `12n`.\n *\n * The entries of a `Map` and the items of a `Set` are in the order of their text. Thus the order of insertion does not change the form.\n * With `\"typed\"`, the default of `bigint` is `\"suffix\"`. The text with tags is not JSON, so use it only for a key or a hash.\n */\n readonly tags?: \"none\" | \"typed\";\n}\n\n/** The getter of `RegExp.prototype.source`. It causes a `TypeError` for an object with no slot of a `RegExp`. */\nconst sourceOf = Object.getOwnPropertyDescriptor(RegExp.prototype, \"source\")!.get!;\n\n/** The getter of the `Symbol.toStringTag` of the typed arrays. It gives `undefined` for an object that is not a typed array. */\nconst typedArrayName = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(Uint8Array.prototype) as object, Symbol.toStringTag)!.get! as (this: object) => string | undefined;\n\n/** The types that the option `tags: \"typed\"` writes with a tag. */\nconst tagged: ReadonlySet<string> = new Set([\"Date\", \"RegExp\", \"Map\", \"Set\"]);\n\n/** This function is true for an object, and false for `null` and each primitive. */\nconst isObject = (v: unknown): v is object => v !== null && typeof v === \"object\";\n\n/** This function gives the brand of an object: the tag of `Object.prototype.toString`. The brand is the same in each realm. */\nconst brandOf = (o: object): string => Object.prototype.toString.call(o).slice(8, -1);\n\n/** This function is true when `method` accepts `o` as its `this`. Thus it tests an internal slot, the same in each realm. */\nfunction hasSlot(method: (...args: never[]) => unknown, o: object): boolean {\n try {\n Reflect.apply(method, o, []);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * This function gives the primitive value of a boxed `Number`, `String`, `Boolean` or `BigInt`, as `JSON.stringify` does.\n * For each other object, it gives the object.\n */\nfunction unboxed(o: object, brand: string): unknown {\n if (brand === \"Number\" && hasSlot(Number.prototype.valueOf, o)) return Number(o);\n if (brand === \"String\" && hasSlot(String.prototype.valueOf, o)) return String(o);\n if (brand === \"Boolean\" && hasSlot(Boolean.prototype.valueOf, o)) return Boolean.prototype.valueOf.call(o);\n if (brand === \"BigInt\" && hasSlot(BigInt.prototype.valueOf, o)) return BigInt.prototype.valueOf.call(o);\n return o;\n}\n\n/**\n * This function gives the canonical form of a value. The default form is RFC 8785 (JCS):\n *\n * - The keys of each object are in the order of their UTF-16 code units, with no whitespace.\n * - The numbers and the strings have the form of `JSON.stringify`, which is the form of RFC 8785.\n * - A key with the value `undefined` is not in the form. An `undefined` in an array, or as the value, becomes `null`.\n * - An object with a method `toJSON`, for example a `Date`, gives the form of the result of that method.\n * - A boxed `Number`, `String`, `Boolean` or `BigInt` gives the form of its primitive value, as in `JSON.stringify`.\n * - A plain object, an array, an `arguments` object and a typed array give the form of their own enumerable keys.\n * - Each other object causes a `TypeError`, because `JSON.stringify` writes it as `{}` and loses its content.\n * Examples are a `Map`, a `Set`, a `RegExp`, an `Error`, a `WeakMap` and a `Promise`.\n * - A function, a symbol and a cycle cause a `TypeError`.\n * - With the option `tags: \"typed\"`, a `Date`, a `RegExp`, a `Map` and a `Set` get a tag with their value. The option has the forms.\n *\n * The type of an object comes from `Object.prototype.toString` and from its internal slots. Thus a value from another realm gets the same form.\n * An object with its own `Symbol.toStringTag` needs a method `toJSON`. The form does not see private fields.\n *\n * `canonicalJson(JSON.parse(canonicalJson(v)))` is equal to `canonicalJson(v)`, with no tags.\n */\nexport function canonicalJson(value: unknown, options?: CanonicalOptions): string {\n const form = options?.form ?? \"jcs\";\n const tags = options?.tags ?? \"none\";\n if (tags !== \"none\" && tags !== \"typed\") throw new RangeError(`canonicalJson: tags is \"none\" or \"typed\", not ${String(tags)}.`);\n const typed = tags === \"typed\";\n const bigint = options?.bigint ?? (typed ? \"suffix\" : \"error\");\n if (form !== \"jcs\" && form !== \"json\") throw new RangeError(`canonicalJson: form is \"jcs\" or \"json\", not ${String(form)}.`);\n if (bigint !== \"error\" && bigint !== \"suffix\") throw new RangeError(`canonicalJson: bigint is \"error\" or \"suffix\", not ${String(bigint)}.`);\n const jcs = form === \"jcs\";\n const open = new Set<object>();\n const text = (s: string): string => {\n if (jcs && !(s as string & { isWellFormed(): boolean }).isWellFormed()) throw new RangeError(`canonicalJson: the string ${JSON.stringify(s)} has a lone surrogate, which RFC 8785 does not allow.`);\n return JSON.stringify(s);\n };\n // This function gives the tag of a Date, a RegExp, a Map or a Set, and undefined for each other object.\n // It tests the internal slot of each type. Thus a value from another realm gets its tag, and an object with only the name of a type does not.\n const tagOf = (o: object): string | undefined => {\n const brand = brandOf(o);\n if (brand === \"Date\" && hasSlot(Date.prototype.getTime, o)) return `Date(${Date.prototype.getTime.call(o)})`;\n if (brand === \"RegExp\" && hasSlot(sourceOf, o)) return `RegExp(${text(RegExp.prototype.toString.call(o))})`;\n const map = brand === \"Map\" && hasSlot(Map.prototype.has, o);\n if (!map && !(brand === \"Set\" && hasSlot(Set.prototype.has, o))) return undefined;\n if (open.has(o)) throw new TypeError(\"canonicalJson: the value has a cycle.\");\n open.add(o);\n const parts: string[] = [];\n if (map) for (const [k, item] of Map.prototype.entries.call(o) as Iterable<[unknown, unknown]>) parts.push(`[${write(k, \"\")},${write(item, \"\")}]`);\n else for (const item of Set.prototype.values.call(o) as Iterable<unknown>) parts.push(write(item, \"\"));\n open.delete(o);\n return `${map ? \"Map\" : \"Set\"}(${parts.sort().join(\",\")})`;\n };\n // This function gives the text of the TypeError for an object that keeps its content out of its own keys.\n const lost = (brand: string): string => {\n const fix = !typed && tagged.has(brand) ? 'Use the option tags: \"typed\", or give the object a method toJSON.' : \"Give the object a method toJSON.\";\n return `canonicalJson: the type ${brand} has no JSON form. JSON.stringify keeps only its own keys, so two of them with other contents can get the same hash. ${fix}`;\n };\n const write = (v: unknown, key: string): string => {\n if (typed && isObject(v)) {\n const tag = tagOf(v);\n if (tag !== undefined) return tag;\n }\n if (isObject(v) && typeof (v as { toJSON?: unknown }).toJSON === \"function\") {\n v = (v as { toJSON(key: string): unknown }).toJSON(key);\n // A method toJSON can give a Map or a Set. With tags, it gets its tag.\n if (typed && isObject(v)) {\n const tag = tagOf(v);\n if (tag !== undefined) return tag;\n }\n }\n let brand = \"Object\";\n if (isObject(v)) {\n brand = brandOf(v);\n if (brand !== \"Object\") v = unboxed(v, brand);\n }\n if (v === undefined || v === null) return \"null\";\n switch (typeof v) {\n case \"boolean\":\n return v ? \"true\" : \"false\";\n case \"number\":\n if (Number.isFinite(v)) return JSON.stringify(v);\n if (jcs) throw new RangeError(`canonicalJson: ${String(v)} is not a JSON number.`);\n return \"null\";\n case \"string\":\n return text(v);\n case \"bigint\":\n if (bigint === \"suffix\") return `${v}n`;\n throw new TypeError(`canonicalJson: the bigint ${v}n has no JSON form. Use the option bigint: \"suffix\" for a key.`);\n case \"object\":\n break;\n default:\n throw new TypeError(`canonicalJson: a ${typeof v} has no JSON form.`);\n }\n const o = v as object;\n if (brand !== \"Object\" && brand !== \"Arguments\" && !Array.isArray(o) && typedArrayName.call(o) === undefined) throw new TypeError(lost(brand));\n if (open.has(o)) throw new TypeError(\"canonicalJson: the value has a cycle.\");\n open.add(o);\n let out: string;\n if (Array.isArray(o)) out = `[${o.map((item: unknown, i) => write(item, String(i))).join(\",\")}]`;\n else {\n const r = o as Record<string, unknown>;\n const keys = Object.keys(r).filter((k) => r[k] !== undefined).sort();\n out = `{${keys.map((k) => `${text(k)}:${write(r[k], k)}`).join(\",\")}}`;\n }\n open.delete(o);\n return out;\n };\n return write(value, \"\");\n}\n\n/** This function gives the content hash of a value: `sha256Hex(canonicalJson(value, options))`. */\nexport function canonicalHash(value: unknown, options?: CanonicalOptions): string {\n return sha256Hex(canonicalJson(value, options));\n}\n"],"mappings":";;;;;;;;AAQA,MAAM,IAAuB;CAC3B;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAC5J;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CA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AAC,CAAC,CAAC,CAAC,KAAK,GAAG,EAAE;OACzF;GACH,MAAM,IAAI;GAEV,MAAM,IADO,OAAO,KAAK,CAAC,CAAC,CAAC,QAAQ,MAAM,EAAE,OAAO,KAAA,CAAS,CAAC,CAAC,KACjD,CAAC,CAAC,KAAK,MAAM,GAAG,KAAK,CAAC,EAAE,GAAG,MAAM,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,KAAK,GAAG,EAAE;EACtE;EACA,KAAK,OAAO,CAAC;EACb,OAAO;CACT;CACA,OAAO,MAAM,OAAO,EAAE;AACxB;;AAGA,SAAgB,cAAc,OAAgB,SAAoC;CAChF,OAAO,UAAU,cAAc,OAAO,OAAO,CAAC;AAChD"}
package/build/index.d.ts CHANGED
@@ -2,9 +2,9 @@ import { COVERING_CANDIDATES, COVERING_LIMIT, Coverage, CoveringOptions, coverin
2
2
  import { DistributionSpec, Param, dependsOnLibm, sampler } from "./distributions.js";
3
3
  import { normalCdf, normalQuantile } from "./normal.js";
4
4
  import { Distribution, betaCdf, distributionOf } from "./quantiles.js";
5
- import { Dimension, InputFamily, Point, SaltelliDesign, familyMap, familyPoints, latinHypercube, randomDesign, saltelliDesign, stratifiedDesign } from "./designs.js";
5
+ import { Dimension, InputFamily, Point, SaltelliDesign, familyMap, familyPoints, latinHypercube, oneAtATimePoints, randomDesign, saltelliDesign, stratifiedDesign } from "./designs.js";
6
6
  import { CanonicalOptions, Sha256Options, canonicalHash, canonicalJson, sha256Hex } from "./hash.js";
7
7
  import { SHRINK_CHECKS, SHRINK_TOLERANCE, ShrinkOptions, ShrinkStep, Shrunk, shrinkPoint, shrinkPointAsync, shrinkTarget } from "./shrink.js";
8
8
  import { DIAGRAM_BOX_AREA, GIT_BLOB_BYTES, RECORDED_TABLES, RecordedTable, recordedSpec } from "./recorded.js";
9
9
  import { Random, Source, deriveSeed, fnv1a32, mulberry32, plackettLuceOrder, random, randomOf, seedOf, seedsFrom, stablePick } from "./random.js";
10
- export { COVERING_CANDIDATES, COVERING_LIMIT, type CanonicalOptions, type Coverage, type CoveringOptions, DIAGRAM_BOX_AREA, type Dimension, type Distribution, type DistributionSpec, GIT_BLOB_BYTES, type InputFamily, type Param, type Point, RECORDED_TABLES, type Random, type RecordedTable, SHRINK_CHECKS, SHRINK_TOLERANCE, type SaltelliDesign, type Sha256Options, type ShrinkOptions, type ShrinkStep, type Shrunk, type Source, betaCdf, canonicalHash, canonicalJson, coveringArray, coveringRows, dependsOnLibm, deriveSeed, distributionOf, familyMap, familyPoints, fnv1a32, latinHypercube, mulberry32, normalCdf, normalQuantile, plackettLuceOrder, random, randomDesign, randomOf, recordedSpec, saltelliDesign, sampler, seedOf, seedsFrom, sha256Hex, shrinkPoint, shrinkPointAsync, shrinkTarget, stablePick, stratifiedDesign, tupleCoverage };
10
+ export { COVERING_CANDIDATES, COVERING_LIMIT, type CanonicalOptions, type Coverage, type CoveringOptions, DIAGRAM_BOX_AREA, type Dimension, type Distribution, type DistributionSpec, GIT_BLOB_BYTES, type InputFamily, type Param, type Point, RECORDED_TABLES, type Random, type RecordedTable, SHRINK_CHECKS, SHRINK_TOLERANCE, type SaltelliDesign, type Sha256Options, type ShrinkOptions, type ShrinkStep, type Shrunk, type Source, betaCdf, canonicalHash, canonicalJson, coveringArray, coveringRows, dependsOnLibm, deriveSeed, distributionOf, familyMap, familyPoints, fnv1a32, latinHypercube, mulberry32, normalCdf, normalQuantile, oneAtATimePoints, plackettLuceOrder, random, randomDesign, randomOf, recordedSpec, saltelliDesign, sampler, seedOf, seedsFrom, sha256Hex, shrinkPoint, shrinkPointAsync, shrinkTarget, stablePick, stratifiedDesign, tupleCoverage };
package/build/index.js CHANGED
@@ -2,9 +2,9 @@ import { COVERING_CANDIDATES, COVERING_LIMIT, coveringArray, coveringRows, tuple
2
2
  import { dependsOnLibm, sampler } from "./distributions.js";
3
3
  import { normalCdf, normalQuantile } from "./normal.js";
4
4
  import { betaCdf, distributionOf } from "./quantiles.js";
5
- import { familyMap, familyPoints, latinHypercube, randomDesign, saltelliDesign, stratifiedDesign } from "./designs.js";
5
+ import { familyMap, familyPoints, latinHypercube, oneAtATimePoints, randomDesign, saltelliDesign, stratifiedDesign } from "./designs.js";
6
6
  import { canonicalHash, canonicalJson, sha256Hex } from "./hash.js";
7
7
  import { SHRINK_CHECKS, SHRINK_TOLERANCE, shrinkPoint, shrinkPointAsync, shrinkTarget } from "./shrink.js";
8
8
  import { DIAGRAM_BOX_AREA, GIT_BLOB_BYTES, RECORDED_TABLES, recordedSpec } from "./recorded.js";
9
9
  import { deriveSeed, fnv1a32, mulberry32, plackettLuceOrder, random, randomOf, seedOf, seedsFrom, stablePick } from "./random.js";
10
- export { COVERING_CANDIDATES, COVERING_LIMIT, DIAGRAM_BOX_AREA, GIT_BLOB_BYTES, RECORDED_TABLES, SHRINK_CHECKS, SHRINK_TOLERANCE, betaCdf, canonicalHash, canonicalJson, coveringArray, coveringRows, dependsOnLibm, deriveSeed, distributionOf, familyMap, familyPoints, fnv1a32, latinHypercube, mulberry32, normalCdf, normalQuantile, plackettLuceOrder, random, randomDesign, randomOf, recordedSpec, saltelliDesign, sampler, seedOf, seedsFrom, sha256Hex, shrinkPoint, shrinkPointAsync, shrinkTarget, stablePick, stratifiedDesign, tupleCoverage };
10
+ export { COVERING_CANDIDATES, COVERING_LIMIT, DIAGRAM_BOX_AREA, GIT_BLOB_BYTES, RECORDED_TABLES, SHRINK_CHECKS, SHRINK_TOLERANCE, betaCdf, canonicalHash, canonicalJson, coveringArray, coveringRows, dependsOnLibm, deriveSeed, distributionOf, familyMap, familyPoints, fnv1a32, latinHypercube, mulberry32, normalCdf, normalQuantile, oneAtATimePoints, plackettLuceOrder, random, randomDesign, randomOf, recordedSpec, saltelliDesign, sampler, seedOf, seedsFrom, sha256Hex, shrinkPoint, shrinkPointAsync, shrinkTarget, stablePick, stratifiedDesign, tupleCoverage };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@mark1russell7/rigor-generators",
4
- "version": "0.1.19",
4
+ "version": "0.2.1",
5
5
  "description": "A seeded random number generator (mulberry32) with the draws that tests and benchmarks make: integers, picks, weights, shuffles, samples, normal values and named streams. It also gives covering arrays (pairwise and of strength t), and a content hash: SHA-256 and canonical JSON (RFC 8785).",
6
6
  "keywords": [
7
7
  "random",
@@ -46,7 +46,7 @@
46
46
  },
47
47
  "devDependencies": {
48
48
  "@mark1russell7/cue": "^2.0.0",
49
- "@mark1russell7/rigor-testkit": "^0.1.16",
49
+ "@mark1russell7/rigor-testkit": "^0.2.0",
50
50
  "@types/node": "^26.6.5",
51
51
  "esbuild": "^0.28.2",
52
52
  "fast-check": "^4.10.2",