@thi.ng/compare 2.3.10 → 2.4.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/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2024-07-22T13:15:57Z
3
+ - **Last updated**: 2024-08-18T14:11:34Z
4
4
  - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
5
 
6
6
  All notable changes to this project will be documented in this file.
@@ -9,6 +9,12 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
9
9
  **Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
10
10
  and/or version bumps of transitive dependencies.
11
11
 
12
+ ## [2.4.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/compare@2.4.0) (2024-08-10)
13
+
14
+ #### 🚀 Features
15
+
16
+ - add compareLex(), add tests ([015687c](https://github.com/thi-ng/umbrella/commit/015687c))
17
+
12
18
  ## [2.3.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/compare@2.3.0) (2024-03-29)
13
19
 
14
20
  #### 🚀 Features
package/README.md CHANGED
@@ -7,7 +7,7 @@
7
7
  [![Mastodon Follow](https://img.shields.io/mastodon/follow/109331703950160316?domain=https%3A%2F%2Fmastodon.thi.ng&style=social)](https://mastodon.thi.ng/@toxi)
8
8
 
9
9
  > [!NOTE]
10
- > This is one of 198 standalone projects, maintained as part
10
+ > This is one of 199 standalone projects, maintained as part
11
11
  > of the [@thi.ng/umbrella](https://github.com/thi-ng/umbrella/) monorepo
12
12
  > and anti-framework.
13
13
  >
@@ -46,6 +46,7 @@ interface.
46
46
  - [`compareByKeys4()`](https://docs.thi.ng/umbrella/compare/functions/compareByKeys4.html)
47
47
  - [`compareLengthAsc()`](https://docs.thi.ng/umbrella/compare/functions/compareLengthAsc.html)
48
48
  - [`compareLengthDesc()`](https://docs.thi.ng/umbrella/compare/functions/compareLengthDesc.html)
49
+ - [`compareLex()`](https://docs.thi.ng/umbrella/compare/functions/compareLex.html)
49
50
  - [`compareNumAsc()`](https://docs.thi.ng/umbrella/compare/functions/compareNumAsc.html)
50
51
  - [`compareNumDesc()`](https://docs.thi.ng/umbrella/compare/functions/compareNumDesc.html)
51
52
  - [`reverse()`](https://docs.thi.ng/umbrella/compare/functions/reverse.html)
@@ -93,7 +94,7 @@ For Node.js REPL:
93
94
  const cmp = await import("@thi.ng/compare");
94
95
  ```
95
96
 
96
- Package sizes (brotli'd, pre-treeshake): ESM: 629 bytes
97
+ Package sizes (brotli'd, pre-treeshake): ESM: 804 bytes
97
98
 
98
99
  ## Dependencies
99
100
 
@@ -103,16 +104,17 @@ Note: @thi.ng/api is in _most_ cases a type-only import (not used at runtime)
103
104
 
104
105
  ## Usage examples
105
106
 
106
- Four projects in this repo's
107
+ Five projects in this repo's
107
108
  [/examples](https://github.com/thi-ng/umbrella/tree/develop/examples)
108
109
  directory are using this package:
109
110
 
110
- | Screenshot | Description | Live demo | Source |
111
- |:-------------------------------------------------------------------------------------------------------------------------|:----------------------------------------------------------------------------|:--------------------------------------------------------|:-------------------------------------------------------------------------------------|
112
- | <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/dominant-colors.png" width="240"/> | Color palette generation via dominant color extraction from uploaded images | [Demo](https://demo.thi.ng/umbrella/dominant-colors/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/dominant-colors) |
113
- | <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/geom-csv-piechart.png" width="240"/> | Piechart visualization of CSV data | [Demo](https://demo.thi.ng/umbrella/geom-csv-piechart/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/geom-csv-piechart) |
114
- | | Full umbrella repo doc string search w/ paginated results | [Demo](https://demo.thi.ng/umbrella/rdom-search-docs/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/rdom-search-docs) |
115
- | <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/triple-query.png" width="240"/> | Triple store query results & sortable table | [Demo](https://demo.thi.ng/umbrella/triple-query/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/triple-query) |
111
+ | Screenshot | Description | Live demo | Source |
112
+ |:-------------------------------------------------------------------------------------------------------------------------|:------------------------------------------------------------------------------|:--------------------------------------------------------|:-------------------------------------------------------------------------------------|
113
+ | <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/dominant-colors.png" width="240"/> | Color palette generation via dominant color extraction from uploaded images | [Demo](https://demo.thi.ng/umbrella/dominant-colors/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/dominant-colors) |
114
+ | <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/geom-csv-piechart.png" width="240"/> | Piechart visualization of CSV data | [Demo](https://demo.thi.ng/umbrella/geom-csv-piechart/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/geom-csv-piechart) |
115
+ | | Full umbrella repo doc string search w/ paginated results | [Demo](https://demo.thi.ng/umbrella/rdom-search-docs/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/rdom-search-docs) |
116
+ | <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/thing-browser.avif" width="240"/> | Tree-based UI to find & explore thi.ng projects via their associated keywords | [Demo](https://demo.thi.ng/umbrella/thing-browser/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/thing-browser) |
117
+ | <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/triple-query.png" width="240"/> | Triple store query results & sortable table | [Demo](https://demo.thi.ng/umbrella/triple-query/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/triple-query) |
116
118
 
117
119
  ## API
118
120
 
package/index.d.ts CHANGED
@@ -4,4 +4,5 @@ export * from "./length.js";
4
4
  export * from "./numeric.js";
5
5
  export * from "./ops.js";
6
6
  export * from "./reverse.js";
7
+ export * from "./string.js";
7
8
  //# sourceMappingURL=index.d.ts.map
package/index.js CHANGED
@@ -4,3 +4,4 @@ export * from "./length.js";
4
4
  export * from "./numeric.js";
5
5
  export * from "./ops.js";
6
6
  export * from "./reverse.js";
7
+ export * from "./string.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/compare",
3
- "version": "2.3.10",
3
+ "version": "2.4.1",
4
4
  "description": "Comparators with support for types implementing the @thi.ng/api/ICompare interface",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -36,13 +36,13 @@
36
36
  "tool:tangle": "../../node_modules/.bin/tangle src/**/*.ts"
37
37
  },
38
38
  "dependencies": {
39
- "@thi.ng/api": "^8.11.7"
39
+ "@thi.ng/api": "^8.11.9"
40
40
  },
41
41
  "devDependencies": {
42
- "@microsoft/api-extractor": "^7.47.0",
42
+ "@microsoft/api-extractor": "^7.47.5",
43
43
  "esbuild": "^0.23.0",
44
- "typedoc": "^0.26.3",
45
- "typescript": "^5.5.3"
44
+ "typedoc": "^0.26.5",
45
+ "typescript": "^5.5.4"
46
46
  },
47
47
  "keywords": [
48
48
  "comparator",
@@ -80,10 +80,13 @@
80
80
  },
81
81
  "./reverse": {
82
82
  "default": "./reverse.js"
83
+ },
84
+ "./string": {
85
+ "default": "./string.js"
83
86
  }
84
87
  },
85
88
  "thi.ng": {
86
89
  "alias": "cmp"
87
90
  },
88
- "gitHead": "324d6b7dbf31558329e9fb6452e29b2f7db9c61a\n"
91
+ "gitHead": "f6e26ea1142525171de5d36b9c3119f2782bb437\n"
89
92
  }
package/string.d.ts ADDED
@@ -0,0 +1,26 @@
1
+ import type { Comparator } from "@thi.ng/api";
2
+ /**
3
+ * Comparator intended for short tag-like strings containing compounds of words
4
+ * and numbers (e.g. `2d` or `base36`). Splits strings at first digit/non-digit
5
+ * boundary and if both inputs happen to contain such a boundary, compares
6
+ * inputs pairwise, comparing each lexicographically (ascending order for
7
+ * numeric parts)
8
+ *
9
+ * @example
10
+ * ```ts tangle:../export/compare-lex.ts
11
+ * import { compareLex } from "@thi.ng/compare";
12
+ *
13
+ * const sr = ["2d", "16bit", "base36", "8bit", "1d", "base8"];
14
+ *
15
+ * console.log("native", [...src].sort());
16
+ * // [ "16bit", "1d", "2d", "8bit", "base36", "base8" ]
17
+ *
18
+ * console.log("compareLex", [...src].sort(compareLex));
19
+ * // [ "1d", "2d", "8bit", "16bit", "base8", "base36" ]
20
+ * ```
21
+ *
22
+ * @param a
23
+ * @param b
24
+ */
25
+ export declare const compareLex: Comparator<string>;
26
+ //# sourceMappingURL=string.d.ts.map
package/string.js ADDED
@@ -0,0 +1,29 @@
1
+ const BOUNDARY = /(\D\d|\d\D)/;
2
+ const compareLex = (a, b) => {
3
+ const ma = BOUNDARY.exec(a);
4
+ const mb = BOUNDARY.exec(b);
5
+ return ma && mb ? __compare2(
6
+ __maybeNum(a.substring(0, ma.index + 1)),
7
+ __maybeNum(a.substring(ma.index + 1)),
8
+ __maybeNum(b.substring(0, mb.index + 1)),
9
+ __maybeNum(b.substring(mb.index + 1))
10
+ ) : a < b ? -1 : a > b ? 1 : 0;
11
+ };
12
+ const __compare2 = (a1, a2, b1, b2) => {
13
+ const na = typeof a1 === "number";
14
+ const nb = typeof b1 === "number";
15
+ if (na !== nb) {
16
+ if (na) a1 += " ";
17
+ else a2 += " ";
18
+ if (nb) b1 += " ";
19
+ else b2 += " ";
20
+ }
21
+ return a1 === b1 ? a2 === b2 ? 0 : a2 < b2 ? -2 : 2 : a1 < b1 ? -1 : 1;
22
+ };
23
+ const __maybeNum = (x) => {
24
+ const id = x.charCodeAt(0);
25
+ return id >= 48 && id <= 57 ? +x : x;
26
+ };
27
+ export {
28
+ compareLex
29
+ };