@forthtilliath/ts-kit 0.6.0 → 0.7.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/README.md CHANGED
@@ -46,6 +46,9 @@ import { chunk } from "@forthtilliath/ts-kit/array/chunk";
46
46
  - `rankByNameMatch(items, query, getName)` — ranks `items` by relevance to
47
47
  `query`: earlier match position first, then shorter name — for a
48
48
  search-as-you-type list.
49
+ - `swapItems(items, i, j)` — copy of `items` with the elements at `i` and `j`
50
+ swapped (e.g. moving an item up/down) — a plain copy if either position is
51
+ out of bounds.
49
52
 
50
53
  ### `async`
51
54
 
@@ -3,4 +3,5 @@ export * from "./flattenDeep.js";
3
3
  export * from "./getMostRecentIds.js";
4
4
  export * from "./nextInCycle.js";
5
5
  export * from "./rankByNameMatch.js";
6
+ export * from "./swapItems.js";
6
7
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/array/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,kBAAkB,CAAC;AACjC,cAAc,uBAAuB,CAAC;AACtC,cAAc,kBAAkB,CAAC;AACjC,cAAc,sBAAsB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/array/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,kBAAkB,CAAC;AACjC,cAAc,uBAAuB,CAAC;AACtC,cAAc,kBAAkB,CAAC;AACjC,cAAc,sBAAsB,CAAC;AACrC,cAAc,gBAAgB,CAAC"}
@@ -3,3 +3,4 @@ export * from "./flattenDeep.js";
3
3
  export * from "./getMostRecentIds.js";
4
4
  export * from "./nextInCycle.js";
5
5
  export * from "./rankByNameMatch.js";
6
+ export * from "./swapItems.js";
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Returns a copy of `items` with the elements at positions `i` and `j`
3
+ * swapped (e.g. moving an item up or down in a reorderable list). Never
4
+ * mutates `items`, and stays sound under `noUncheckedIndexedAccess`.
5
+ *
6
+ * @param items - The array to copy.
7
+ * @param i - The position of the first element.
8
+ * @param j - The position of the second element.
9
+ * @returns A new array with both elements swapped, or a plain copy if either
10
+ * position is out of bounds.
11
+ * @example
12
+ * swapItems(["a", "b", "c"], 0, 2); // => ["c", "b", "a"]
13
+ * swapItems(["a", "b", "c"], 0, 5); // => ["a", "b", "c"]
14
+ */
15
+ export declare function swapItems<T>(items: readonly T[], i: number, j: number): T[];
16
+ //# sourceMappingURL=swapItems.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"swapItems.d.ts","sourceRoot":"","sources":["../../src/array/swapItems.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,KAAK,EAAE,SAAS,CAAC,EAAE,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,CAAC,EAAE,CAQ3E"}
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Returns a copy of `items` with the elements at positions `i` and `j`
3
+ * swapped (e.g. moving an item up or down in a reorderable list). Never
4
+ * mutates `items`, and stays sound under `noUncheckedIndexedAccess`.
5
+ *
6
+ * @param items - The array to copy.
7
+ * @param i - The position of the first element.
8
+ * @param j - The position of the second element.
9
+ * @returns A new array with both elements swapped, or a plain copy if either
10
+ * position is out of bounds.
11
+ * @example
12
+ * swapItems(["a", "b", "c"], 0, 2); // => ["c", "b", "a"]
13
+ * swapItems(["a", "b", "c"], 0, 5); // => ["a", "b", "c"]
14
+ */
15
+ export function swapItems(items, i, j) {
16
+ const next = [...items];
17
+ const a = next[i];
18
+ const b = next[j];
19
+ if (a === undefined || b === undefined)
20
+ return next;
21
+ next[i] = b;
22
+ next[j] = a;
23
+ return next;
24
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@forthtilliath/ts-kit",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
@@ -26,8 +26,8 @@
26
26
  "jsdom": "^29.1.1",
27
27
  "typescript": "^5.9.3",
28
28
  "vitest": "^4.1.10",
29
- "@forthtilliath/eslint-config": "0.5.0",
30
- "@forthtilliath/typescript-config": "0.0.0"
29
+ "@forthtilliath/typescript-config": "0.1.1",
30
+ "@forthtilliath/eslint-config": "0.7.0"
31
31
  },
32
32
  "scripts": {
33
33
  "dev": "tsc --watch -p tsconfig.build.json",