@inglorious/utils 3.8.0 → 4.0.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 +67 -3
- package/package.json +88 -12
- package/src/algorithms/decision-tree.js +3 -3
- package/src/algorithms/path-finding.js +6 -6
- package/src/data-structures/board.js +97 -15
- package/src/data-structures/index.js +1 -2
- package/src/data-structures/objects.js +352 -34
- package/src/data-structures/tree.js +1 -1
- package/src/functions/functions.js +168 -0
- package/src/functions/index.js +1 -4
- package/src/math/circle.js +5 -7
- package/src/math/index.js +0 -4
- package/src/math/line.js +3 -3
- package/src/math/point.js +10 -9
- package/src/math/quaternions.js +20 -4
- package/src/math/rectangle.js +4 -4
- package/src/math/segment.js +11 -5
- package/src/math/vectors.js +471 -65
- package/src/physics/acceleration.js +2 -3
- package/src/physics/friction.js +2 -2
- package/src/physics/gravity.js +2 -2
- package/src/physics/jump.js +0 -4
- package/src/physics/velocity.js +2 -3
- package/src/v.js +1 -1
- package/types/algorithms/decision-tree.d.ts +27 -0
- package/types/algorithms/index.d.ts +2 -0
- package/types/algorithms/path-finding.d.ts +69 -0
- package/types/data-structures/array.d.ts +56 -0
- package/types/data-structures/board.d.ts +121 -0
- package/types/data-structures/boolean.d.ts +4 -0
- package/types/data-structures/heap.d.ts +69 -0
- package/types/data-structures/index.d.ts +8 -0
- package/types/data-structures/objects.d.ts +237 -0
- package/types/data-structures/set.d.ts +4 -0
- package/types/data-structures/tree.d.ts +21 -0
- package/types/functions/functions.d.ts +57 -0
- package/types/functions/index.d.ts +1 -0
- package/types/index.d.ts +6 -4
- package/types/math/circle.d.ts +22 -0
- package/types/math/hitmask.d.ts +20 -0
- package/types/math/index.d.ts +14 -0
- package/types/math/line.d.ts +11 -0
- package/types/math/linear-interpolation.d.ts +2 -0
- package/types/math/numbers.d.ts +26 -0
- package/types/math/point.d.ts +25 -0
- package/types/math/quaternions.d.ts +10 -0
- package/types/math/rectangle.d.ts +36 -0
- package/types/math/rng.d.ts +14 -0
- package/types/math/segment.d.ts +23 -0
- package/types/math/statistics.d.ts +11 -0
- package/types/math/triangle.d.ts +5 -0
- package/types/math/trigonometry.d.ts +26 -0
- package/types/math/vectors.d.ts +190 -0
- package/types/physics/acceleration.d.ts +24 -0
- package/types/physics/friction.d.ts +11 -0
- package/types/physics/gravity.d.ts +20 -0
- package/types/physics/index.d.ts +5 -0
- package/types/physics/jump.d.ts +10 -0
- package/types/physics/velocity.d.ts +20 -0
- package/types/v.d.ts +7 -3
- package/src/data-structures/object.js +0 -320
- package/src/functions/function.js +0 -9
- package/src/math/quaternion.js +0 -22
- package/src/math/vector.js +0 -426
- package/types/algorithms.d.ts +0 -26
- package/types/data-structures.d.ts +0 -53
- package/types/math.d.ts +0 -90
package/src/physics/velocity.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @typedef {import("../../types/math").Vector3} Vector3
|
|
2
|
+
* @typedef {import("../../types/math/vectors").Vector3} Vector3
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
import { clamp, multiply, zero } from "../math/
|
|
6
|
-
import { sum } from "../math/vectors.js"
|
|
5
|
+
import { clamp, multiply, sum, zero } from "../math/vectors.js"
|
|
7
6
|
|
|
8
7
|
const DEFAULT_DT = 1 // Default time delta for the applyVelocity function.
|
|
9
8
|
const DEFAULT_MAX_SPEED = 0 // Default maximum speed for velocity clamping.
|
package/src/v.js
CHANGED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents the input conditions evaluated by a decision tree.
|
|
3
|
+
*/
|
|
4
|
+
export type Conditions = unknown
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Represents the value returned by a decision tree node's test function.
|
|
8
|
+
*/
|
|
9
|
+
export type Outcome = string | boolean
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Represents a node in a decision tree.
|
|
13
|
+
*/
|
|
14
|
+
export interface DecisionTree<C = Conditions> {
|
|
15
|
+
/** Evaluates conditions and returns the key of the branch to follow. */
|
|
16
|
+
test?: (conditions: C) => Outcome
|
|
17
|
+
/** Resolves the branch associated with a test outcome. */
|
|
18
|
+
[outcome: string]: ((conditions: C) => Outcome | DecisionTree<C>) | undefined
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Traverses a decision tree based on the provided conditions.
|
|
23
|
+
*/
|
|
24
|
+
export function decide<C>(
|
|
25
|
+
tree: DecisionTree<C>,
|
|
26
|
+
conditions: NoInfer<C>,
|
|
27
|
+
): Outcome | DecisionTree<C> | undefined
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { Vector2 } from "../math/vectors"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A map where keys are node identifiers and values are their [x, y] coordinates.
|
|
5
|
+
*/
|
|
6
|
+
export interface NodeMap {
|
|
7
|
+
[id: string]: [x: number, y: number]
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Represents a node in a graph.
|
|
12
|
+
*/
|
|
13
|
+
export interface Node {
|
|
14
|
+
/** A unique identifier for the node. */
|
|
15
|
+
id: string
|
|
16
|
+
/** The [x, y] coordinates of the node. */
|
|
17
|
+
position: [x: number, y: number]
|
|
18
|
+
/** An optional cost associated with traversing to this node. */
|
|
19
|
+
cost?: number
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Represents a directed edge between two nodes in a graph.
|
|
24
|
+
*/
|
|
25
|
+
export interface Arc {
|
|
26
|
+
/** The ID of the starting node. */
|
|
27
|
+
from: string
|
|
28
|
+
/** The ID of the ending node. */
|
|
29
|
+
to: string
|
|
30
|
+
/** An optional cost associated with traversing this arc. */
|
|
31
|
+
cost?: number
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Represents a graph structure consisting of nodes and arcs.
|
|
36
|
+
*/
|
|
37
|
+
export interface Graph {
|
|
38
|
+
/** The graph nodes, as either an array or a map for efficient lookups. */
|
|
39
|
+
nodes: NodeMap | Node[]
|
|
40
|
+
/** The arcs connecting the nodes. */
|
|
41
|
+
arcs: Arc[]
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export type Heuristic = (a: Node, b: Node) => number
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Calculates the cost of a path using Dijkstra's algorithm.
|
|
48
|
+
*/
|
|
49
|
+
export function dijkstra(): number
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Calculates the Euclidean distance between two nodes.
|
|
53
|
+
*/
|
|
54
|
+
export function eucledianDistance(a: Node, b: Node): number
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Calculates the Manhattan distance between two nodes.
|
|
58
|
+
*/
|
|
59
|
+
export function manhattanDistance(a: Node, b: Node): Vector2
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Finds the shortest path between two nodes in a graph.
|
|
63
|
+
*/
|
|
64
|
+
export function findPath(
|
|
65
|
+
graph: Graph,
|
|
66
|
+
start: string,
|
|
67
|
+
end: string,
|
|
68
|
+
heuristic?: Heuristic,
|
|
69
|
+
): string[] | undefined
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
export type Comparator<T> = (left: T, right: T) => number
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Checks whether an array contains a specific item.
|
|
5
|
+
*/
|
|
6
|
+
export function contains<T>(arr: readonly T[], item: T): boolean
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Ensures that a value is an array.
|
|
10
|
+
*/
|
|
11
|
+
export function ensureArray<T>(value: T): T extends readonly unknown[] ? T : T[]
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Checks whether a value is an array.
|
|
15
|
+
*/
|
|
16
|
+
export function isArray(value: unknown): value is readonly unknown[]
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Finds the maximum item in an array using a comparator.
|
|
20
|
+
*/
|
|
21
|
+
export function max<T extends number>(
|
|
22
|
+
arr: readonly T[],
|
|
23
|
+
comparator?: Comparator<T>,
|
|
24
|
+
): T
|
|
25
|
+
|
|
26
|
+
export function max<T>(arr: readonly T[], comparator: Comparator<T>): T
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Finds the minimum item in an array using a comparator.
|
|
30
|
+
*/
|
|
31
|
+
export function min<T extends number>(
|
|
32
|
+
arr: readonly T[],
|
|
33
|
+
comparator?: Comparator<T>,
|
|
34
|
+
): T
|
|
35
|
+
|
|
36
|
+
export function min<T>(arr: readonly T[], comparator: Comparator<T>): T
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Removes the smallest item from an array using a comparator.
|
|
40
|
+
*/
|
|
41
|
+
export function pop<T extends number>(
|
|
42
|
+
arr: readonly T[],
|
|
43
|
+
comparator?: Comparator<T>,
|
|
44
|
+
): T[]
|
|
45
|
+
|
|
46
|
+
export function pop<T>(arr: readonly T[], comparator: Comparator<T>): T[]
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Adds an item to an array immutably.
|
|
50
|
+
*/
|
|
51
|
+
export function push<T, U>(arr: readonly T[], item: U): Array<T | U>
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Removes an item from an array immutably.
|
|
55
|
+
*/
|
|
56
|
+
export function remove<T>(arr: readonly T[], item: T): T[]
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
export type Coordinates = readonly [row: number, column: number]
|
|
2
|
+
|
|
3
|
+
export type CoordinatesResult = [row: number, column: number]
|
|
4
|
+
|
|
5
|
+
export type BoardSize = readonly [rows: number, columns: number]
|
|
6
|
+
|
|
7
|
+
export type VerticalBoardSize = readonly [rows: number, columns?: number]
|
|
8
|
+
|
|
9
|
+
export type Board<T> = T[]
|
|
10
|
+
|
|
11
|
+
export type BoardFiller<T> = (row: number, column: number) => T
|
|
12
|
+
|
|
13
|
+
export type CellToString<T> = (cell: T, row: number, column: number) => string
|
|
14
|
+
|
|
15
|
+
export type NeighborPredicate<T> = (cell: T) => boolean
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The axes that wrap around board edges. "horizontal" wraps the column axis,
|
|
19
|
+
* "vertical" wraps the row axis, and "both" wraps each axis independently.
|
|
20
|
+
*/
|
|
21
|
+
export type Wrap = "horizontal" | "vertical" | "both"
|
|
22
|
+
|
|
23
|
+
export interface NeighborOptions {
|
|
24
|
+
wrap?: Wrap
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface CountNeighborsOptions<T> extends NeighborOptions {
|
|
28
|
+
predicate?: NeighborPredicate<T>
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Counts the neighboring cells that satisfy a predicate.
|
|
33
|
+
*/
|
|
34
|
+
export function countNeighbors<T>(
|
|
35
|
+
cells: readonly T[],
|
|
36
|
+
coords: Coordinates,
|
|
37
|
+
size: BoardSize,
|
|
38
|
+
options?: CountNeighborsOptions<T>,
|
|
39
|
+
): number
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Returns the flat-board index for a row and column.
|
|
43
|
+
*/
|
|
44
|
+
export function getIndex(row: number, column: number, columns: number): number
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Creates a one-dimensional board and fills each cell using the given function.
|
|
48
|
+
*/
|
|
49
|
+
export function createBoard(dimensions: BoardSize, filler?: undefined): null[]
|
|
50
|
+
|
|
51
|
+
export function createBoard<T>(
|
|
52
|
+
dimensions: BoardSize,
|
|
53
|
+
filler: BoardFiller<T>,
|
|
54
|
+
): T[]
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Moves coordinates one step down.
|
|
58
|
+
*/
|
|
59
|
+
export function down(
|
|
60
|
+
coords: Coordinates,
|
|
61
|
+
size: VerticalBoardSize,
|
|
62
|
+
): CoordinatesResult
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Moves coordinates one step down and left.
|
|
66
|
+
*/
|
|
67
|
+
export function downLeft(
|
|
68
|
+
coords: Coordinates,
|
|
69
|
+
size: BoardSize,
|
|
70
|
+
): CoordinatesResult
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Moves coordinates one step down and right.
|
|
74
|
+
*/
|
|
75
|
+
export function downRight(
|
|
76
|
+
coords: Coordinates,
|
|
77
|
+
size: BoardSize,
|
|
78
|
+
): CoordinatesResult
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Moves coordinates one step left.
|
|
82
|
+
*/
|
|
83
|
+
export function left(coords: Coordinates, size?: BoardSize): CoordinatesResult
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Returns the coordinates neighboring a cell that fall within the board.
|
|
87
|
+
*/
|
|
88
|
+
export function neighbors(
|
|
89
|
+
coords: Coordinates,
|
|
90
|
+
size: BoardSize,
|
|
91
|
+
options?: NeighborOptions,
|
|
92
|
+
): CoordinatesResult[]
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Moves coordinates one step right.
|
|
96
|
+
*/
|
|
97
|
+
export function right(coords: Coordinates, size: BoardSize): CoordinatesResult
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Converts a board to a string representation.
|
|
101
|
+
*/
|
|
102
|
+
export function toString<T>(
|
|
103
|
+
board: Readonly<Board<T>>,
|
|
104
|
+
dimensions: BoardSize,
|
|
105
|
+
cellToString?: CellToString<T>,
|
|
106
|
+
): string
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Moves coordinates one step up.
|
|
110
|
+
*/
|
|
111
|
+
export function up(coords: Coordinates, size?: BoardSize): CoordinatesResult
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Moves coordinates one step up and left.
|
|
115
|
+
*/
|
|
116
|
+
export function upLeft(coords: Coordinates, size: BoardSize): CoordinatesResult
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Moves coordinates one step up and right.
|
|
120
|
+
*/
|
|
121
|
+
export function upRight(coords: Coordinates, size: BoardSize): CoordinatesResult
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
export type HeapComparator<T> = (a: T, b: T) => number
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Checks whether a heap contains a specific item.
|
|
5
|
+
*/
|
|
6
|
+
export function contains<T>(heap: readonly T[], item: T): boolean
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Converts an array into a heap.
|
|
10
|
+
*/
|
|
11
|
+
export function heapify<T extends number>(
|
|
12
|
+
arr: readonly T[],
|
|
13
|
+
comparator?: HeapComparator<T>,
|
|
14
|
+
): T[]
|
|
15
|
+
|
|
16
|
+
export function heapify<T>(
|
|
17
|
+
arr: readonly T[],
|
|
18
|
+
comparator: HeapComparator<T>,
|
|
19
|
+
): T[]
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Adds an item to a heap.
|
|
23
|
+
*/
|
|
24
|
+
export function push<T extends number>(
|
|
25
|
+
heap: readonly T[],
|
|
26
|
+
item: T,
|
|
27
|
+
comparator?: HeapComparator<T>,
|
|
28
|
+
): T[]
|
|
29
|
+
|
|
30
|
+
export function push<T>(
|
|
31
|
+
heap: readonly T[],
|
|
32
|
+
item: T,
|
|
33
|
+
comparator: HeapComparator<T>,
|
|
34
|
+
): T[]
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Returns the index of a node's left child.
|
|
38
|
+
*/
|
|
39
|
+
export function left(index: number): number
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Returns the index of a node's parent.
|
|
43
|
+
*/
|
|
44
|
+
export function parent(index: number): number
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Removes the root item from a heap.
|
|
48
|
+
*/
|
|
49
|
+
export function pop<T extends number>(
|
|
50
|
+
heap: readonly T[],
|
|
51
|
+
comparator?: HeapComparator<T>,
|
|
52
|
+
): T[]
|
|
53
|
+
|
|
54
|
+
export function pop<T>(heap: readonly T[], comparator: HeapComparator<T>): T[]
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Removes the root item and reorders the heap.
|
|
58
|
+
*/
|
|
59
|
+
export function remove<T extends number>(heap: readonly T[]): T[]
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Returns the index of a node's right child.
|
|
63
|
+
*/
|
|
64
|
+
export function right(index: number): number
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Returns a heap's root item.
|
|
68
|
+
*/
|
|
69
|
+
export function root<T>(heap: readonly T[]): T | undefined
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * as array from "./array"
|
|
2
|
+
export * as board from "./board"
|
|
3
|
+
export * as boolean from "./boolean"
|
|
4
|
+
export * as heap from "./heap"
|
|
5
|
+
export * as objects from "./objects"
|
|
6
|
+
export * as set from "./set"
|
|
7
|
+
export * as string from "./string"
|
|
8
|
+
export * as tree from "./tree"
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import type { Vector } from "../math/vectors"
|
|
2
|
+
|
|
3
|
+
export type ObjectValue<T> = T extends object ? T[keyof T] : never
|
|
4
|
+
|
|
5
|
+
export type PathKey<T, Key extends string> = T extends object
|
|
6
|
+
? Key extends keyof T
|
|
7
|
+
? Key
|
|
8
|
+
: Key extends `${infer Index extends number}`
|
|
9
|
+
? Index extends keyof T
|
|
10
|
+
? Index
|
|
11
|
+
: never
|
|
12
|
+
: never
|
|
13
|
+
: never
|
|
14
|
+
|
|
15
|
+
export type PathValue<T, Path extends string> = T extends object
|
|
16
|
+
? Path extends `${infer Key}.${infer Rest}`
|
|
17
|
+
? PathKey<T, Key> extends never
|
|
18
|
+
? unknown
|
|
19
|
+
: PathValue<T[PathKey<T, Key>], Rest>
|
|
20
|
+
: PathKey<T, Path> extends never
|
|
21
|
+
? unknown
|
|
22
|
+
: T[PathKey<T, Path>]
|
|
23
|
+
: unknown
|
|
24
|
+
|
|
25
|
+
export type DeserializedValue =
|
|
26
|
+
| null
|
|
27
|
+
| boolean
|
|
28
|
+
| number
|
|
29
|
+
| string
|
|
30
|
+
| Vector
|
|
31
|
+
| DeserializedValue[]
|
|
32
|
+
| { [key: string]: DeserializedValue }
|
|
33
|
+
|
|
34
|
+
export type Recipe<T, Args extends unknown[] = []> = (
|
|
35
|
+
draft: T,
|
|
36
|
+
...args: Args
|
|
37
|
+
) => T | void
|
|
38
|
+
|
|
39
|
+
export type ToStringResult<T> = T extends Function
|
|
40
|
+
? T
|
|
41
|
+
: T extends string | readonly unknown[] | object
|
|
42
|
+
? string
|
|
43
|
+
: T
|
|
44
|
+
|
|
45
|
+
export type Merger = (targetValue: unknown, sourceValue: unknown) => unknown
|
|
46
|
+
|
|
47
|
+
export type ObjectSource = object | null | undefined
|
|
48
|
+
|
|
49
|
+
export type MergeObject<T extends object, S extends object> = Omit<T, keyof S> &
|
|
50
|
+
S
|
|
51
|
+
|
|
52
|
+
export type MergeSources<
|
|
53
|
+
T extends object,
|
|
54
|
+
Sources extends readonly unknown[],
|
|
55
|
+
> = Sources extends readonly [
|
|
56
|
+
infer Source,
|
|
57
|
+
...infer Rest extends readonly unknown[],
|
|
58
|
+
]
|
|
59
|
+
? Source extends object
|
|
60
|
+
? MergeSources<MergeObject<T, Source>, Rest>
|
|
61
|
+
: MergeSources<T, Rest>
|
|
62
|
+
: T
|
|
63
|
+
|
|
64
|
+
export type DefaultsResult<T extends object, D extends object> = Omit<
|
|
65
|
+
T,
|
|
66
|
+
keyof D
|
|
67
|
+
> & {
|
|
68
|
+
[K in keyof D]: K extends keyof T
|
|
69
|
+
? Exclude<T[K], null | undefined> | D[K]
|
|
70
|
+
: D[K]
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Creates a deep clone of a value.
|
|
75
|
+
*/
|
|
76
|
+
export function clone<T>(obj: T): T
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Assigns default properties to a target in place.
|
|
80
|
+
*/
|
|
81
|
+
export function defaults<T extends object, D extends object>(
|
|
82
|
+
target: T,
|
|
83
|
+
defaultProps: D,
|
|
84
|
+
): DefaultsResult<T, D>
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Deserializes a value, restoring vector representations.
|
|
88
|
+
*/
|
|
89
|
+
export function deserialize(str: string): DeserializedValue
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Creates a new object by deeply merging source objects into a target.
|
|
93
|
+
*/
|
|
94
|
+
export function extend<
|
|
95
|
+
T extends object,
|
|
96
|
+
Sources extends readonly ObjectSource[],
|
|
97
|
+
>(target: T, ...sources: Sources): MergeSources<T, Sources>
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Creates a new deeply merged object using a custom merger.
|
|
101
|
+
*/
|
|
102
|
+
export function extendWith<
|
|
103
|
+
T extends object,
|
|
104
|
+
Sources extends readonly ObjectSource[],
|
|
105
|
+
>(
|
|
106
|
+
merger: null | undefined,
|
|
107
|
+
target: T,
|
|
108
|
+
...sources: Sources
|
|
109
|
+
): MergeSources<T, Sources>
|
|
110
|
+
|
|
111
|
+
export function extendWith<
|
|
112
|
+
T extends object,
|
|
113
|
+
Sources extends readonly ObjectSource[],
|
|
114
|
+
>(merger: Merger, target: T, ...sources: Sources): Record<string, unknown>
|
|
115
|
+
|
|
116
|
+
export function extendWith<
|
|
117
|
+
T extends object,
|
|
118
|
+
Sources extends readonly ObjectSource[],
|
|
119
|
+
>(
|
|
120
|
+
merger: Merger | null | undefined,
|
|
121
|
+
target: T,
|
|
122
|
+
...sources: Sources
|
|
123
|
+
): Record<string, unknown>
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Filters the properties of an object using a callback.
|
|
127
|
+
*/
|
|
128
|
+
export function filter<T extends object>(
|
|
129
|
+
obj: T,
|
|
130
|
+
callback: (key: string, value: ObjectValue<T>, index: number) => boolean,
|
|
131
|
+
): Partial<T>
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Finds the first property of an object that matches a callback.
|
|
135
|
+
*/
|
|
136
|
+
export function find<T extends object>(
|
|
137
|
+
obj: T,
|
|
138
|
+
callback: (key: string, value: ObjectValue<T>, index: number) => boolean,
|
|
139
|
+
): Partial<T>
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Gets the value at a path in an object.
|
|
143
|
+
*/
|
|
144
|
+
export function get<T, Path extends string | null | undefined, D>(
|
|
145
|
+
obj: T,
|
|
146
|
+
path: Path,
|
|
147
|
+
defaultValue: D,
|
|
148
|
+
): Path extends null | undefined ? D : PathValue<T, Extract<Path, string>> | D
|
|
149
|
+
|
|
150
|
+
export function get<T, Path extends string | null | undefined = string>(
|
|
151
|
+
obj: T,
|
|
152
|
+
path: Path,
|
|
153
|
+
): Path extends null | undefined
|
|
154
|
+
? undefined
|
|
155
|
+
: PathValue<T, Extract<Path, string>> | undefined
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Checks whether a value is a plain object.
|
|
159
|
+
*/
|
|
160
|
+
export function isObject(value: unknown): value is Record<string, unknown>
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Maps the properties of an object using a callback.
|
|
164
|
+
*/
|
|
165
|
+
export function map<T extends object, R>(
|
|
166
|
+
obj: T,
|
|
167
|
+
callback: (key: string, value: ObjectValue<T>, obj: T) => R,
|
|
168
|
+
): Record<string, R>
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Merges source objects into a target in place.
|
|
172
|
+
*/
|
|
173
|
+
export function merge<
|
|
174
|
+
T extends object,
|
|
175
|
+
Sources extends readonly ObjectSource[],
|
|
176
|
+
>(target: T, ...sources: Sources): MergeSources<T, Sources>
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Deeply merges into a target in place using a custom merger.
|
|
180
|
+
*/
|
|
181
|
+
export function mergeWith<
|
|
182
|
+
T extends object,
|
|
183
|
+
Sources extends readonly ObjectSource[],
|
|
184
|
+
>(
|
|
185
|
+
merger: null | undefined,
|
|
186
|
+
target: T,
|
|
187
|
+
...sources: Sources
|
|
188
|
+
): MergeSources<T, Sources>
|
|
189
|
+
|
|
190
|
+
export function mergeWith<
|
|
191
|
+
T extends object,
|
|
192
|
+
Sources extends readonly ObjectSource[],
|
|
193
|
+
>(merger: Merger, target: T, ...sources: Sources): Record<string, unknown>
|
|
194
|
+
|
|
195
|
+
export function mergeWith<
|
|
196
|
+
T extends object,
|
|
197
|
+
Sources extends readonly ObjectSource[],
|
|
198
|
+
>(
|
|
199
|
+
merger: Merger | null | undefined,
|
|
200
|
+
target: T,
|
|
201
|
+
...sources: Sources
|
|
202
|
+
): Record<string, unknown>
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Creates the next immutable state using a recipe.
|
|
206
|
+
*/
|
|
207
|
+
export function produce<T, Args extends unknown[] = []>(
|
|
208
|
+
recipe: Recipe<T, Args>,
|
|
209
|
+
): (state: T, ...args: Args) => T
|
|
210
|
+
|
|
211
|
+
export function produce<T, Args extends unknown[] = []>(
|
|
212
|
+
baseState: T,
|
|
213
|
+
recipe: Recipe<T, Args>,
|
|
214
|
+
...args: Args
|
|
215
|
+
): T
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Serializes a value, converting vectors to plain objects.
|
|
219
|
+
*/
|
|
220
|
+
export function serialize(obj: unknown): string | undefined
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Sets the value at a path in an object and returns the object.
|
|
224
|
+
*/
|
|
225
|
+
export function set<T extends object>(
|
|
226
|
+
obj: T,
|
|
227
|
+
path: string | null | undefined,
|
|
228
|
+
value: unknown,
|
|
229
|
+
): T
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Converts an object or array to a formatted string representation.
|
|
233
|
+
*/
|
|
234
|
+
export function toString<T>(
|
|
235
|
+
obj: T,
|
|
236
|
+
indentationLevel?: number,
|
|
237
|
+
): ToStringResult<T>
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a generic tree data structure.
|
|
3
|
+
* Each node in the tree has a value and an optional array of child nodes.
|
|
4
|
+
* @template T - The type of the value stored in the tree nodes. Defaults to `any`.
|
|
5
|
+
*/
|
|
6
|
+
export interface Tree<T = any> {
|
|
7
|
+
/** The value of the current node. */
|
|
8
|
+
value: T
|
|
9
|
+
/** An optional array of child `Tree` nodes. */
|
|
10
|
+
children?: readonly Tree<T>[]
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Traverses a tree breadth-first.
|
|
15
|
+
*/
|
|
16
|
+
export function bfs<T>(tree: Tree<T>): T[]
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Traverses a tree depth-first.
|
|
20
|
+
*/
|
|
21
|
+
export function dfs<T>(tree: Tree<T>): T[]
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
type AnyFunction = (...args: any[]) => any
|
|
2
|
+
|
|
3
|
+
export type DebouncedFunction<T extends AnyFunction> = ((
|
|
4
|
+
...args: Parameters<T>
|
|
5
|
+
) => ReturnType<T> | undefined) & {
|
|
6
|
+
cancel: () => void
|
|
7
|
+
flush: () => ReturnType<T> | undefined
|
|
8
|
+
pending: () => boolean
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export type ThrottledFunction<T extends AnyFunction> = ((
|
|
12
|
+
...args: Parameters<T>
|
|
13
|
+
) => ReturnType<T> | undefined) & {
|
|
14
|
+
cancel: () => void
|
|
15
|
+
pending: () => boolean
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Composes multiple functions from right to left.
|
|
20
|
+
*/
|
|
21
|
+
export function compose<TArgs extends any[], TResult>(
|
|
22
|
+
...fns: [(...args: TArgs) => any, ...Array<(value: any) => any>]
|
|
23
|
+
): (...args: TArgs) => TResult
|
|
24
|
+
|
|
25
|
+
export function compose(): <T>(value: T) => T
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Creates a debounced function that delays invoking `fn` until after `wait`
|
|
29
|
+
* milliseconds have elapsed since the last call.
|
|
30
|
+
*/
|
|
31
|
+
export function debounce<T extends AnyFunction>(
|
|
32
|
+
fn: T,
|
|
33
|
+
wait?: number,
|
|
34
|
+
): DebouncedFunction<T>
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Checks whether a value is a function.
|
|
38
|
+
*/
|
|
39
|
+
export function isFunction(func: unknown): func is Function
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Pipes multiple functions from left to right.
|
|
43
|
+
*/
|
|
44
|
+
export function pipe<TArgs extends any[], TResult>(
|
|
45
|
+
...fns: [(...args: TArgs) => any, ...Array<(value: any) => any>]
|
|
46
|
+
): (...args: TArgs) => TResult
|
|
47
|
+
|
|
48
|
+
export function pipe(): <T>(value: T) => T
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Creates a throttled function that invokes `fn` immediately, then ignores
|
|
52
|
+
* calls until `wait` milliseconds have elapsed.
|
|
53
|
+
*/
|
|
54
|
+
export function throttle<T extends AnyFunction>(
|
|
55
|
+
fn: T,
|
|
56
|
+
wait?: number,
|
|
57
|
+
): ThrottledFunction<T>
|