@softsky/utils 2.3.3 → 2.5.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
@@ -107,7 +107,13 @@ ${\textsf{\color{ForestGreen}const}}$ SEC_MS - Milliseconds in a second
107
107
  ## Control
108
108
  Utils related to code execution flow.
109
109
 
110
- ${\textsf{\color{ForestGreen}const}}$ UUID - Get unique id
110
+ ${\textsf{\color{ForestGreen}const}}$ generateNumberId - Get unique number id
111
+
112
+ ---
113
+ ${\textsf{\color{CornflowerBlue}function}}$ UUID - Get universally unique string id
114
+
115
+ ---
116
+ ${\textsf{\color{CornflowerBlue}function}}$ extractUUIDDate - Extract exact date of uuid generation
111
117
 
112
118
  ---
113
119
  ${\textsf{\color{CornflowerBlue}function}}$ createCashedFunction - Creates cached function. All arguments/results are cached.
@@ -215,6 +221,31 @@ pipe(
215
221
  ---
216
222
 
217
223
 
224
+ ## Graphs
225
+ Pos
226
+
227
+ ${\textsf{\color{CornflowerBlue}function}}$ unfoldPathfindingResult - Unfold pathfinding result to path array.
228
+
229
+ ---
230
+ ${\textsf{\color{CornflowerBlue}function}}$ aStar - Pathfind using aStar.
231
+ Returns a target and map of parents.
232
+ You can use `unfoldPathfindingResult()` to get array of nodes.
233
+
234
+ ---
235
+ ${\textsf{\color{CornflowerBlue}function}}$ bfs - Breadth-first search. Slower than dfs.
236
+ If isTarget is omitted becomes floodfill.
237
+ Returns a target and map of parents.
238
+ You can use `unfoldPathfindingResult()` to get array of nodes.
239
+
240
+ ---
241
+ ${\textsf{\color{CornflowerBlue}function}}$ dfs - Depth-first search. Faster than bfs.
242
+ If isTarget is omitted becomes floodfill.
243
+ Returns a target and map of parents.
244
+ You can use `unfoldPathfindingResult()` to get array of nodes.
245
+
246
+ ---
247
+
248
+
218
249
  ## Numbers
219
250
  Numbers, math, etc.
220
251
 
package/dist/arrays.js CHANGED
@@ -33,24 +33,22 @@ export function swap(array, index, index2) {
33
33
  * If compare returns < 0 it means we have to cut out smaller side of array.
34
34
  */
35
35
  export function binarySearch(size, compare, returnClosest) {
36
+ if (size === 0)
37
+ return returnClosest ? 0 : -1;
36
38
  let low = 0;
37
39
  let high = size;
38
- let position = -1;
39
- while (high >= low) {
40
- const mid = ((low + high) / 2) | 0;
41
- if (returnClosest)
42
- position = mid;
40
+ let mid = -1;
41
+ while (high > low) {
42
+ mid = ((low + high) / 2) | 0;
43
43
  const compared = compare(mid);
44
- if (compared === 0) {
45
- position = mid;
46
- break;
47
- }
44
+ if (compared === 0)
45
+ return mid;
48
46
  else if (compared > 0)
49
- high = mid - 1;
47
+ high = mid;
50
48
  else
51
49
  low = mid + 1;
52
50
  }
53
- return position;
51
+ return returnClosest ? mid : -1;
54
52
  }
55
53
  /** Split array into sub arrays of spicified size */
56
54
  export function chunk(array, chunkSize) {
package/dist/control.d.ts CHANGED
@@ -2,8 +2,12 @@
2
2
  * Utils related to code execution flow.
3
3
  */
4
4
  import { AwaitedObject, JSONSerializable } from './types';
5
- /** Get unique id */
6
- export declare const UUID: () => number;
5
+ /** Get unique number id */
6
+ export declare const generateNumberId: () => number;
7
+ /** Get universally unique string id */
8
+ export declare function UUID(): string;
9
+ /** Extract exact date of uuid generation */
10
+ export declare function extractUUIDDate(uuid: string): Date;
7
11
  /**
8
12
  * Creates cached function. All arguments/results are cached.
9
13
  * Returns [
package/dist/control.js CHANGED
@@ -2,8 +2,18 @@
2
2
  * Utils related to code execution flow.
3
3
  */
4
4
  let _uuid = Date.now() * 1000;
5
- /** Get unique id */
6
- export const UUID = () => _uuid++;
5
+ /** Get unique number id */
6
+ export const generateNumberId = () => _uuid++;
7
+ const SESSION_ID = ((Math.random() * 2_147_483_648) | 0).toString(16);
8
+ let stringIdInc = 0;
9
+ /** Get universally unique string id */
10
+ export function UUID() {
11
+ return `${Date.now().toString(36).padStart(11, '0')}${(++stringIdInc).toString(36).padStart(3, '0')}${SESSION_ID}`;
12
+ }
13
+ /** Extract exact date of uuid generation */
14
+ export function extractUUIDDate(uuid) {
15
+ return new Date(Number.parseInt(uuid.slice(0, 11), 36));
16
+ }
7
17
  /**
8
18
  * Creates cached function. All arguments/results are cached.
9
19
  * Returns [
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Unfold pathfinding result to path array.
3
+ */
4
+ export declare function unfoldPathfindingResult<T>({ target, parents, }: {
5
+ parents: Map<T, T>;
6
+ target?: T;
7
+ }): T[];
8
+ /**
9
+ * Pathfind using aStar.
10
+ * Returns a target and map of parents.
11
+ * You can use `unfoldPathfindingResult()` to get array of nodes.
12
+ */
13
+ export declare function aStar<T>(options: {
14
+ /** Start node */
15
+ start: T;
16
+ /** Get weight of node */
17
+ getWeight: (node: T) => number;
18
+ /** Presumed cost of going to the target from node */
19
+ heuristic: (node: T) => number;
20
+ /** Get neighbors of node */
21
+ getNeighbors: (node: T) => T[];
22
+ /** Check if node is a target */
23
+ isTarget: (node: T) => boolean;
24
+ }): {
25
+ parents: Map<T, T>;
26
+ target?: T;
27
+ };
28
+ /**
29
+ * Breadth-first search. Slower than dfs.
30
+ * If isTarget is omitted becomes floodfill.
31
+ * Returns a target and map of parents.
32
+ * You can use `unfoldPathfindingResult()` to get array of nodes.
33
+ */
34
+ export declare function bfs<T>(options: {
35
+ /** Start node */
36
+ start: T;
37
+ /** Get neighbors of node */
38
+ getNeighbors: (parent: T) => T[];
39
+ /** Check if node is a target */
40
+ isTarget?: (child: T, parent: T) => boolean;
41
+ }): {
42
+ parents: Map<T, T>;
43
+ target?: T;
44
+ };
45
+ /**
46
+ * Depth-first search. Faster than bfs.
47
+ * If isTarget is omitted becomes floodfill.
48
+ * Returns a target and map of parents.
49
+ * You can use `unfoldPathfindingResult()` to get array of nodes.
50
+ */
51
+ export declare function dfs<T>(options: {
52
+ /** Start node */
53
+ start: T;
54
+ /** Get neighbors of node */
55
+ getNeighbors: (parent: T) => T[];
56
+ /** Check if node is a target */
57
+ isTarget?: (child: T, parent: T) => boolean;
58
+ }): {
59
+ parents: Map<T, T>;
60
+ target?: T;
61
+ };
package/dist/graphs.js ADDED
@@ -0,0 +1,111 @@
1
+ import { pushToSorted } from './arrays';
2
+ /**
3
+ * Unfold pathfinding result to path array.
4
+ */
5
+ export function unfoldPathfindingResult({ target, parents, }) {
6
+ let node = target;
7
+ const path = [];
8
+ while (node) {
9
+ path.push(node);
10
+ node = parents.get(node);
11
+ }
12
+ return path.reverse();
13
+ }
14
+ /**
15
+ * Pathfind using aStar.
16
+ * Returns a target and map of parents.
17
+ * You can use `unfoldPathfindingResult()` to get array of nodes.
18
+ */
19
+ export function aStar(options) {
20
+ const parents = new Map();
21
+ const queue = [
22
+ {
23
+ d: options.start,
24
+ g: 0,
25
+ h: options.heuristic(options.start),
26
+ },
27
+ ];
28
+ while (queue.length > 0) {
29
+ const node = queue.pop();
30
+ if (options.isTarget(node.d))
31
+ return {
32
+ parents,
33
+ target: node.d,
34
+ };
35
+ const neighbors = options.getNeighbors(node.d);
36
+ for (let index = 0; index < neighbors.length; index++) {
37
+ const neighbor = neighbors[index];
38
+ if (options.start === neighbor || parents.has(neighbor))
39
+ continue;
40
+ parents.set(neighbor, node.d);
41
+ const neighborNode = {
42
+ d: neighbor,
43
+ g: node.g + options.getWeight(neighbor),
44
+ h: options.heuristic(neighbor),
45
+ };
46
+ neighborNode.h += node.g;
47
+ pushToSorted(queue, neighborNode, (x) => neighborNode.h - x.h);
48
+ }
49
+ }
50
+ return {
51
+ parents,
52
+ };
53
+ }
54
+ /**
55
+ * Breadth-first search. Slower than dfs.
56
+ * If isTarget is omitted becomes floodfill.
57
+ * Returns a target and map of parents.
58
+ * You can use `unfoldPathfindingResult()` to get array of nodes.
59
+ */
60
+ export function bfs(options) {
61
+ const queue = [options.start];
62
+ const parents = new Map();
63
+ for (let index = 0; index < queue.length; index++) {
64
+ const parent = queue[index];
65
+ const neighbors = options.getNeighbors(parent);
66
+ for (let index = 0; index < neighbors.length; index++) {
67
+ const neighbor = neighbors[index];
68
+ if (neighbor === options.start || parents.has(neighbor))
69
+ continue;
70
+ parents.set(neighbor, parent);
71
+ if (options.isTarget?.(neighbor, parent))
72
+ return {
73
+ target: neighbor,
74
+ parents,
75
+ };
76
+ queue.push(neighbor);
77
+ }
78
+ }
79
+ return {
80
+ parents,
81
+ };
82
+ }
83
+ /**
84
+ * Depth-first search. Faster than bfs.
85
+ * If isTarget is omitted becomes floodfill.
86
+ * Returns a target and map of parents.
87
+ * You can use `unfoldPathfindingResult()` to get array of nodes.
88
+ */
89
+ export function dfs(options) {
90
+ const queue = [options.start];
91
+ const parents = new Map();
92
+ while (queue.length > 0) {
93
+ const node = queue.pop();
94
+ const neighbors = options.getNeighbors(node);
95
+ for (let index = 0; index < neighbors.length; index++) {
96
+ const neighbor = neighbors[index];
97
+ if (neighbor === options.start || parents.has(neighbor))
98
+ continue;
99
+ parents.set(neighbor, node);
100
+ if (options.isTarget?.(neighbor, node))
101
+ return {
102
+ target: neighbor,
103
+ parents,
104
+ };
105
+ queue.push(neighbor);
106
+ }
107
+ }
108
+ return {
109
+ parents,
110
+ };
111
+ }
package/dist/index.d.ts CHANGED
@@ -3,6 +3,7 @@ export * from './consts';
3
3
  export * from './control';
4
4
  export * from './errors';
5
5
  export * from './formatting';
6
+ export * from './graphs';
6
7
  export * from './numbers';
7
8
  export * from './objects';
8
9
  export * from './signals';
package/dist/index.js CHANGED
@@ -3,6 +3,7 @@ export * from './consts';
3
3
  export * from './control';
4
4
  export * from './errors';
5
5
  export * from './formatting';
6
+ export * from './graphs';
6
7
  export * from './numbers';
7
8
  export * from './objects';
8
9
  export * from './signals';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@softsky/utils",
3
- "version": "2.3.3",
3
+ "version": "2.5.0",
4
4
  "description": "JavaScript/TypeScript utilities",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -20,8 +20,8 @@
20
20
  },
21
21
  "homepage": "https://github.com/SoundOfTheSky/utils#readme",
22
22
  "devDependencies": {
23
- "@softsky/configs": "^1.3.0",
24
- "@types/bun": "^1.2.8"
23
+ "@softsky/configs": "^1.3.3",
24
+ "@types/bun": "^1.2.11"
25
25
  },
26
26
  "files": ["dist/**/*"]
27
27
  }