@aifivem/lib 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.
@@ -1,60 +1,60 @@
1
- /** Small readers for panel content (`data/*.json`): every one returns the fallback when the value is missing or wrong. */
2
-
3
- /** A finite number clamped to `[min, max]`, else the fallback. */
4
- export function num(
5
- value: unknown,
6
- fallback: number,
7
- min = -Infinity,
8
- max = Infinity,
9
- ): number {
10
- return typeof value === "number" && Number.isFinite(value)
11
- ? Math.min(max, Math.max(min, value))
12
- : fallback;
13
- }
14
-
15
- /** `num` rounded to an integer. */
16
- export function int(
17
- value: unknown,
18
- fallback: number,
19
- min = -Infinity,
20
- max = Infinity,
21
- ): number {
22
- return Math.round(num(value, fallback, min, max));
23
- }
24
-
25
- /** A boolean, else the fallback. */
26
- export function bool(value: unknown, fallback: boolean): boolean {
27
- return typeof value === "boolean" ? value : fallback;
28
- }
29
-
30
- /** A trimmed non-empty string cut to `max`, else the fallback. */
31
- export function str(value: unknown, fallback: string, max = 64): string {
32
- return typeof value === "string" && value.trim()
33
- ? value.trim().slice(0, max)
34
- : fallback;
35
- }
36
-
37
- /** The value as a plain object, or an empty one. */
38
- export function record(value: unknown): Record<string, unknown> {
39
- return typeof value === "object" && value !== null && !Array.isArray(value)
40
- ? (value as Record<string, unknown>)
41
- : {};
42
- }
43
-
44
- export interface Vec4 {
45
- x: number;
46
- y: number;
47
- z: number;
48
- heading: number;
49
- }
50
-
51
- /** A position with heading; each missing field comes from the fallback. */
52
- export function vec4(value: unknown, fallback: Vec4): Vec4 {
53
- const v = record(value);
54
- return {
55
- x: num(v.x, fallback.x),
56
- y: num(v.y, fallback.y),
57
- z: num(v.z, fallback.z),
58
- heading: num(v.heading, fallback.heading),
59
- };
60
- }
1
+ /** Small readers for panel content (`data/*.json`): every one returns the fallback when the value is missing or wrong. */
2
+
3
+ /** A finite number clamped to `[min, max]`, else the fallback. */
4
+ export function num(
5
+ value: unknown,
6
+ fallback: number,
7
+ min = -Infinity,
8
+ max = Infinity,
9
+ ): number {
10
+ return typeof value === "number" && Number.isFinite(value)
11
+ ? Math.min(max, Math.max(min, value))
12
+ : fallback;
13
+ }
14
+
15
+ /** `num` rounded to an integer. */
16
+ export function int(
17
+ value: unknown,
18
+ fallback: number,
19
+ min = -Infinity,
20
+ max = Infinity,
21
+ ): number {
22
+ return Math.round(num(value, fallback, min, max));
23
+ }
24
+
25
+ /** A boolean, else the fallback. */
26
+ export function bool(value: unknown, fallback: boolean): boolean {
27
+ return typeof value === "boolean" ? value : fallback;
28
+ }
29
+
30
+ /** A trimmed non-empty string cut to `max`, else the fallback. */
31
+ export function str(value: unknown, fallback: string, max = 64): string {
32
+ return typeof value === "string" && value.trim()
33
+ ? value.trim().slice(0, max)
34
+ : fallback;
35
+ }
36
+
37
+ /** The value as a plain object, or an empty one. */
38
+ export function record(value: unknown): Record<string, unknown> {
39
+ return typeof value === "object" && value !== null && !Array.isArray(value)
40
+ ? (value as Record<string, unknown>)
41
+ : {};
42
+ }
43
+
44
+ export interface Vec4 {
45
+ x: number;
46
+ y: number;
47
+ z: number;
48
+ heading: number;
49
+ }
50
+
51
+ /** A position with heading; each missing field comes from the fallback. */
52
+ export function vec4(value: unknown, fallback: Vec4): Vec4 {
53
+ const v = record(value);
54
+ return {
55
+ x: num(v.x, fallback.x),
56
+ y: num(v.y, fallback.y),
57
+ z: num(v.z, fallback.z),
58
+ heading: num(v.heading, fallback.heading),
59
+ };
60
+ }
@@ -1,70 +1,70 @@
1
- /**
2
- * Strict readers for client input (callback and event arguments): a wrong value is refused with null, never replaced
3
- * by a fallback (that is what the panel readers `num` / `int` do).
4
- */
5
-
6
- /** A finite number, else null (NaN and Infinity refused). */
7
- export function readFinite(value: unknown): number | null {
8
- return typeof value === "number" && Number.isFinite(value) ? value : null;
9
- }
10
-
11
- /** A whole number in `[min, max]`, else null (fractions, NaN, Infinity and out-of-range values refused). */
12
- export function readInt(
13
- value: unknown,
14
- min: number,
15
- max: number,
16
- ): number | null {
17
- return Number.isInteger(value) &&
18
- (value as number) >= min &&
19
- (value as number) <= max
20
- ? (value as number)
21
- : null;
22
- }
23
-
24
- /** A token or network id: a positive 32-bit integer, else null. */
25
- export const readId = (value: unknown): number | null =>
26
- readInt(value, 1, 2_147_483_647);
27
-
28
- /** A non-empty trimmed string cut to `max`, else null. */
29
- export function readText(value: unknown, max: number): string | null {
30
- if (typeof value !== "string") return null;
31
- const text = value.trim();
32
- return text ? text.slice(0, max) : null;
33
- }
34
-
35
- /** An `args` parser for inputs that take one value: `onClientCallback(name, { args: oneArg(readId) }, (source, token) => ...)`. */
36
- export const oneArg =
37
- <T>(read: (value: unknown) => T | null) =>
38
- ([value]: unknown[]): [T] | null => {
39
- const parsed = read(value);
40
- return parsed === null ? null : [parsed];
41
- };
42
-
43
- /** The x, y, z (and w) of `{ x, y, z }` or a Lua vector that arrived as `[x, y, z, w?]`. */
44
- function parts(value: unknown): unknown[] | null {
45
- if (Array.isArray(value)) return value;
46
- if (typeof value !== "object" || value === null) return null;
47
- const v = value as Record<string, unknown>;
48
- return [v.x, v.y, v.z, v.heading ?? v.w];
49
- }
50
-
51
- /** A point from client or export input: `{ x, y, z }` or a Lua vector `[x, y, z]`, every value finite; else null. */
52
- export function readVec3(
53
- value: unknown,
54
- ): { x: number; y: number; z: number } | null {
55
- const p = parts(value);
56
- if (!p) return null;
57
- const [x, y, z] = [readFinite(p[0]), readFinite(p[1]), readFinite(p[2])];
58
- return x !== null && y !== null && z !== null ? { x, y, z } : null;
59
- }
60
-
61
- /** A point with a heading: `{ x, y, z, heading | w }` or a Lua vector4; a missing heading reads 0; else null. */
62
- export function readVec4(
63
- value: unknown,
64
- ): { x: number; y: number; z: number; heading: number } | null {
65
- const point = readVec3(value);
66
- if (!point) return null;
67
- const raw = parts(value)?.[3];
68
- const heading = raw === undefined || raw === null ? 0 : readFinite(raw);
69
- return heading === null ? null : { ...point, heading };
70
- }
1
+ /**
2
+ * Strict readers for client input (callback and event arguments): a wrong value is refused with null, never replaced
3
+ * by a fallback (that is what the panel readers `num` / `int` do).
4
+ */
5
+
6
+ /** A finite number, else null (NaN and Infinity refused). */
7
+ export function readFinite(value: unknown): number | null {
8
+ return typeof value === "number" && Number.isFinite(value) ? value : null;
9
+ }
10
+
11
+ /** A whole number in `[min, max]`, else null (fractions, NaN, Infinity and out-of-range values refused). */
12
+ export function readInt(
13
+ value: unknown,
14
+ min: number,
15
+ max: number,
16
+ ): number | null {
17
+ return Number.isInteger(value) &&
18
+ (value as number) >= min &&
19
+ (value as number) <= max
20
+ ? (value as number)
21
+ : null;
22
+ }
23
+
24
+ /** A token or network id: a positive 32-bit integer, else null. */
25
+ export const readId = (value: unknown): number | null =>
26
+ readInt(value, 1, 2_147_483_647);
27
+
28
+ /** A non-empty trimmed string cut to `max`, else null. */
29
+ export function readText(value: unknown, max: number): string | null {
30
+ if (typeof value !== "string") return null;
31
+ const text = value.trim();
32
+ return text ? text.slice(0, max) : null;
33
+ }
34
+
35
+ /** An `args` parser for inputs that take one value: `onClientCallback(name, { args: oneArg(readId) }, (source, token) => ...)`. */
36
+ export const oneArg =
37
+ <T>(read: (value: unknown) => T | null) =>
38
+ ([value]: unknown[]): [T] | null => {
39
+ const parsed = read(value);
40
+ return parsed === null ? null : [parsed];
41
+ };
42
+
43
+ /** The x, y, z (and w) of `{ x, y, z }` or a Lua vector that arrived as `[x, y, z, w?]`. */
44
+ function parts(value: unknown): unknown[] | null {
45
+ if (Array.isArray(value)) return value;
46
+ if (typeof value !== "object" || value === null) return null;
47
+ const v = value as Record<string, unknown>;
48
+ return [v.x, v.y, v.z, v.heading ?? v.w];
49
+ }
50
+
51
+ /** A point from client or export input: `{ x, y, z }` or a Lua vector `[x, y, z]`, every value finite; else null. */
52
+ export function readVec3(
53
+ value: unknown,
54
+ ): { x: number; y: number; z: number } | null {
55
+ const p = parts(value);
56
+ if (!p) return null;
57
+ const [x, y, z] = [readFinite(p[0]), readFinite(p[1]), readFinite(p[2])];
58
+ return x !== null && y !== null && z !== null ? { x, y, z } : null;
59
+ }
60
+
61
+ /** A point with a heading: `{ x, y, z, heading | w }` or a Lua vector4; a missing heading reads 0; else null. */
62
+ export function readVec4(
63
+ value: unknown,
64
+ ): { x: number; y: number; z: number; heading: number } | null {
65
+ const point = readVec3(value);
66
+ if (!point) return null;
67
+ const raw = parts(value)?.[3];
68
+ const heading = raw === undefined || raw === null ? 0 : readFinite(raw);
69
+ return heading === null ? null : { ...point, heading };
70
+ }
package/src/shared/vec.ts CHANGED
@@ -1,28 +1,28 @@
1
- /** A point in the world. */
2
- export interface Vec3 {
3
- x: number;
4
- y: number;
5
- z: number;
6
- }
7
-
8
- /** Straight distance between two points, in metres. */
9
- export const distance = (a: Vec3, b: Vec3): number =>
10
- Math.hypot(a.x - b.x, a.y - b.y, a.z - b.z);
11
-
1
+ /** A point in the world. */
2
+ export interface Vec3 {
3
+ x: number;
4
+ y: number;
5
+ z: number;
6
+ }
7
+
8
+ /** Straight distance between two points, in metres. */
9
+ export const distance = (a: Vec3, b: Vec3): number =>
10
+ Math.hypot(a.x - b.x, a.y - b.y, a.z - b.z);
11
+
12
12
  /** Distance on the ground plane (x / y only), in metres. */
