@alextheman/utility 2.10.1 → 2.10.3
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.cjs +31 -2
- package/dist/index.d.cts +14 -2
- package/dist/index.d.ts +14 -2
- package/dist/index.js +31 -2
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -121,15 +121,44 @@ function convertFileToBase64(file) {
|
|
|
121
121
|
var convertFileToBase64_default = convertFileToBase64;
|
|
122
122
|
|
|
123
123
|
// src/functions/createFormData.ts
|
|
124
|
-
function createFormData(data) {
|
|
124
|
+
function createFormData(data, options = { nullableResolution: "empty" }) {
|
|
125
125
|
const formData = new FormData();
|
|
126
|
+
function resolveByStrategy(key, value, resolutionStrategy) {
|
|
127
|
+
switch (resolutionStrategy) {
|
|
128
|
+
case "empty":
|
|
129
|
+
formData.append(key, "");
|
|
130
|
+
break;
|
|
131
|
+
case "stringify":
|
|
132
|
+
formData.append(key, JSON.stringify(value));
|
|
133
|
+
break;
|
|
134
|
+
case "omit":
|
|
135
|
+
break;
|
|
136
|
+
default:
|
|
137
|
+
throw new Error("SLOPPY_PURE_JAVASCRIPT_USER_ERROR");
|
|
138
|
+
}
|
|
139
|
+
}
|
|
126
140
|
for (const key in data) {
|
|
127
141
|
if (data[key] instanceof Blob) {
|
|
128
142
|
formData.append(key, data[key]);
|
|
143
|
+
} else if (data[key] === void 0 || data[key] === null) {
|
|
144
|
+
if (options.nullableResolution) {
|
|
145
|
+
resolveByStrategy(key, data[key], options.nullableResolution);
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
if (options.undefinedResolution || options.nullResolution) {
|
|
149
|
+
if (data[key] === void 0 && options.undefinedResolution) {
|
|
150
|
+
resolveByStrategy(key, data[key], options.undefinedResolution);
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
if (data[key] === null && options.nullResolution) {
|
|
154
|
+
resolveByStrategy(key, data[key], options.nullResolution);
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
129
158
|
} else if (typeof data[key] === "object") {
|
|
130
159
|
formData.append(key, JSON.stringify(data[key]));
|
|
131
160
|
} else {
|
|
132
|
-
formData.append(key, data[key]);
|
|
161
|
+
formData.append(key, String(data[key]));
|
|
133
162
|
}
|
|
134
163
|
}
|
|
135
164
|
return formData;
|
package/dist/index.d.cts
CHANGED
|
@@ -8,7 +8,19 @@ declare function camelToKebab(string: string): string;
|
|
|
8
8
|
|
|
9
9
|
declare function convertFileToBase64(file: File): Promise<string>;
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
type FormDataResolutionStrategy = "stringify" | "empty" | "omit";
|
|
12
|
+
interface CreateFormDataOptionsUndefinedOrNullResolution {
|
|
13
|
+
undefinedResolution?: FormDataResolutionStrategy;
|
|
14
|
+
nullResolution?: FormDataResolutionStrategy;
|
|
15
|
+
nullableResolution?: never;
|
|
16
|
+
}
|
|
17
|
+
interface CreateFormDataOptionsNullableResolution {
|
|
18
|
+
undefinedResolution?: never;
|
|
19
|
+
nullResolution?: never;
|
|
20
|
+
nullableResolution: FormDataResolutionStrategy;
|
|
21
|
+
}
|
|
22
|
+
type CreateFormDataOptions = CreateFormDataOptionsUndefinedOrNullResolution | CreateFormDataOptionsNullableResolution;
|
|
23
|
+
declare function createFormData<T extends Record<string, unknown>>(data: T, options?: CreateFormDataOptions): FormData;
|
|
12
24
|
|
|
13
25
|
declare function fillArray<T>(callback: (index: number) => T | Promise<T>, length?: number): T[] | Promise<T[]>;
|
|
14
26
|
|
|
@@ -78,4 +90,4 @@ type NonUndefined<T> = T extends undefined ? never : T;
|
|
|
78
90
|
|
|
79
91
|
type OptionalOnCondition<Condition extends boolean, T> = Condition extends true ? T : T | undefined;
|
|
80
92
|
|
|
81
|
-
export { APIError, type DisallowUndefined, type Email, type Env, type HTTPErrorCode, type HTTPErrorCodes, type NonUndefined, type OptionalOnCondition, type UUID, addDaysToDate, appendSemicolon, camelToKebab, convertFileToBase64, createFormData, fillArray, formatDateAndTime, getRandomNumber, httpErrorCodeLookup, interpolateObjects, isLeapYear, isMonthlyMultiple, isSameDate, omitProperties, parseEmail, parseEnv, parseIntStrict, parseUUID, randomiseArray, range, removeDuplicates, stringListToArray, stringToBoolean, truncate, wait };
|
|
93
|
+
export { APIError, type CreateFormDataOptions, type CreateFormDataOptionsNullableResolution, type CreateFormDataOptionsUndefinedOrNullResolution, type DisallowUndefined, type Email, type Env, type FormDataResolutionStrategy, type HTTPErrorCode, type HTTPErrorCodes, type NonUndefined, type OptionalOnCondition, type StringListToArrayOptions, type UUID, addDaysToDate, appendSemicolon, camelToKebab, convertFileToBase64, createFormData, fillArray, formatDateAndTime, getRandomNumber, httpErrorCodeLookup, interpolateObjects, isLeapYear, isMonthlyMultiple, isSameDate, omitProperties, parseEmail, parseEnv, parseIntStrict, parseUUID, randomiseArray, range, removeDuplicates, stringListToArray, stringToBoolean, truncate, wait };
|
package/dist/index.d.ts
CHANGED
|
@@ -8,7 +8,19 @@ declare function camelToKebab(string: string): string;
|
|
|
8
8
|
|
|
9
9
|
declare function convertFileToBase64(file: File): Promise<string>;
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
type FormDataResolutionStrategy = "stringify" | "empty" | "omit";
|
|
12
|
+
interface CreateFormDataOptionsUndefinedOrNullResolution {
|
|
13
|
+
undefinedResolution?: FormDataResolutionStrategy;
|
|
14
|
+
nullResolution?: FormDataResolutionStrategy;
|
|
15
|
+
nullableResolution?: never;
|
|
16
|
+
}
|
|
17
|
+
interface CreateFormDataOptionsNullableResolution {
|
|
18
|
+
undefinedResolution?: never;
|
|
19
|
+
nullResolution?: never;
|
|
20
|
+
nullableResolution: FormDataResolutionStrategy;
|
|
21
|
+
}
|
|
22
|
+
type CreateFormDataOptions = CreateFormDataOptionsUndefinedOrNullResolution | CreateFormDataOptionsNullableResolution;
|
|
23
|
+
declare function createFormData<T extends Record<string, unknown>>(data: T, options?: CreateFormDataOptions): FormData;
|
|
12
24
|
|
|
13
25
|
declare function fillArray<T>(callback: (index: number) => T | Promise<T>, length?: number): T[] | Promise<T[]>;
|
|
14
26
|
|
|
@@ -78,4 +90,4 @@ type NonUndefined<T> = T extends undefined ? never : T;
|
|
|
78
90
|
|
|
79
91
|
type OptionalOnCondition<Condition extends boolean, T> = Condition extends true ? T : T | undefined;
|
|
80
92
|
|
|
81
|
-
export { APIError, type DisallowUndefined, type Email, type Env, type HTTPErrorCode, type HTTPErrorCodes, type NonUndefined, type OptionalOnCondition, type UUID, addDaysToDate, appendSemicolon, camelToKebab, convertFileToBase64, createFormData, fillArray, formatDateAndTime, getRandomNumber, httpErrorCodeLookup, interpolateObjects, isLeapYear, isMonthlyMultiple, isSameDate, omitProperties, parseEmail, parseEnv, parseIntStrict, parseUUID, randomiseArray, range, removeDuplicates, stringListToArray, stringToBoolean, truncate, wait };
|
|
93
|
+
export { APIError, type CreateFormDataOptions, type CreateFormDataOptionsNullableResolution, type CreateFormDataOptionsUndefinedOrNullResolution, type DisallowUndefined, type Email, type Env, type FormDataResolutionStrategy, type HTTPErrorCode, type HTTPErrorCodes, type NonUndefined, type OptionalOnCondition, type StringListToArrayOptions, type UUID, addDaysToDate, appendSemicolon, camelToKebab, convertFileToBase64, createFormData, fillArray, formatDateAndTime, getRandomNumber, httpErrorCodeLookup, interpolateObjects, isLeapYear, isMonthlyMultiple, isSameDate, omitProperties, parseEmail, parseEnv, parseIntStrict, parseUUID, randomiseArray, range, removeDuplicates, stringListToArray, stringToBoolean, truncate, wait };
|
package/dist/index.js
CHANGED
|
@@ -63,15 +63,44 @@ function convertFileToBase64(file) {
|
|
|
63
63
|
var convertFileToBase64_default = convertFileToBase64;
|
|
64
64
|
|
|
65
65
|
// src/functions/createFormData.ts
|
|
66
|
-
function createFormData(data) {
|
|
66
|
+
function createFormData(data, options = { nullableResolution: "empty" }) {
|
|
67
67
|
const formData = new FormData();
|
|
68
|
+
function resolveByStrategy(key, value, resolutionStrategy) {
|
|
69
|
+
switch (resolutionStrategy) {
|
|
70
|
+
case "empty":
|
|
71
|
+
formData.append(key, "");
|
|
72
|
+
break;
|
|
73
|
+
case "stringify":
|
|
74
|
+
formData.append(key, JSON.stringify(value));
|
|
75
|
+
break;
|
|
76
|
+
case "omit":
|
|
77
|
+
break;
|
|
78
|
+
default:
|
|
79
|
+
throw new Error("SLOPPY_PURE_JAVASCRIPT_USER_ERROR");
|
|
80
|
+
}
|
|
81
|
+
}
|
|
68
82
|
for (const key in data) {
|
|
69
83
|
if (data[key] instanceof Blob) {
|
|
70
84
|
formData.append(key, data[key]);
|
|
85
|
+
} else if (data[key] === void 0 || data[key] === null) {
|
|
86
|
+
if (options.nullableResolution) {
|
|
87
|
+
resolveByStrategy(key, data[key], options.nullableResolution);
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
if (options.undefinedResolution || options.nullResolution) {
|
|
91
|
+
if (data[key] === void 0 && options.undefinedResolution) {
|
|
92
|
+
resolveByStrategy(key, data[key], options.undefinedResolution);
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
if (data[key] === null && options.nullResolution) {
|
|
96
|
+
resolveByStrategy(key, data[key], options.nullResolution);
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
71
100
|
} else if (typeof data[key] === "object") {
|
|
72
101
|
formData.append(key, JSON.stringify(data[key]));
|
|
73
102
|
} else {
|
|
74
|
-
formData.append(key, data[key]);
|
|
103
|
+
formData.append(key, String(data[key]));
|
|
75
104
|
}
|
|
76
105
|
}
|
|
77
106
|
return formData;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alextheman/utility",
|
|
3
|
-
"version": "2.10.
|
|
3
|
+
"version": "2.10.3",
|
|
4
4
|
"description": "Helpful utility functions",
|
|
5
5
|
"license": "ISC",
|
|
6
6
|
"author": "alextheman",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"zod": "^4.1.12"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@alextheman/eslint-plugin": "^2.
|
|
39
|
+
"@alextheman/eslint-plugin": "^2.5.0",
|
|
40
40
|
"@eslint/js": "^9.39.0",
|
|
41
41
|
"@types/node": "^24.9.2",
|
|
42
42
|
"eslint": "^9.39.0",
|