@zukhruf/python 0.8.3 → 0.8.4
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 +4 -1
- package/dist/compare-strings.d.ts +2 -0
- package/dist/compare-strings.d.ts.map +1 -0
- package/dist/compare-strings.js +19 -0
- package/dist/compare-strings.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @zukhruf/python
|
|
2
2
|
|
|
3
|
-
The results of CPython 3.12 in JavaScript, to the last bit: the `sum()` of floats, the `repr()` of a float, `json.loads`, the white space of `str.isspace()`, and the word characters of `\w`. Use it when a TypeScript port must give the same numbers and text as a Python program, such as a benchmark's scorer.
|
|
3
|
+
The results of CPython 3.12 in JavaScript, to the last bit: the `sum()` of floats, the `repr()` of a float, the order of two strings, `json.loads`, the white space of `str.isspace()`, and the word characters of `\w`. Use it when a TypeScript port must give the same numbers and text as a Python program, such as a benchmark's scorer.
|
|
4
4
|
|
|
5
5
|
The package is public on npm. Until 2026-11-07 it was private and reached through a workspace path (ADR 0001); it became public with `@zukhruf/evals`, which depends on it (backlog #3888).
|
|
6
6
|
|
|
@@ -12,6 +12,7 @@ import {
|
|
|
12
12
|
NeumaierSum,
|
|
13
13
|
PYTHON_SPACE,
|
|
14
14
|
PYTHON_WORD,
|
|
15
|
+
compareStrings,
|
|
15
16
|
floatRepr,
|
|
16
17
|
loadsJson,
|
|
17
18
|
toJsonParseValue,
|
|
@@ -20,6 +21,7 @@ import {
|
|
|
20
21
|
NeumaierSum.of([0.1, 0.2, 0.3]); // 0.6, as sum([0.1, 0.2, 0.3]) in Python
|
|
21
22
|
NeumaierSum.mean([0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]); // 0.1, as sum(values) / len(values) in Python
|
|
22
23
|
floatRepr(1e16); // '1e+16', as repr(1e16)
|
|
24
|
+
['é', '😀', ''].toSorted(compareStrings); // ['é', '', '😀'], as sorted() in Python
|
|
23
25
|
const value: JsonData = loadsJson('{"b": 1, "10": 2.0, "a": NaN}');
|
|
24
26
|
// Map { 'b' => 1n, '10' => 2, 'a' => NaN }
|
|
25
27
|
toJsonParseValue(value); // { '10': 2, b: 1, a: NaN }, for a validator
|
|
@@ -30,6 +32,7 @@ new RegExp(`[${PYTHON_WORD}]+`, 'u'); // Python's \w
|
|
|
30
32
|
| Export | CPython | What it keeps |
|
|
31
33
|
| ------------------ | ---------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
32
34
|
| `NeumaierSum` | `sum()` of floats (Python/bltinmodule.c) | Neumaier's compensated sum. It starts at 0 and adds the compensation only when that is not 0 and is finite, so `[1e308, 1e308, -1e308]` gives `Infinity` and `[-0.0]` gives `0`. `add` and `value` give a running sum; `NeumaierSum.of(values)` sums an iterable. `NeumaierSum.mean(values)` is `sum(values) / len(values)`, and no value throws a `RangeError` (Python's `ZeroDivisionError`, ADR 0003). |
|
|
35
|
+
| `compareStrings` | the order of two `str` | The order of the Unicode code points, as `sorted()` gives. JavaScript's `<` compares UTF-16 code units, so a character above U+FFFF sorts before U+E000 to U+FFFF there and after them in Python. |
|
|
33
36
|
| `floatRepr` | `repr()` of a float | The shortest digits, fixed notation for exponents from -4 to 15, and always a fraction: `1.0`, `1e+16`, `1e-05`, `-0.0`, `nan`, `inf`. |
|
|
34
37
|
| `loadsJson` | `json.loads` | A dict is a `Map` in the order of its keys, and a repeated key keeps its first place and its last value. An int is a `bigint` and a float a `number`, so `1` and `1.0` stay apart. `NaN`, `Infinity` and `-Infinity` are numbers. Text that is not JSON throws a `SyntaxError` (Python's `JSONDecodeError`). An int of more than 4300 digits throws a `RangeError` (Python's `ValueError`). |
|
|
35
38
|
| `JsonData` | the values of `json.loads` | `null`, `boolean`, `bigint`, `number`, `string`, arrays and `Map`s of them. They are mutable, as in Python, so `Array.isArray` and `instanceof Map` narrow a value. |
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compare-strings.d.ts","sourceRoot":"","sources":["../src/compare-strings.ts"],"names":[],"mappings":"AAOA,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAWlE"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// UTF-16 code units order the strings that have no character above U+FFFF
|
|
2
|
+
// as code points do.
|
|
3
|
+
const SURROGATE = /[\u{10000}-\u{10FFFF}]/u;
|
|
4
|
+
// Compares two strings as Python does: by Unicode code point, so
|
|
5
|
+
// sorted(strings) of Python is strings.toSorted(compareStrings). UTF-8 bytes,
|
|
6
|
+
// which C's strcmp compares, have the same order.
|
|
7
|
+
export function compareStrings(left, right) {
|
|
8
|
+
if (!SURROGATE.test(left) && !SURROGATE.test(right))
|
|
9
|
+
return left < right ? -1 : left > right ? 1 : 0;
|
|
10
|
+
const a = [...left];
|
|
11
|
+
const b = [...right];
|
|
12
|
+
for (let index = 0; index < Math.min(a.length, b.length); index++) {
|
|
13
|
+
const difference = (a[index]?.codePointAt(0) ?? 0) - (b[index]?.codePointAt(0) ?? 0);
|
|
14
|
+
if (difference !== 0)
|
|
15
|
+
return difference;
|
|
16
|
+
}
|
|
17
|
+
return a.length - b.length;
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=compare-strings.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compare-strings.js","sourceRoot":"","sources":["../src/compare-strings.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,qBAAqB;AACrB,MAAM,SAAS,GAAG,yBAAyB,CAAC;AAE5C,iEAAiE;AACjE,8EAA8E;AAC9E,kDAAkD;AAClD,MAAM,UAAU,cAAc,CAAC,IAAY,EAAE,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,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EACL,KAAK,cAAc,EACnB,gBAAgB,GACjB,MAAM,0BAA0B,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,YAAY,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC/C,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EACL,KAAK,cAAc,EACnB,gBAAgB,GACjB,MAAM,0BAA0B,CAAC"}
|
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAEL,gBAAgB,GACjB,MAAM,0BAA0B,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAEL,gBAAgB,GACjB,MAAM,0BAA0B,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zukhruf/python",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.4",
|
|
4
4
|
"description": "CPython 3.12 results in JavaScript: the compensated sum() of floats, repr() of a float, json.loads with the order of keys and ints apart from floats, and the white space of str.isspace().",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|