13
13
  export const flatDistance = (
14
14
  a: Pick<Vec3, "x" | "y">,
15
15
  b: Pick<Vec3, "x" | "y">,
16
16
  ): number => Math.hypot(a.x - b.x, a.y - b.y);
17
17
 
18
- /** The heading (0-360, GTA's: 0 = north, counter-clockwise) that faces from `from` towards `to`. */
19
- export function headingTo(
20
- from: Pick<Vec3, "x" | "y">,
21
- to: Pick<Vec3, "x" | "y">,
22
- ): number {
23
- const deg = (Math.atan2(to.x - from.x, to.y - from.y) * 180) / Math.PI;
24
- return (-deg + 360) % 360;
25
- }
18
+ /** The heading (0-360, GTA's: 0 = north, counter-clockwise) that faces from `from` towards `to`. */
19
+ export function headingTo(
20
+ from: Pick<Vec3, "x" | "y">,
21
+ to: Pick<Vec3, "x" | "y">,
22
+ ): number {
23
+ const deg = (Math.atan2(to.x - from.x, to.y - from.y) * 180) / Math.PI;
24
+ return (-deg + 360) % 360;
25
+ }
26
26
 
27
27
  /** The item nearest to `point` (3D, `pos` reads an item's position), within `max` metres; undefined when none. */
