@duplojs/utils 1.5.2 → 1.5.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/clean/primitive/base.d.ts +1 -1
- package/dist/common/formData.cjs +15 -1
- package/dist/common/formData.d.ts +61 -3
- package/dist/common/formData.mjs +15 -1
- package/dist/dataParser/identifier.d.ts +2 -2
- package/dist/date/createOrThrow.cjs +1 -1
- package/dist/date/createOrThrow.mjs +1 -1
- package/package.json +1 -1
|
@@ -239,7 +239,7 @@ export type Boolean = ReturnType<typeof Boolean["createWithUnknownOrThrow"]>;
|
|
|
239
239
|
* @namespace C
|
|
240
240
|
*
|
|
241
241
|
*/
|
|
242
|
-
export declare const Date: PrimitiveHandler<DDate.TheDate,
|
|
242
|
+
export declare const Date: PrimitiveHandler<DDate.TheDate, `date${number}-` | `date${number}+` | DDate.TheDate | globalThis.Date>;
|
|
243
243
|
export type Date = ReturnType<typeof Date["createWithUnknownOrThrow"]>;
|
|
244
244
|
/**
|
|
245
245
|
* Business primitive for duration values (`TheTime`).
|
package/dist/common/formData.cjs
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var escapeRegExp = require('./escapeRegExp.cjs');
|
|
4
|
+
var theTime = require('../date/theTime.cjs');
|
|
5
|
+
var theDate = require('../date/theDate.cjs');
|
|
4
6
|
|
|
5
7
|
const separator = "/*\\";
|
|
6
8
|
const firstElementInPathRegex = new RegExp(`^((?:(?!${escapeRegExp.escapeRegExp(separator)}).)*)(?:${escapeRegExp.escapeRegExp(separator)})?`);
|
|
@@ -17,9 +19,18 @@ class TheFormData extends FormData {
|
|
|
17
19
|
}
|
|
18
20
|
static *toFlatEntries(value, path) {
|
|
19
21
|
if (typeof value === "string"
|
|
20
|
-
|| value instanceof
|
|
22
|
+
|| value instanceof theTime.TheTime
|
|
23
|
+
|| value instanceof theDate.TheDate
|
|
24
|
+
|| typeof value === "number"
|
|
25
|
+
|| typeof value === "boolean") {
|
|
26
|
+
yield [path ?? "", value.toString()];
|
|
27
|
+
}
|
|
28
|
+
else if (value instanceof File) {
|
|
21
29
|
yield [path ?? "", value];
|
|
22
30
|
}
|
|
31
|
+
else if (value === null) {
|
|
32
|
+
yield [path ?? "", "null"];
|
|
33
|
+
}
|
|
23
34
|
else if (value === undefined) {
|
|
24
35
|
return;
|
|
25
36
|
}
|
|
@@ -83,6 +94,9 @@ class TheFormData extends FormData {
|
|
|
83
94
|
return new TheFormData(inputValues);
|
|
84
95
|
}
|
|
85
96
|
}
|
|
97
|
+
/**
|
|
98
|
+
* {@include common/createFormData/index.md}
|
|
99
|
+
*/
|
|
86
100
|
function createFormData(inputValues) {
|
|
87
101
|
return TheFormData.new(inputValues);
|
|
88
102
|
}
|
|
@@ -1,11 +1,69 @@
|
|
|
1
|
-
|
|
1
|
+
import * as DDate from "../date";
|
|
2
|
+
export type EligibleFormDataValue = (boolean | number | null | string | File | undefined | DDate.TheDate | DDate.TheTime | {
|
|
2
3
|
[key: string]: EligibleFormDataValue;
|
|
3
4
|
} | EligibleFormDataValue[]);
|
|
4
5
|
export declare class TheFormData<GenericValues extends Record<string, EligibleFormDataValue>> extends FormData {
|
|
5
6
|
readonly inputValues: GenericValues;
|
|
6
7
|
private constructor();
|
|
7
|
-
static toFlatEntries(value: EligibleFormDataValue, path?: string): Iterable<[string, string | File]>;
|
|
8
|
+
static toFlatEntries(value: EligibleFormDataValue, path?: string): Iterable<[string, string | File], void>;
|
|
8
9
|
static fromEntries(iterable: Iterable<[string, unknown]>, arrayMaxIndex: number): object;
|
|
9
10
|
}
|
|
11
|
+
/**
|
|
12
|
+
* Creates an extended `FormData` instance from nested values and provides helpers to flatten or rebuild entries.
|
|
13
|
+
*
|
|
14
|
+
* **Supported call styles:**
|
|
15
|
+
* - Classic: `createFormData(inputValues)` -> returns a `TheFormData` instance
|
|
16
|
+
*
|
|
17
|
+
* The returned instance extends native `FormData` and stores the original object in `inputValues`.
|
|
18
|
+
* Supported values are `string`, `File`, `boolean`, `number`, `null`, `undefined`, `DDate.TheDate`, `DDate.TheTime`, objects, and arrays.
|
|
19
|
+
* At flatten time, non-`File` values are serialized to strings (`null` becomes `"null"`).
|
|
20
|
+
* For backend reconstruction, use `TheFormData.fromEntries(...)` to rebuild nested objects from flattened form-data keys.
|
|
21
|
+
*
|
|
22
|
+
* ```ts
|
|
23
|
+
* const created = createFormData(
|
|
24
|
+
* {
|
|
25
|
+
* profile: {
|
|
26
|
+
* name: "Ada",
|
|
27
|
+
* tags: ["core", "utils"],
|
|
28
|
+
* },
|
|
29
|
+
* enabled: true,
|
|
30
|
+
* retryCount: 2,
|
|
31
|
+
* optionalValue: null,
|
|
32
|
+
* createdDate: DDate.createOrThrow(1735689600000),
|
|
33
|
+
* createdTime: DDate.createTimeOrThrow(45000000),
|
|
34
|
+
* },
|
|
35
|
+
* );
|
|
36
|
+
* const flattenedName = created.get("profile/*\\name");
|
|
37
|
+
* // flattenedName: "Ada"
|
|
38
|
+
* const flattenedEnabled = created.get("enabled");
|
|
39
|
+
* // flattenedEnabled: "true"
|
|
40
|
+
* const flattenedNull = created.get("optionalValue");
|
|
41
|
+
* // flattenedNull: "null"
|
|
42
|
+
*
|
|
43
|
+
* const flatEntries = Array.from(
|
|
44
|
+
* TheFormData.toFlatEntries({
|
|
45
|
+
* tags: ["core", "utils"],
|
|
46
|
+
* time: DDate.createTimeOrThrow(31500000),
|
|
47
|
+
* }),
|
|
48
|
+
* );
|
|
49
|
+
* // flatEntries: [["tags/*\\[0]", "core"], ["tags/*\\[1]", "utils"], ["time", "time31500000+"]]
|
|
50
|
+
*
|
|
51
|
+
* const rebuilt = TheFormData.fromEntries(
|
|
52
|
+
* [
|
|
53
|
+
* ["profile/*\\name", "Ada"],
|
|
54
|
+
* ["profile/*\\tags/*\\[0]", "core"],
|
|
55
|
+
* ["enabled", "true"],
|
|
56
|
+
* ],
|
|
57
|
+
* 10,
|
|
58
|
+
* );
|
|
59
|
+
* // rebuilt: { profile: { name: "Ada", tags: ["core"] }, enabled: "true" }
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* @remarks
|
|
63
|
+
* - `TheFormData.toFlatEntries(...)` flattens nested objects/arrays into path keys using the `/*\` separator.
|
|
64
|
+
* - `TheFormData.fromEntries(...)` ignores `__proto__`, `constructor`, and `prototype` paths for safety.
|
|
65
|
+
*
|
|
66
|
+
* @see https://utils.duplojs.dev/en/v1/api/common/createFormData
|
|
67
|
+
*
|
|
68
|
+
*/
|
|
10
69
|
export declare function createFormData<GenericValues extends Record<string, EligibleFormDataValue>>(inputValues: GenericValues): TheFormData<GenericValues>;
|
|
11
|
-
export {};
|
package/dist/common/formData.mjs
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { escapeRegExp } from './escapeRegExp.mjs';
|
|
2
|
+
import { TheTime } from '../date/theTime.mjs';
|
|
3
|
+
import { TheDate } from '../date/theDate.mjs';
|
|
2
4
|
|
|
3
5
|
const separator = "/*\\";
|
|
4
6
|
const firstElementInPathRegex = new RegExp(`^((?:(?!${escapeRegExp(separator)}).)*)(?:${escapeRegExp(separator)})?`);
|
|
@@ -15,9 +17,18 @@ class TheFormData extends FormData {
|
|
|
15
17
|
}
|
|
16
18
|
static *toFlatEntries(value, path) {
|
|
17
19
|
if (typeof value === "string"
|
|
18
|
-
|| value instanceof
|
|
20
|
+
|| value instanceof TheTime
|
|
21
|
+
|| value instanceof TheDate
|
|
22
|
+
|| typeof value === "number"
|
|
23
|
+
|| typeof value === "boolean") {
|
|
24
|
+
yield [path ?? "", value.toString()];
|
|
25
|
+
}
|
|
26
|
+
else if (value instanceof File) {
|
|
19
27
|
yield [path ?? "", value];
|
|
20
28
|
}
|
|
29
|
+
else if (value === null) {
|
|
30
|
+
yield [path ?? "", "null"];
|
|
31
|
+
}
|
|
21
32
|
else if (value === undefined) {
|
|
22
33
|
return;
|
|
23
34
|
}
|
|
@@ -81,6 +92,9 @@ class TheFormData extends FormData {
|
|
|
81
92
|
return new TheFormData(inputValues);
|
|
82
93
|
}
|
|
83
94
|
}
|
|
95
|
+
/**
|
|
96
|
+
* {@include common/createFormData/index.md}
|
|
97
|
+
*/
|
|
84
98
|
function createFormData(inputValues) {
|
|
85
99
|
return TheFormData.new(inputValues);
|
|
86
100
|
}
|
|
@@ -42,7 +42,7 @@ export declare const identifier: {
|
|
|
42
42
|
input: boolean;
|
|
43
43
|
output: boolean;
|
|
44
44
|
}>> | import("../common").KindHandler<import("../common").KindDefinition<"@DuplojsUtilsDataParser/base", {
|
|
45
|
-
input:
|
|
45
|
+
input: `date${number}-` | `date${number}+` | import("../date").TheDate | Date;
|
|
46
46
|
output: import("../date").TheDate;
|
|
47
47
|
}>> | import("../common").KindHandler<import("../common").KindDefinition<"@DuplojsUtilsDataParser/base", {
|
|
48
48
|
input: number | import("../date").TheTime | `time${number}-` | `time${number}+`;
|
|
@@ -91,7 +91,7 @@ export declare const identifier: {
|
|
|
91
91
|
input: boolean;
|
|
92
92
|
output: boolean;
|
|
93
93
|
}>> | import("../common").KindHandler<import("../common").KindDefinition<"@DuplojsUtilsDataParser/base", {
|
|
94
|
-
input:
|
|
94
|
+
input: `date${number}-` | `date${number}+` | import("../date").TheDate | Date;
|
|
95
95
|
output: import("../date").TheDate;
|
|
96
96
|
}>> | import("../common").KindHandler<import("../common").KindDefinition<"@DuplojsUtilsDataParser/base", {
|
|
97
97
|
input: number | import("../date").TheTime | `time${number}-` | `time${number}+`;
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
var create = require('./create.cjs');
|
|
4
4
|
var kind = require('../common/kind.cjs');
|
|
5
|
+
var errorKindNamespace = require('../common/errorKindNamespace.cjs');
|
|
5
6
|
var is = require('../either/left/is.cjs');
|
|
6
7
|
var unwrap = require('../common/unwrap.cjs');
|
|
7
|
-
var errorKindNamespace = require('../common/errorKindNamespace.cjs');
|
|
8
8
|
|
|
9
9
|
class CreateTheDateError extends kind.kindHeritage("create-the-date-error", errorKindNamespace.createErrorKind("create-the-date-error"), Error) {
|
|
10
10
|
input;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { create } from './create.mjs';
|
|
2
2
|
import { kindHeritage } from '../common/kind.mjs';
|
|
3
|
+
import { createErrorKind } from '../common/errorKindNamespace.mjs';
|
|
3
4
|
import { isLeft } from '../either/left/is.mjs';
|
|
4
5
|
import { unwrap } from '../common/unwrap.mjs';
|
|
5
|
-
import { createErrorKind } from '../common/errorKindNamespace.mjs';
|
|
6
6
|
|
|
7
7
|
class CreateTheDateError extends kindHeritage("create-the-date-error", createErrorKind("create-the-date-error"), Error) {
|
|
8
8
|
input;
|