@softsky/utils 2.7.3 → 2.8.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/README.md CHANGED
@@ -300,6 +300,12 @@ If isTarget is omitted becomes floodfill.
300
300
  Returns a target and map of parents.
301
301
  You can use `unfoldPathfindingResult()` to get array of nodes.
302
302
 
303
+ ---
304
+ __function__ `dijkstra` - Dijkstra search. Like bfs but supports weight.
305
+ If isTarget is omitted becomes floodfill (WITH WEIGHT).
306
+ Returns a target and map of parents.
307
+ You can use `unfoldPathfindingResult()` to get array of nodes.
308
+
303
309
  ---
304
310
  __function__ `knapsack` - Knapsack find best way to get maximum value in limited capacity
305
311
 
@@ -616,3 +622,11 @@ b: "b";
616
622
  ```
617
623
 
618
624
  ---
625
+ __type__ `Tuple` - Create a tuple of size.
626
+
627
+ ```ts
628
+ type a = Tuple<number, 3>
629
+ type b = [number, number, number]
630
+ ```
631
+
632
+ ---
package/dist/arrays.js CHANGED
@@ -48,7 +48,7 @@ export function binarySearch(size, compare, returnClosest) {
48
48
  else
49
49
  low = mid + 1;
50
50
  }
51
- return returnClosest ? mid : -1;
51
+ return returnClosest ? low : -1;
52
52
  }
53
53
  /** Split array into sub arrays of spicified size */
54
54
  export function chunk(array, chunkSize) {
package/dist/graphs.d.ts CHANGED
@@ -59,6 +59,26 @@ export declare function dfs<T>(options: {
59
59
  parents: Map<T, T>;
60
60
  target?: T;
61
61
  };
62
+ /**
63
+ * Dijkstra search. Like bfs but supports weight.
64
+ * If isTarget is omitted becomes floodfill (WITH WEIGHT).
65
+ * Returns a target and map of parents.
66
+ * You can use `unfoldPathfindingResult()` to get array of nodes.
67
+ */
68
+ export declare function dijkstra<T>(options: {
69
+ /** Start node */
70
+ start: T;
71
+ /** Get weight of node */
72
+ getWeight: (node: T) => number;
73
+ /** Get neighbors of node */
74
+ getNeighbors: (node: T) => T[];
75
+ /** Check if node is a target */
76
+ isTarget?: (node: T) => boolean;
77
+ }): {
78
+ parents: Map<T, T>;
79
+ target?: T;
80
+ distance: Map<T, number>;
81
+ };
62
82
  /** Knapsack find best way to get maximum value in limited capacity */
63
83
  export declare function knapsack(weights: number[], values: number[], capacity: number): {
64
84
  weight: number;
package/dist/graphs.js CHANGED
@@ -109,6 +109,39 @@ export function dfs(options) {
109
109
  parents,
110
110
  };
111
111
  }
112
+ /**
113
+ * Dijkstra search. Like bfs but supports weight.
114
+ * If isTarget is omitted becomes floodfill (WITH WEIGHT).
115
+ * Returns a target and map of parents.
116
+ * You can use `unfoldPathfindingResult()` to get array of nodes.
117
+ */
118
+ export function dijkstra(options) {
119
+ const parents = new Map();
120
+ const distance = new Map();
121
+ const visited = new Set();
122
+ const queue = [[0, options.start]];
123
+ distance.set(options.start, 0);
124
+ while (queue.length > 0) {
125
+ const [distribution, node] = queue.shift();
126
+ if (visited.has(node))
127
+ continue;
128
+ visited.add(node);
129
+ if (options.isTarget?.(node))
130
+ return { parents, target: node, distance };
131
+ const neighbors = options.getNeighbors(node);
132
+ for (let index = 0; index < neighbors.length; index++) {
133
+ const neighbor = neighbors[index];
134
+ const cost = options.getWeight(neighbor);
135
+ const newDistribution = distribution + cost;
136
+ if (newDistribution < (distance.get(neighbor) ?? Infinity)) {
137
+ distance.set(neighbor, newDistribution);
138
+ parents.set(neighbor, node);
139
+ pushToSorted(queue, [newDistribution, neighbor], ([x]) => x - newDistribution);
140
+ }
141
+ }
142
+ }
143
+ return { parents, distance };
144
+ }
112
145
  /** Knapsack find best way to get maximum value in limited capacity */
113
146
  export function knapsack(weights, values, capacity) {
114
147
  const n = weights.length;
package/dist/time.js CHANGED
@@ -251,7 +251,6 @@ export class SpeedCalculator {
251
251
  : {
252
252
  speed,
253
253
  percent: this.sum / this.size,
254
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-conversion
255
254
  eta: ~~((this.size - this.sum) / speed) * 1000,
256
255
  };
257
256
  }
package/dist/types.d.ts CHANGED
@@ -8,9 +8,9 @@ export type AnyFunction = (...data: any[]) => any;
8
8
  /** Values that convert to false */
9
9
  export type Falsy = false | '' | 0 | null | undefined;
10
10
  /** Make keys in object optional */
11
- export type Optional<T, K extends keyof T> = Omit<T, K & keyof T> & Partial<Pick<T, K & keyof T>>;
11
+ export type Optional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
12
12
  /** Make keys in object required */
13
- export type RequiredKey<T, K extends keyof T> = Omit<T, K & keyof T> & Required<Pick<T, K & keyof T>>;
13
+ export type RequiredKey<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>;
14
14
  /** Get contructor type of an instance */
15
15
  export type Constructor<T> = new (..._arguments: any[]) => T;
16
16
  /** Recursively resolves promises in objects and arrays */
@@ -59,3 +59,12 @@ export type Concat<T, U> = T extends any[] ? U extends any[] ? [...T, ...U] : ne
59
59
  export type Prettify<T extends object> = {
60
60
  [k in keyof T]: T[k];
61
61
  } & {};
62
+ /**
63
+ * Create a tuple of size.
64
+ *
65
+ * ```ts
66
+ * type a = Tuple<number, 3>
67
+ * type b = [number, number, number]
68
+ * ```
69
+ */
70
+ export type Tuple<T, N extends number, R extends readonly T[] = []> = R['length'] extends N ? R : Tuple<T, N, [...R, T]>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@softsky/utils",
3
- "version": "2.7.3",
3
+ "version": "2.8.1",
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.4",
24
- "@types/bun": "^1.2.21"
23
+ "@softsky/configs": "^1.3.5",
24
+ "@types/bun": "^1.3.2"
25
25
  },
26
26
  "files": ["dist/**/*"]
27
27
  }