@fxhash/params 0.0.12 → 0.0.13
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/dist/index.d.ts +214 -213
- package/dist/index.js +78 -83
- package/dist/index.js.map +1 -1
- package/dist/utils-BXUz-G9x.js +387 -0
- package/dist/{chunk-B3N473DY.js.map → utils-BXUz-G9x.js.map} +1 -1
- package/dist/utils-Bd4LPrAy.d.ts +136 -0
- package/dist/utils.d.ts +2 -1
- package/dist/utils.js +3 -3
- package/package.json +7 -10
- package/dist/chunk-B3N473DY.js +0 -412
- package/dist/utils-jEvPb5wq.d.ts +0 -141
- package/dist/utils.js.map +0 -1
@@ -0,0 +1,136 @@
|
|
1
|
+
//#region src/types.d.ts
|
2
|
+
type FxParamType = "number" | "bigint" | "boolean" | "color" | "string" | "bytes" | "select";
|
3
|
+
type FxParamOptionsMap = { [Type in FxParamType]: {
|
4
|
+
number: {
|
5
|
+
min?: number;
|
6
|
+
max?: number;
|
7
|
+
step?: number;
|
8
|
+
};
|
9
|
+
bigint: {
|
10
|
+
min?: number | bigint;
|
11
|
+
max?: number | bigint;
|
12
|
+
};
|
13
|
+
boolean: undefined;
|
14
|
+
color: undefined;
|
15
|
+
string: {
|
16
|
+
minLength?: number;
|
17
|
+
maxLength?: number;
|
18
|
+
};
|
19
|
+
bytes: {
|
20
|
+
length: number;
|
21
|
+
};
|
22
|
+
select: {
|
23
|
+
options: string[];
|
24
|
+
};
|
25
|
+
}[Type] };
|
26
|
+
type FxParamTypeMap = { [Type in FxParamType]: {
|
27
|
+
number: number;
|
28
|
+
bigint: bigint;
|
29
|
+
boolean: boolean;
|
30
|
+
color: string;
|
31
|
+
string: string;
|
32
|
+
bytes: Uint8Array;
|
33
|
+
select: string;
|
34
|
+
}[Type] };
|
35
|
+
type FxParamTransformationTypeMap = { [Type in FxParamType]: {
|
36
|
+
number: number;
|
37
|
+
bigint: bigint;
|
38
|
+
boolean: boolean;
|
39
|
+
color: {
|
40
|
+
hex: {
|
41
|
+
rgb: string;
|
42
|
+
rgba: string;
|
43
|
+
};
|
44
|
+
obj: {
|
45
|
+
rgb: {
|
46
|
+
r: number;
|
47
|
+
g: number;
|
48
|
+
b: number;
|
49
|
+
};
|
50
|
+
rgba: {
|
51
|
+
r: number;
|
52
|
+
g: number;
|
53
|
+
b: number;
|
54
|
+
a: number;
|
55
|
+
};
|
56
|
+
};
|
57
|
+
arr: {
|
58
|
+
rgb: [number, number, number];
|
59
|
+
rgba: [number, number, number, number];
|
60
|
+
};
|
61
|
+
};
|
62
|
+
string: string;
|
63
|
+
bytes: Uint8Array;
|
64
|
+
select: string;
|
65
|
+
}[Type] };
|
66
|
+
type FxParamUpdateMode = "page-reload" | "sync" | "code-driven";
|
67
|
+
type FxParamValue<Type extends FxParamType> = FxParamTypeMap[Type];
|
68
|
+
interface FxParamDefinition<Type extends FxParamType> {
|
69
|
+
id: string;
|
70
|
+
name?: string;
|
71
|
+
type: Type;
|
72
|
+
update?: FxParamUpdateMode;
|
73
|
+
default: FxParamTypeMap[Type];
|
74
|
+
value: FxParamTypeMap[Type];
|
75
|
+
options: FxParamOptionsMap[Type];
|
76
|
+
version?: string;
|
77
|
+
}
|
78
|
+
type FxParamDefinitions = FxParamDefinition<FxParamType>[];
|
79
|
+
type hexString = `#${string}`;
|
80
|
+
type FxParamProcessorTransformer<Type extends FxParamType> = (value: FxParamTypeMap[Type], definition?: FxParamDefinition<Type>) => FxParamTransformationTypeMap[Type];
|
81
|
+
type FxParamProcessorConstrainer<Type extends FxParamType> = (value: FxParamTypeMap[Type], definition: FxParamDefinition<Type>) => FxParamTypeMap[Type];
|
82
|
+
interface FxParamProcessor<Type extends FxParamType> {
|
83
|
+
serialize: (input: FxParamTypeMap[Type], definition: FxParamDefinition<Type>) => string;
|
84
|
+
deserialize: (input: string, definition: FxParamDefinition<Type>) => FxParamTypeMap[Type];
|
85
|
+
bytesLength: (definition: FxParamDefinition<Type>) => number;
|
86
|
+
transform?: FxParamProcessorTransformer<Type>;
|
87
|
+
constrain?: FxParamProcessorConstrainer<Type>;
|
88
|
+
random: (definition: FxParamDefinition<Type>) => FxParamTypeMap[Type];
|
89
|
+
}
|
90
|
+
type FxParamProcessors = { [T in FxParamType]: FxParamProcessor<T> };
|
91
|
+
type FxParamTranformType = "transform" | "constrain";
|
92
|
+
type FxParamsData = Record<string, any>;
|
93
|
+
type FxParamsRaw = Record<string, FxParamValue<FxParamType>>;
|
94
|
+
type FxParamTransformation = FxParamTransformationTypeMap[FxParamType];
|
95
|
+
type FxParamsTransformed = Record<string, FxParamTransformation>;
|
96
|
+
//#endregion
|
97
|
+
//#region src/utils.d.ts
|
98
|
+
declare function rgbaToHex(r: number, g: number, b: number, a: number): string;
|
99
|
+
declare function hexToRgba(hexCode: hexString): {
|
100
|
+
r: number;
|
101
|
+
g: number;
|
102
|
+
b: number;
|
103
|
+
a: number;
|
104
|
+
};
|
105
|
+
declare const MIN_SAFE_INT64: bigint;
|
106
|
+
declare const MAX_SAFE_INT64: bigint;
|
107
|
+
declare const ParameterProcessors: FxParamProcessors;
|
108
|
+
declare function serializeParams(params: any, definition: FxParamDefinition<any>[]): string;
|
109
|
+
declare function serializeParamsOrNull(params: FxParamsData, definition: FxParamDefinition<any>[]): string | null;
|
110
|
+
declare function deserializeParams(bytes: string, definition: FxParamDefinition<FxParamType>[], options: {
|
111
|
+
withTransform?: boolean;
|
112
|
+
transformType?: FxParamTranformType;
|
113
|
+
}): FxParamsRaw | FxParamsTransformed;
|
114
|
+
declare function consolidateParams(params: any, data: any): any[];
|
115
|
+
/**
|
116
|
+
* Given a definition and some params data, builds a clean params object where
|
117
|
+
* the values are first found in the data object, then in the definition if a
|
118
|
+
* default value exists, otherwise in randomizes the value using the param
|
119
|
+
* associated processor.
|
120
|
+
*
|
121
|
+
* @param definition an array of parameter definition
|
122
|
+
* @param data the params data used to reconstruct the final values
|
123
|
+
*/
|
124
|
+
declare function buildParamsObject(definition: FxParamDefinitions, data: FxParamsData | null): FxParamsData;
|
125
|
+
declare function getRandomParamValues(params: FxParamDefinition<FxParamType>[], options?: {
|
126
|
+
noTransform?: boolean;
|
127
|
+
randomizeAll?: boolean;
|
128
|
+
}): any;
|
129
|
+
declare function sumBytesParams(definitions: FxParamDefinition<FxParamType>[]): number;
|
130
|
+
declare function stringifyParamsData(data: FxParamsData): string;
|
131
|
+
declare function jsonStringifyBigint(data: any): string;
|
132
|
+
declare const processParam: (paramId: string, value: FxParamValue<FxParamType>, definitions: FxParamDefinition<FxParamType>[], transformType: FxParamTranformType) => FxParamValue<FxParamType> | FxParamTransformation;
|
133
|
+
declare const processParams: (values: FxParamsData, definitions: FxParamDefinition<FxParamType>[], transformType: FxParamTranformType) => Record<string, FxParamValue<FxParamType>>;
|
134
|
+
//#endregion
|
135
|
+
export { FxParamDefinition, FxParamDefinitions, FxParamOptionsMap, FxParamProcessor, FxParamProcessorConstrainer, FxParamProcessorTransformer, FxParamProcessors, FxParamTranformType, FxParamTransformation, FxParamTransformationTypeMap, FxParamType, FxParamTypeMap, FxParamUpdateMode, FxParamValue, FxParamsData, FxParamsRaw, FxParamsTransformed, MAX_SAFE_INT64 as MAX_SAFE_INT64$1, MIN_SAFE_INT64 as MIN_SAFE_INT64$1, ParameterProcessors as ParameterProcessors$1, buildParamsObject as buildParamsObject$1, consolidateParams as consolidateParams$1, deserializeParams as deserializeParams$1, getRandomParamValues as getRandomParamValues$1, hexString, hexToRgba as hexToRgba$1, jsonStringifyBigint as jsonStringifyBigint$1, processParam as processParam$1, processParams as processParams$1, rgbaToHex as rgbaToHex$1, serializeParams as serializeParams$1, serializeParamsOrNull as serializeParamsOrNull$1, stringifyParamsData as stringifyParamsData$1, sumBytesParams as sumBytesParams$1 };
|
136
|
+
//# sourceMappingURL=utils-Bd4LPrAy.d.ts.map
|
package/dist/utils.d.ts
CHANGED
@@ -1 +1,2 @@
|
|
1
|
-
|
1
|
+
import { MAX_SAFE_INT64$1 as MAX_SAFE_INT64, MIN_SAFE_INT64$1 as MIN_SAFE_INT64, ParameterProcessors$1 as ParameterProcessors, buildParamsObject$1 as buildParamsObject, consolidateParams$1 as consolidateParams, deserializeParams$1 as deserializeParams, getRandomParamValues$1 as getRandomParamValues, hexToRgba$1 as hexToRgba, jsonStringifyBigint$1 as jsonStringifyBigint, processParam$1 as processParam, processParams$1 as processParams, rgbaToHex$1 as rgbaToHex, serializeParams$1 as serializeParams, serializeParamsOrNull$1 as serializeParamsOrNull, stringifyParamsData$1 as stringifyParamsData, sumBytesParams$1 as sumBytesParams } from "./utils-Bd4LPrAy.js";
|
2
|
+
export { MAX_SAFE_INT64, MIN_SAFE_INT64, ParameterProcessors, buildParamsObject, consolidateParams, deserializeParams, getRandomParamValues, hexToRgba, jsonStringifyBigint, processParam, processParams, rgbaToHex, serializeParams, serializeParamsOrNull, stringifyParamsData, sumBytesParams };
|
package/dist/utils.js
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
import { MAX_SAFE_INT64, MIN_SAFE_INT64, ParameterProcessors, buildParamsObject, consolidateParams, deserializeParams, getRandomParamValues, hexToRgba, jsonStringifyBigint, processParam, processParams, rgbaToHex, serializeParams, serializeParamsOrNull, stringifyParamsData, sumBytesParams } from "./utils-BXUz-G9x.js";
|
2
|
+
|
3
|
+
export { MAX_SAFE_INT64, MIN_SAFE_INT64, ParameterProcessors, buildParamsObject, consolidateParams, deserializeParams, getRandomParamValues, hexToRgba, jsonStringifyBigint, processParam, processParams, rgbaToHex, serializeParams, serializeParamsOrNull, stringifyParamsData, sumBytesParams };
|
package/package.json
CHANGED
@@ -1,19 +1,15 @@
|
|
1
1
|
{
|
2
2
|
"name": "@fxhash/params",
|
3
|
-
"version": "0.0.
|
3
|
+
"version": "0.0.13",
|
4
4
|
"author": "fxhash",
|
5
5
|
"dependencies": {
|
6
|
-
"sha1": "1.1.1",
|
7
6
|
"zod": "3.24.3"
|
8
7
|
},
|
9
8
|
"devDependencies": {
|
10
|
-
"
|
11
|
-
"@types/sha1": "1.1.3",
|
12
|
-
"tslib": "2.6.2",
|
13
|
-
"tsup": "8.4.0",
|
9
|
+
"tsdown": "0.12.2",
|
14
10
|
"typescript": "5.8.2",
|
15
|
-
"@fxhash/eslint-config": "1.0.
|
16
|
-
"@fxhash/tsconfig": "0.0.
|
11
|
+
"@fxhash/eslint-config": "1.0.5",
|
12
|
+
"@fxhash/tsconfig": "0.0.3"
|
17
13
|
},
|
18
14
|
"exports": {
|
19
15
|
".": {
|
@@ -35,6 +31,7 @@
|
|
35
31
|
"access": "public"
|
36
32
|
},
|
37
33
|
"repository": "fxhash/fxhash-package",
|
34
|
+
"sideEffects": false,
|
38
35
|
"type": "module",
|
39
36
|
"types": "dist/index.d.ts",
|
40
37
|
"typesVersions": {
|
@@ -45,7 +42,7 @@
|
|
45
42
|
}
|
46
43
|
},
|
47
44
|
"scripts": {
|
48
|
-
"build": "
|
49
|
-
"dev": "
|
45
|
+
"build": "tsdown && tsc --noEmit",
|
46
|
+
"dev": "tsdown --watch"
|
50
47
|
}
|
51
48
|
}
|
package/dist/chunk-B3N473DY.js
DELETED
@@ -1,412 +0,0 @@
|
|
1
|
-
// src/utils.ts
|
2
|
-
function rgbaToHex(r, g, b, a) {
|
3
|
-
const outParts = [
|
4
|
-
r.toString(16),
|
5
|
-
g.toString(16),
|
6
|
-
b.toString(16),
|
7
|
-
Math.round(a * 255).toString(16).substring(0, 2)
|
8
|
-
];
|
9
|
-
outParts.forEach(function(part, i) {
|
10
|
-
if (part.length === 1) {
|
11
|
-
outParts[i] = "0" + part;
|
12
|
-
}
|
13
|
-
});
|
14
|
-
return "#" + outParts.join("");
|
15
|
-
}
|
16
|
-
function completeHexColor(hexCode) {
|
17
|
-
let hex = hexCode.replace("#", "");
|
18
|
-
if (hex.length === 6) {
|
19
|
-
hex = `${hex}ff`;
|
20
|
-
}
|
21
|
-
if (hex.length === 3) {
|
22
|
-
hex = `${hex[0]}${hex[0]}${hex[1]}${hex[1]}${hex[2]}${hex[2]}ff`;
|
23
|
-
}
|
24
|
-
return hex;
|
25
|
-
}
|
26
|
-
function hexToRgba(hexCode) {
|
27
|
-
const hex = completeHexColor(hexCode);
|
28
|
-
const r = parseInt(hex.substring(0, 2), 16);
|
29
|
-
const g = parseInt(hex.substring(2, 4), 16);
|
30
|
-
const b = parseInt(hex.substring(4, 6), 16);
|
31
|
-
const a = Math.round(
|
32
|
-
(parseInt(hex.substring(6, 8), 16) / 255 + Number.EPSILON) * 100
|
33
|
-
) / 100;
|
34
|
-
return { r, g, b, a };
|
35
|
-
}
|
36
|
-
var stringToHex = function(s) {
|
37
|
-
let rtn = "";
|
38
|
-
for (let i = 0; i < s.length; i++) {
|
39
|
-
rtn += s.charCodeAt(i).toString(16).padStart(4, "0");
|
40
|
-
}
|
41
|
-
return rtn;
|
42
|
-
};
|
43
|
-
var hexToString = function(h) {
|
44
|
-
const hx = h.match(/.{1,4}/g) || [];
|
45
|
-
let rtn = "";
|
46
|
-
for (let i = 0; i < hx.length; i++) {
|
47
|
-
const int = parseInt(hx[i], 16);
|
48
|
-
if (int === 0) break;
|
49
|
-
rtn += String.fromCharCode(int);
|
50
|
-
}
|
51
|
-
return rtn;
|
52
|
-
};
|
53
|
-
var MIN_SAFE_INT64 = BigInt("-9223372036854775808");
|
54
|
-
var MAX_SAFE_INT64 = BigInt("9223372036854775807");
|
55
|
-
var ParameterProcessors = {
|
56
|
-
number: {
|
57
|
-
serialize: (input) => {
|
58
|
-
const view = new DataView(new ArrayBuffer(8));
|
59
|
-
view.setFloat64(0, input);
|
60
|
-
return view.getBigUint64(0).toString(16).padStart(16, "0");
|
61
|
-
},
|
62
|
-
deserialize: (input) => {
|
63
|
-
const view = new DataView(new ArrayBuffer(8));
|
64
|
-
for (let i = 0; i < 8; i++) {
|
65
|
-
view.setUint8(i, parseInt(input.substring(i * 2, i * 2 + 2), 16));
|
66
|
-
}
|
67
|
-
return view.getFloat64(0);
|
68
|
-
},
|
69
|
-
bytesLength: () => 8,
|
70
|
-
constrain: (value, definition) => {
|
71
|
-
let min = Number.MIN_SAFE_INTEGER;
|
72
|
-
if (typeof definition.options?.min !== "undefined")
|
73
|
-
min = Number(definition.options.min);
|
74
|
-
let max = Number.MAX_SAFE_INTEGER;
|
75
|
-
if (typeof definition.options?.max !== "undefined")
|
76
|
-
max = Number(definition.options.max);
|
77
|
-
max = Math.min(max, Number.MAX_SAFE_INTEGER);
|
78
|
-
min = Math.max(min, Number.MIN_SAFE_INTEGER);
|
79
|
-
const v = Math.min(Math.max(value, min), max);
|
80
|
-
if (definition?.options?.step) {
|
81
|
-
const t = 1 / definition?.options?.step;
|
82
|
-
return Math.round(v * t) / t;
|
83
|
-
}
|
84
|
-
return v;
|
85
|
-
},
|
86
|
-
random: (definition) => {
|
87
|
-
let min = Number.MIN_SAFE_INTEGER;
|
88
|
-
if (typeof definition.options?.min !== "undefined")
|
89
|
-
min = Number(definition.options.min);
|
90
|
-
let max = Number.MAX_SAFE_INTEGER;
|
91
|
-
if (typeof definition.options?.max !== "undefined")
|
92
|
-
max = Number(definition.options.max);
|
93
|
-
max = Math.min(max, Number.MAX_SAFE_INTEGER);
|
94
|
-
min = Math.max(min, Number.MIN_SAFE_INTEGER);
|
95
|
-
const v = Math.random() * (max - min) + min;
|
96
|
-
if (definition?.options?.step) {
|
97
|
-
const t = 1 / definition?.options?.step;
|
98
|
-
return Math.round(v * t) / t;
|
99
|
-
}
|
100
|
-
return v;
|
101
|
-
}
|
102
|
-
},
|
103
|
-
bigint: {
|
104
|
-
serialize: (input) => {
|
105
|
-
const view = new DataView(new ArrayBuffer(8));
|
106
|
-
view.setBigInt64(0, BigInt(input));
|
107
|
-
return view.getBigUint64(0).toString(16).padStart(16, "0");
|
108
|
-
},
|
109
|
-
deserialize: (input) => {
|
110
|
-
const view = new DataView(new ArrayBuffer(8));
|
111
|
-
for (let i = 0; i < 8; i++) {
|
112
|
-
view.setUint8(i, parseInt(input.substring(i * 2, i * 2 + 2), 16));
|
113
|
-
}
|
114
|
-
return view.getBigInt64(0);
|
115
|
-
},
|
116
|
-
bytesLength: () => 8,
|
117
|
-
random: (definition) => {
|
118
|
-
let min = MIN_SAFE_INT64;
|
119
|
-
let max = MAX_SAFE_INT64;
|
120
|
-
if (typeof definition.options?.min !== "undefined")
|
121
|
-
min = BigInt(definition.options.min);
|
122
|
-
if (typeof definition.options?.max !== "undefined")
|
123
|
-
max = BigInt(definition.options.max);
|
124
|
-
const range = max - min;
|
125
|
-
const bits = range.toString(2).length;
|
126
|
-
let random;
|
127
|
-
do {
|
128
|
-
random = BigInt(
|
129
|
-
"0b" + Array.from(
|
130
|
-
crypto.getRandomValues(new Uint8Array(Math.ceil(bits / 8)))
|
131
|
-
).map((b) => b.toString(2).padStart(8, "0")).join("")
|
132
|
-
);
|
133
|
-
} while (random > range);
|
134
|
-
return random + min;
|
135
|
-
}
|
136
|
-
},
|
137
|
-
boolean: {
|
138
|
-
serialize: (input) => {
|
139
|
-
return typeof input === "boolean" ? input ? "01" : "00" : typeof input === "string" ? input === "true" ? "01" : "00" : "00";
|
140
|
-
},
|
141
|
-
deserialize: (input) => {
|
142
|
-
return input === "00" ? false : true;
|
143
|
-
},
|
144
|
-
bytesLength: () => 1,
|
145
|
-
random: () => Math.random() < 0.5
|
146
|
-
},
|
147
|
-
color: {
|
148
|
-
serialize: (input) => {
|
149
|
-
return completeHexColor(input);
|
150
|
-
},
|
151
|
-
deserialize: (input) => {
|
152
|
-
return input;
|
153
|
-
},
|
154
|
-
bytesLength: () => 4,
|
155
|
-
transform: (input) => {
|
156
|
-
const color = completeHexColor(input);
|
157
|
-
const r = parseInt(color.slice(0, 2), 16);
|
158
|
-
const g = parseInt(color.slice(2, 4), 16);
|
159
|
-
const b = parseInt(color.slice(4, 6), 16);
|
160
|
-
const a = parseInt(color.slice(6, 8), 16);
|
161
|
-
return {
|
162
|
-
hex: {
|
163
|
-
rgb: "#" + input.slice(0, 6),
|
164
|
-
rgba: "#" + input
|
165
|
-
},
|
166
|
-
obj: {
|
167
|
-
rgb: { r, g, b },
|
168
|
-
rgba: { r, g, b, a }
|
169
|
-
},
|
170
|
-
arr: {
|
171
|
-
rgb: [r, g, b],
|
172
|
-
rgba: [r, g, b, a]
|
173
|
-
}
|
174
|
-
};
|
175
|
-
},
|
176
|
-
constrain: (value) => {
|
177
|
-
const hex = value.replace("#", "");
|
178
|
-
return hex.slice(0, 8).padEnd(8, "f");
|
179
|
-
},
|
180
|
-
random: () => `${[...Array(8)].map(() => Math.floor(Math.random() * 16).toString(16)).join("")}`
|
181
|
-
},
|
182
|
-
string: {
|
183
|
-
serialize: (input, def) => {
|
184
|
-
if (!def.version) {
|
185
|
-
let hex2 = stringToHex(input.substring(0, 64));
|
186
|
-
hex2 = hex2.padEnd(64 * 4, "0");
|
187
|
-
return hex2;
|
188
|
-
}
|
189
|
-
let max = 64;
|
190
|
-
if (typeof def.options?.maxLength !== "undefined")
|
191
|
-
max = Number(def.options.maxLength);
|
192
|
-
let hex = stringToHex(input.substring(0, max));
|
193
|
-
hex = hex.padEnd(max * 4, "0");
|
194
|
-
return hex;
|
195
|
-
},
|
196
|
-
deserialize: (input) => {
|
197
|
-
return hexToString(input);
|
198
|
-
},
|
199
|
-
bytesLength: (def) => {
|
200
|
-
if (!def.version) {
|
201
|
-
return 64 * 2;
|
202
|
-
}
|
203
|
-
if (typeof def.options?.maxLength !== "undefined")
|
204
|
-
return Number(def.options.maxLength) * 2;
|
205
|
-
return 64 * 2;
|
206
|
-
},
|
207
|
-
random: (definition) => {
|
208
|
-
let min = 0;
|
209
|
-
if (typeof definition.options?.minLength !== "undefined")
|
210
|
-
min = definition.options.minLength;
|
211
|
-
let max = 64;
|
212
|
-
if (typeof definition.options?.maxLength !== "undefined")
|
213
|
-
max = definition.options.maxLength;
|
214
|
-
const length = Math.round(Math.random() * (max - min) + min);
|
215
|
-
return [...Array(length)].map((i) => (~~(Math.random() * 36)).toString(36)).join("");
|
216
|
-
},
|
217
|
-
constrain: (value, definition) => {
|
218
|
-
let min = 0;
|
219
|
-
if (typeof definition.options?.minLength !== "undefined")
|
220
|
-
min = definition.options.minLength;
|
221
|
-
let max = 64;
|
222
|
-
if (typeof definition.options?.maxLength !== "undefined")
|
223
|
-
max = definition.options.maxLength;
|
224
|
-
const v = value.slice(0, max);
|
225
|
-
if (v.length < min) {
|
226
|
-
return v.padEnd(min);
|
227
|
-
}
|
228
|
-
return v;
|
229
|
-
}
|
230
|
-
},
|
231
|
-
bytes: {
|
232
|
-
serialize: (input, def) => {
|
233
|
-
return Array.from(input).map((i) => i.toString(16).padStart(2, "0")).join("");
|
234
|
-
},
|
235
|
-
deserialize: (input, def) => {
|
236
|
-
const len = input.length / 2;
|
237
|
-
const uint8 = new Uint8Array(len);
|
238
|
-
let idx;
|
239
|
-
for (let i = 0; i < len; i++) {
|
240
|
-
idx = i * 2;
|
241
|
-
uint8[i] = parseInt(`${input[idx]}${input[idx + 1]}`, 16);
|
242
|
-
}
|
243
|
-
return uint8;
|
244
|
-
},
|
245
|
-
bytesLength: (def) => def.options.length,
|
246
|
-
random: (def) => {
|
247
|
-
const len = def.options?.length || 0;
|
248
|
-
const uint8 = new Uint8Array(len);
|
249
|
-
for (let i = 0; i < len; i++) {
|
250
|
-
uint8[i] = Math.random() * 255 | 0;
|
251
|
-
}
|
252
|
-
return uint8;
|
253
|
-
}
|
254
|
-
},
|
255
|
-
select: {
|
256
|
-
serialize: (input, def) => {
|
257
|
-
return Math.min(255, def.options?.options?.indexOf(input) || 0).toString(16).padStart(2, "0");
|
258
|
-
},
|
259
|
-
deserialize: (input, def) => {
|
260
|
-
const idx = parseInt(input, 16);
|
261
|
-
return def.options?.options?.[idx] || def.options?.options?.[0] || "";
|
262
|
-
},
|
263
|
-
bytesLength: () => 1,
|
264
|
-
// index between 0 and 255
|
265
|
-
constrain: (value, definition) => {
|
266
|
-
if (definition.options.options.includes(value)) {
|
267
|
-
return value;
|
268
|
-
}
|
269
|
-
return definition.options.options[0];
|
270
|
-
},
|
271
|
-
random: (definition) => {
|
272
|
-
const index = Math.round(
|
273
|
-
Math.random() * (definition.options.options.length - 1) + 0
|
274
|
-
);
|
275
|
-
return definition?.options?.options[index];
|
276
|
-
}
|
277
|
-
}
|
278
|
-
};
|
279
|
-
function serializeParams(params, definition) {
|
280
|
-
let bytes = "";
|
281
|
-
if (!definition) return bytes;
|
282
|
-
for (const def of definition) {
|
283
|
-
const { id, type } = def;
|
284
|
-
const processor = ParameterProcessors[type];
|
285
|
-
const v = params[id];
|
286
|
-
const val = typeof v !== "undefined" ? v : typeof def.default !== "undefined" ? def.default : processor.random(def);
|
287
|
-
const serialized = processor.serialize(val, def);
|
288
|
-
bytes += serialized;
|
289
|
-
}
|
290
|
-
return bytes;
|
291
|
-
}
|
292
|
-
function serializeParamsOrNull(params, definition) {
|
293
|
-
const serialized = serializeParams(params, definition || []);
|
294
|
-
if (serialized.length === 0) return null;
|
295
|
-
return serialized;
|
296
|
-
}
|
297
|
-
function deserializeParams(bytes, definition, options) {
|
298
|
-
const params = {};
|
299
|
-
for (const def of definition) {
|
300
|
-
const processor = ParameterProcessors[def.type];
|
301
|
-
const transformer = options.withTransform && processor[options.transformType || "transform"];
|
302
|
-
if (!bytes) {
|
303
|
-
let v;
|
304
|
-
if (typeof def.default === "undefined") v = processor.random(def);
|
305
|
-
else v = def.default;
|
306
|
-
params[def.id] = transformer ? transformer(v, def) : v;
|
307
|
-
continue;
|
308
|
-
}
|
309
|
-
const bytesLen = processor.bytesLength(def);
|
310
|
-
const valueBytes = bytes.substring(0, bytesLen * 2);
|
311
|
-
bytes = bytes.substring(bytesLen * 2);
|
312
|
-
const val = processor.deserialize(
|
313
|
-
valueBytes,
|
314
|
-
def
|
315
|
-
);
|
316
|
-
params[def.id] = transformer ? transformer(val, def) : val;
|
317
|
-
}
|
318
|
-
return params;
|
319
|
-
}
|
320
|
-
function consolidateParams(params, data) {
|
321
|
-
if (!params) return [];
|
322
|
-
const rtn = [...params];
|
323
|
-
for (const p in rtn) {
|
324
|
-
const definition = rtn[p];
|
325
|
-
const { id, type, default: def } = definition;
|
326
|
-
if (data && data.hasOwnProperty(id)) {
|
327
|
-
rtn[p].value = data[id];
|
328
|
-
} else {
|
329
|
-
const processor = ParameterProcessors[type];
|
330
|
-
let v;
|
331
|
-
if (typeof def === "undefined") v = processor.random(definition);
|
332
|
-
else v = def;
|
333
|
-
rtn[p].value = processor.transform?.(v) || v;
|
334
|
-
}
|
335
|
-
}
|
336
|
-
return rtn;
|
337
|
-
}
|
338
|
-
function buildParamsObject(definition, data) {
|
339
|
-
if (!definition) return {};
|
340
|
-
const out = {};
|
341
|
-
for (const def of definition) {
|
342
|
-
if (data?.hasOwnProperty(def.id)) {
|
343
|
-
out[def.id] = data[def.id];
|
344
|
-
continue;
|
345
|
-
}
|
346
|
-
if (def.hasOwnProperty("default")) {
|
347
|
-
out[def.id] = def.default;
|
348
|
-
continue;
|
349
|
-
}
|
350
|
-
const processor = ParameterProcessors[def.type];
|
351
|
-
const rand = processor.random(def);
|
352
|
-
out[def.id] = processor.transform?.(rand) || rand;
|
353
|
-
}
|
354
|
-
return out;
|
355
|
-
}
|
356
|
-
function getRandomParamValues(params, options) {
|
357
|
-
return params.reduce(
|
358
|
-
(acc, definition) => {
|
359
|
-
const processor = ParameterProcessors[definition.type];
|
360
|
-
let v = definition.value || definition.default;
|
361
|
-
if (definition.update !== "code-driven" || options?.randomizeAll) {
|
362
|
-
v = processor.random(definition);
|
363
|
-
}
|
364
|
-
if (v) {
|
365
|
-
acc[definition.id] = options?.noTransform ? v : processor.transform?.(v) || v;
|
366
|
-
}
|
367
|
-
return acc;
|
368
|
-
},
|
369
|
-
{}
|
370
|
-
);
|
371
|
-
}
|
372
|
-
function sumBytesParams(definitions) {
|
373
|
-
return definitions?.reduce(
|
374
|
-
(acc, def) => acc + ParameterProcessors[def.type].bytesLength(def),
|
375
|
-
0
|
376
|
-
) || 0;
|
377
|
-
}
|
378
|
-
function stringifyParamsData(data) {
|
379
|
-
return JSON.stringify(data, (key, value) => {
|
380
|
-
if (typeof value === "bigint") return value.toString();
|
381
|
-
return value;
|
382
|
-
});
|
383
|
-
}
|
384
|
-
function jsonStringifyBigint(data) {
|
385
|
-
return JSON.stringify(data, (key, value) => {
|
386
|
-
if (typeof value === "bigint") return value.toString();
|
387
|
-
return value;
|
388
|
-
});
|
389
|
-
}
|
390
|
-
var processParam = (paramId, value, definitions, transformType) => {
|
391
|
-
const definition = definitions.find((d) => d.id === paramId);
|
392
|
-
if (!definition) {
|
393
|
-
throw new Error(`No definition found for param ${paramId}`);
|
394
|
-
}
|
395
|
-
const processor = ParameterProcessors[definition.type];
|
396
|
-
const transformer = processor[transformType];
|
397
|
-
return transformer?.(value, definition) || value;
|
398
|
-
};
|
399
|
-
var processParams = (values, definitions, transformType) => {
|
400
|
-
const paramValues = {};
|
401
|
-
for (const definition of definitions) {
|
402
|
-
const processor = ParameterProcessors[definition.type];
|
403
|
-
const value = values[definition.id];
|
404
|
-
const transformer = processor[transformType];
|
405
|
-
paramValues[definition.id] = transformer?.(value, definition) || value;
|
406
|
-
}
|
407
|
-
return paramValues;
|
408
|
-
};
|
409
|
-
|
410
|
-
export { MAX_SAFE_INT64, MIN_SAFE_INT64, ParameterProcessors, buildParamsObject, consolidateParams, deserializeParams, getRandomParamValues, hexToRgba, jsonStringifyBigint, processParam, processParams, rgbaToHex, serializeParams, serializeParamsOrNull, stringifyParamsData, sumBytesParams };
|
411
|
-
//# sourceMappingURL=chunk-B3N473DY.js.map
|
412
|
-
//# sourceMappingURL=chunk-B3N473DY.js.map
|