28
28
  export function nearestOf<T>(
@@ -1,138 +1,138 @@
1
- /**
2
- * A vehicle's look and tuning as plain data (colours, mods, extras, livery, wheels, neon, tint, plate), read from and
3
- * written to an entity. Stored JSON carries `v: 1`.
4
- */
5
- export interface VehicleProps {
6
- v: 1;
7
- model: number;
8
- plate: string;
9
- plateIndex: number;
10
- colors: [number, number];
11
- extraColors: [number, number];
12
- customPrimary: [number, number, number] | null;
13
- customSecondary: [number, number, number] | null;
14
- wheelType: number;
15
- windowTint: number;
16
- livery: number;
17
- /** Mod type -> index (-1 = stock); toggle mods (turbo 18, xenon 22, ...) as 0 / 1. */
18
- mods: Record<number, number>;
19
- xenonColor: number;
20
- neon: [boolean, boolean, boolean, boolean];
21
- neonColor: [number, number, number];
22
- tyreSmoke: [number, number, number];
23
- extras: Record<number, boolean>;
24
- dirt: number;
25
- engineHealth: number;
26
- bodyHealth: number;
27
- }
28
-
29
- const isObj = (v: unknown): v is Record<string, unknown> =>
30
- typeof v === "object" && v !== null && !Array.isArray(v);
31
- const nums = (v: unknown, n: number): number[] | null =>
32
- Array.isArray(v) && v.length === n && v.every((x) => typeof x === "number")
33
- ? v
34
- : null;
35
-
36
- /** Stored or received props, checked; null when the shape is not ours. */
37
- export function toVehicleProps(v: unknown): VehicleProps | null {
38
- if (
39
- !isObj(v) ||
40
- v.v !== 1 ||
41
- typeof v.model !== "number" ||
42
- typeof v.plate !== "string"
43
- )
44
- return null;
45
- const colors = nums(v.colors, 2);
46
- const extraColors = nums(v.extraColors, 2);
47
- const neonColor = nums(v.neonColor, 3);
48
- const tyreSmoke = nums(v.tyreSmoke, 3);
49
- if (
50
- !colors ||
51
- !extraColors ||
52
- !neonColor ||
53
- !tyreSmoke ||
54
- !isObj(v.mods) ||
55
- !isObj(v.extras) ||
56
- !Array.isArray(v.neon)
57
- )
58
- return null;
59
- const mods: Record<number, number> = {};
60
- for (const [k, x] of Object.entries(v.mods))
61
- if (typeof x === "number") mods[Number(k)] = x;
62
- const extras: Record<number, boolean> = {};
63
- for (const [k, x] of Object.entries(v.extras))
64
- if (typeof x === "boolean") extras[Number(k)] = x;
65
- const n = (x: unknown, d: number) => (typeof x === "number" ? x : d);
66
- const rgb = (x: unknown) => nums(x, 3) as [number, number, number] | null;
67
- return {
68
- v: 1,
69
- model: v.model,
70
- plate: v.plate.slice(0, 8),
71
- plateIndex: n(v.plateIndex, 0),
72
- colors: [colors[0] ?? 0, colors[1] ?? 0],
73
- extraColors: [extraColors[0] ?? 0, extraColors[1] ?? 0],
74
- customPrimary: rgb(v.customPrimary),
75
- customSecondary: rgb(v.customSecondary),
76
- wheelType: n(v.wheelType, 0),
77
- windowTint: n(v.windowTint, -1),
78
- livery: n(v.livery, -1),
79
- mods,
80
- xenonColor: n(v.xenonColor, 255),
81
- neon: [0, 1, 2, 3].map(
82
- (i) => v.neon instanceof Array && v.neon[i] === true,
83
- ) as VehicleProps["neon"],
84
- neonColor: [neonColor[0] ?? 255, neonColor[1] ?? 0, neonColor[2] ?? 255],
85
- tyreSmoke: [tyreSmoke[0] ?? 255, tyreSmoke[1] ?? 255, tyreSmoke[2] ?? 255],
86
- extras,
87
- dirt: n(v.dirt, 0),
88
- engineHealth: n(v.engineHealth, 1000),
89
- bodyHealth: n(v.bodyHealth, 1000),
90
- };
91
- }
92
-
93
- /** What a freshly spawned vehicle gets on top of the model's defaults (lib `spawnVehicle`'s `setup`). */
94
- export interface VehicleSetup {
95
- /** Every performance mod at its top level, turbo and xenons on. */
96
- maxed?: boolean;
97
- /** GTA colour indexes (primary, secondary), as `VehicleProps.colors`. */
98
- colors?: [number, number];
99
- primary?: [number, number, number];
100
- secondary?: [number, number, number];
101
- livery?: number;
102
- windowTint?: number;
103
- /** 0 (clean) - 15. */
104
- dirt?: number;
105
- engineOn?: boolean;
106
- }
107
-
108
- const rgbOf = (v: unknown): [number, number, number] | undefined =>
109
- Array.isArray(v) &&
110
- v.length === 3 &&
111
- v.every((x) => typeof x === "number" && x >= 0 && x <= 255)
112
- ? [v[0], v[1], v[2]]
113
- : undefined;
114
- const intOf = (v: unknown, min: number, max: number): number | undefined =>
115
- typeof v === "number" && Number.isInteger(v) && v >= min && v <= max
116
- ? v
117
- : undefined;
118
-
119
- const colorsOf = (v: unknown): [number, number] | undefined => {
120
- if (!Array.isArray(v) || v.length !== 2) return undefined;
121
- const [a, b] = [intOf(v[0], 0, 255), intOf(v[1], 0, 255)];
122
- return a === undefined || b === undefined ? undefined : [a, b];
123
- };
124
-
125
- /** A received setup, checked field by field (wrong fields drop); null when it is not an object. */
126
- export function toVehicleSetup(v: unknown): VehicleSetup | null {
127
- if (!isObj(v)) return null;
128
- return {
129
- maxed: v.maxed === true || undefined,
130
- colors: colorsOf(v.colors),
131
- primary: rgbOf(v.primary),
132
- secondary: rgbOf(v.secondary),
133
- livery: intOf(v.livery, 0, 100),
134
- windowTint: intOf(v.windowTint, 0, 6),
135
- dirt: intOf(v.dirt, 0, 15),
136
- engineOn: v.engineOn === true || undefined,
137
- };
138
- }
1
+ /**
2
+ * A vehicle's look and tuning as plain data (colours, mods, extras, livery, wheels, neon, tint, plate), read from and
3
+ * written to an entity. Stored JSON carries `v: 1`.
4
+ */
5
+ export interface VehicleProps {
6
+ v: 1;
7
+ model: number;
8
+ plate: string;
9
+ plateIndex: number;
10
+ colors: [number, number];
11
+ extraColors: [number, number];
12
+ customPrimary: [number, number, number] | null;
13
+ customSecondary: [number, number, number] | null;
14
+ wheelType: number;
15
+ windowTint: number;
16
+ livery: number;
17
+ /** Mod type -> index (-1 = stock); toggle mods (turbo 18, xenon 22, ...) as 0 / 1. */
18
+ mods: Record<number, number>;
19
+ xenonColor: number;
20
+ neon: [boolean, boolean, boolean, boolean];
21
+ neonColor: [number, number, number];
22
+ tyreSmoke: [number, number, number];
23
+ extras: Record<number, boolean>;
24
+ dirt: number;
25
+ engineHealth: number;
26
+ bodyHealth: number;
27
+ }
28
+
29
+ const isObj = (v: unknown): v is Record<string, unknown> =>
30
+ typeof v === "object" && v !== null && !Array.isArray(v);
31
+ const nums = (v: unknown, n: number): number[] | null =>
32
+ Array.isArray(v) && v.length === n && v.every((x) => typeof x === "number")
33
+ ? v
34
+ : null;
35
+
36
+ /** Stored or received props, checked; null when the shape is not ours. */
37
+ export function toVehicleProps(v: unknown): VehicleProps | null {
38
+ if (
39
+ !isObj(v) ||
40
+ v.v !== 1 ||
41
+ typeof v.model !== "number" ||
42
+ typeof v.plate !== "string"
43
+ )
44
+ return null;
45
+ const colors = nums(v.colors, 2);
46
+ const extraColors = nums(v.extraColors, 2);
47
+ const neonColor = nums(v.neonColor, 3);
48
+ const tyreSmoke = nums(v.tyreSmoke, 3);
49
+ if (
50
+ !colors ||
51
+ !extraColors ||
52
+ !neonColor ||
53
+ !tyreSmoke ||
54
+ !isObj(v.mods) ||
55
+ !isObj(v.extras) ||
56
+ !Array.isArray(v.neon)
57
+ )
58
+ return null;
59
+ const mods: Record<number, number> = {};
60
+ for (const [k, x] of Object.entries(v.mods))
61
+ if (typeof x === "number") mods[Number(k)] = x;
62
+ const extras: Record<number, boolean> = {};
63
+ for (const [k, x] of Object.entries(v.extras))
64
+ if (typeof x === "boolean") extras[Number(k)] = x;
65
+ const n = (x: unknown, d: number) => (typeof x === "number" ? x : d);
66
+ const rgb = (x: unknown) => nums(x, 3) as [number, number, number] | null;
67
+ return {
68
+ v: 1,
69
+ model: v.model,
70
+ plate: v.plate.slice(0, 8),
71
+ plateIndex: n(v.plateIndex, 0),
72
+ colors: [colors[0] ?? 0, colors[1] ?? 0],
73
+ extraColors: [extraColors[0] ?? 0, extraColors[1] ?? 0],
74
+ customPrimary: rgb(v.customPrimary),
75
+ customSecondary: rgb(v.customSecondary),
76
+ wheelType: n(v.wheelType, 0),
77
+ windowTint: n(v.windowTint, -1),
78
+ livery: n(v.livery, -1),
79
+ mods,
80
+ xenonColor: n(v.xenonColor, 255),
81
+ neon: [0, 1, 2, 3].map(
82
+ (i) => v.neon instanceof Array && v.neon[i] === true,
83
+ ) as VehicleProps["neon"],
84
+ neonColor: [neonColor[0] ?? 255, neonColor[1] ?? 0, neonColor[2] ?? 255],
85
+ tyreSmoke: [tyreSmoke[0] ?? 255, tyreSmoke[1] ?? 255, tyreSmoke[2] ?? 255],
86
+ extras,
87
+ dirt: n(v.dirt, 0),
88
+ engineHealth: n(v.engineHealth, 1000),
89
+ bodyHealth: n(v.bodyHealth, 1000),
90
+ };
91
+ }
92
+
93
+ /** What a freshly spawned vehicle gets on top of the model's defaults (lib `spawnVehicle`'s `setup`). */
94
+ export interface VehicleSetup {
95
+ /** Every performance mod at its top level, turbo and xenons on. */
96
+ maxed?: boolean;
97
+ /** GTA colour indexes (primary, secondary), as `VehicleProps.colors`. */
98
+ colors?: [number, number];
99
+ primary?: [number, number, number];
100
+ secondary?: [number, number, number];
101
+ livery?: number;
102
+ windowTint?: number;
103
+ /** 0 (clean) - 15. */
104
+ dirt?: number;
105
+ engineOn?: boolean;
106
+ }
107
+
108
+ const rgbOf = (v: unknown): [number, number, number] | undefined =>
109
+ Array.isArray(v) &&
110
+ v.length === 3 &&
111
+ v.every((x) => typeof x === "number" && x >= 0 && x <= 255)
112
+ ? [v[0], v[1], v[2]]
113
+ : undefined;
114
+ const intOf = (v: unknown, min: number, max: number): number | undefined =>
115
+ typeof v === "number" && Number.isInteger(v) && v >= min && v <= max
116
+ ? v
117
+ : undefined;
118
+
119
+ const colorsOf = (v: unknown): [number, number] | undefined => {
120
+ if (!Array.isArray(v) || v.length !== 2) return undefined;
121
+ const [a, b] = [intOf(v[0], 0, 255), intOf(v[1], 0, 255)];
122
+ return a === undefined || b === undefined ? undefined : [a, b];
123
+ };
124
+
125
+ /** A received setup, checked field by field (wrong fields drop); null when it is not an object. */
126
+ export function toVehicleSetup(v: unknown): VehicleSetup | null {
127
+ if (!isObj(v)) return null;
128
+ return {
129
+ maxed: v.maxed === true || undefined,
130
+ colors: colorsOf(v.colors),
131
+ primary: rgbOf(v.primary),
132
+ secondary: rgbOf(v.secondary),
133
+ livery: intOf(v.livery, 0, 100),
134
+ windowTint: intOf(v.windowTint, 0, 6),
135
+ dirt: intOf(v.dirt, 0, 15),
136
+ engineOn: v.engineOn === true || undefined,
137
+ };
138
+ }
@@ -1,27 +1,27 @@
1
- /** Resolves after `ms` milliseconds. */
2
- export function sleep(ms: number): Promise<void> {
3
- return new Promise((resolve) => setTimeout(resolve, ms));
4
- }
5
-
6
- /**
7
- * Resolves with the first value `check` returns that is not undefined, checking once per tick (or every `intervalMs`).
8
- * Rejects after `timeoutMs` (default 10 s); pass `false` to wait forever.
9
- * Return `cond || undefined` from `check` to wait for a truthy condition.
10
- */
11
- export function waitFor<T>(
12
- check: () => T | undefined,
13
- timeoutMs: number | false = 10_000,
14
- intervalMs = 0,
15
- ): Promise<T> {
16
- return new Promise((resolve, reject) => {
17
- const started = Date.now();
18
- const poll = (): void => {
19
- const value = check();
20
- if (value !== undefined) return resolve(value);
21
- if (timeoutMs !== false && Date.now() - started > timeoutMs)
22
- return reject(new Error(`waitFor timed out after ${timeoutMs} ms`));
23
- setTimeout(poll, intervalMs);
24
- };
25
- poll();
26
- });
27
- }
1
+ /** Resolves after `ms` milliseconds. */
2
+ export function sleep(ms: number): Promise<void> {
3
+ return new Promise((resolve) => setTimeout(resolve, ms));
4
+ }
5
+
6
+ /**
7
+ * Resolves with the first value `check` returns that is not undefined, checking once per tick (or every `intervalMs`).
8
+ * Rejects after `timeoutMs` (default 10 s); pass `false` to wait forever.
9
+ * Return `cond || undefined` from `check` to wait for a truthy condition.
10
+ */
11
+ export function waitFor<T>(
12
+ check: () => T | undefined,
13
+ timeoutMs: number | false = 10_000,
14
+ intervalMs = 0,
15
+ ): Promise<T> {
16
+ return new Promise((resolve, reject) => {
17
+ const started = Date.now();
18
+ const poll = (): void => {
19
+ const value = check();
20
+ if (value !== undefined) return resolve(value);
21
+ if (timeoutMs !== false && Date.now() - started > timeoutMs)
22
+ return reject(new Error(`waitFor timed out after ${timeoutMs} ms`));
23
+ setTimeout(poll, intervalMs);
24
+ };
25
+ poll();
26
+ });
27
+ }