@agoric/internal 0.3.3-dev-1a00282.0 → 0.3.3-dev-8093cf6.0
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/package.json +4 -4
- package/src/natural-sort.d.ts +2 -0
- package/src/natural-sort.d.ts.map +1 -0
- package/src/natural-sort.js +48 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agoric/internal",
|
|
3
|
-
"version": "0.3.3-dev-
|
|
3
|
+
"version": "0.3.3-dev-8093cf6.0+8093cf6",
|
|
4
4
|
"description": "Externally unsupported utilities internal to agoric-sdk",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.js",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"lint:types": "tsc"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@agoric/base-zone": "0.1.1-dev-
|
|
23
|
+
"@agoric/base-zone": "0.1.1-dev-8093cf6.0+8093cf6",
|
|
24
24
|
"@endo/common": "^1.2.9",
|
|
25
25
|
"@endo/errors": "^1.2.9",
|
|
26
26
|
"@endo/far": "^1.1.10",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"jessie.js": "^0.3.4"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
|
-
"@agoric/cosmic-proto": "0.4.1-dev-
|
|
37
|
+
"@agoric/cosmic-proto": "0.4.1-dev-8093cf6.0+8093cf6",
|
|
38
38
|
"@endo/exo": "^1.5.8",
|
|
39
39
|
"@endo/init": "^1.1.8",
|
|
40
40
|
"ava": "^5.3.0",
|
|
@@ -60,5 +60,5 @@
|
|
|
60
60
|
"typeCoverage": {
|
|
61
61
|
"atLeast": 93.04
|
|
62
62
|
},
|
|
63
|
-
"gitHead": "
|
|
63
|
+
"gitHead": "8093cf63e76c44a830bd5eabd58e7bad58f5f9f3"
|
|
64
64
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"natural-sort.d.ts","sourceRoot":"","sources":["natural-sort.js"],"names":[],"mappings":"AAqCO,kCAJI,MAAM,KACN,MAAM,GACJ,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAWtB"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param {string} a
|
|
3
|
+
* @param {string} b
|
|
4
|
+
* @returns {-1 | 0 | 1}
|
|
5
|
+
*/
|
|
6
|
+
const compareNats = (a, b) => {
|
|
7
|
+
// Default to IEEE 754 number arithmetic for speed, but fall back on bigint
|
|
8
|
+
// arithmetic to resolve ties because big numbers can lose resolution
|
|
9
|
+
// (sometimes even becoming infinite) and then ultimately on length to resolve
|
|
10
|
+
// ties by ascending count of leading zeros.
|
|
11
|
+
const diff = +a - +b;
|
|
12
|
+
const finiteDiff =
|
|
13
|
+
(Number.isFinite(diff) && diff) ||
|
|
14
|
+
(a === b ? 0 : Number(BigInt(a) - BigInt(b)) || a.length - b.length);
|
|
15
|
+
|
|
16
|
+
// @ts-expect-error this call really does return -1 | 0 | 1
|
|
17
|
+
return Math.sign(finiteDiff);
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
// TODO: compareByCodePoints
|
|
21
|
+
// https://github.com/endojs/endo/pull/2008
|
|
22
|
+
// eslint-disable-next-line no-nested-ternary
|
|
23
|
+
const compareStrings = (a, b) => (a > b ? 1 : a < b ? -1 : 0);
|
|
24
|
+
|
|
25
|
+
const rPrefixedDigits = /^(\D*)(\d+)(\D.*|)/s;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Perform a single-level natural-sort comparison, finding the first decimal
|
|
29
|
+
* digit sequence in each operand and comparing first by the (possibly empty)
|
|
30
|
+
* preceding prefix as strings, then by the digits as integers, then by any
|
|
31
|
+
* following suffix (e.g., sorting 'ko42' before 'ko100' as ['ko', 42] vs.
|
|
32
|
+
* ['ko', 100]).
|
|
33
|
+
*
|
|
34
|
+
* @param {string} a
|
|
35
|
+
* @param {string} b
|
|
36
|
+
* @returns {-1 | 0 | 1}
|
|
37
|
+
*/
|
|
38
|
+
export const naturalCompare = (a, b) => {
|
|
39
|
+
const [_a, aPrefix, aDigits, aSuffix] = rPrefixedDigits.exec(a) || [];
|
|
40
|
+
if (aPrefix !== undefined) {
|
|
41
|
+
const [_b, bPrefix, bDigits, bSuffix] = rPrefixedDigits.exec(b) || [];
|
|
42
|
+
if (bPrefix === aPrefix) {
|
|
43
|
+
return compareNats(aDigits, bDigits) || compareStrings(aSuffix, bSuffix);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return compareStrings(a, b);
|
|
47
|
+
};
|
|
48
|
+
harden(naturalCompare);
|