@vielzeug/forge 26.11.11 → 26.11.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/_field.cjs +1 -1
- package/dist/_field.cjs.map +1 -1
- package/dist/_field.d.ts +2 -2
- package/dist/_field.d.ts.map +1 -1
- package/dist/_field.js +32 -31
- package/dist/_field.js.map +1 -1
- package/dist/core/path.cjs +1 -1
- package/dist/core/path.cjs.map +1 -1
- package/dist/core/path.d.ts +1 -8
- package/dist/core/path.d.ts.map +1 -1
- package/dist/core/path.js +16 -43
- package/dist/core/path.js.map +1 -1
- package/dist/core/touched.cjs +2 -0
- package/dist/core/touched.cjs.map +1 -0
- package/dist/core/touched.d.ts +9 -0
- package/dist/core/touched.d.ts.map +1 -0
- package/dist/core/touched.js +29 -0
- package/dist/core/touched.js.map +1 -0
- package/dist/dom.cjs +1 -1
- package/dist/dom.cjs.map +1 -1
- package/dist/dom.d.ts.map +1 -1
- package/dist/dom.js +4 -4
- package/dist/dom.js.map +1 -1
- package/dist/forge.cjs +1 -1
- package/dist/forge.cjs.map +1 -1
- package/dist/forge.iife.js +1 -1
- package/dist/forge.iife.js.map +1 -1
- package/dist/forge.js +110 -109
- package/dist/forge.js.map +1 -1
- package/dist/form-data.cjs +1 -1
- package/dist/form-data.cjs.map +1 -1
- package/dist/form-data.js +1 -1
- package/dist/form-data.js.map +1 -1
- package/dist/form.cjs +1 -1
- package/dist/form.cjs.map +1 -1
- package/dist/form.d.ts.map +1 -1
- package/dist/form.js +77 -74
- package/dist/form.js.map +1 -1
- package/dist/persist.cjs +1 -1
- package/dist/persist.cjs.map +1 -1
- package/dist/persist.d.ts +21 -13
- package/dist/persist.d.ts.map +1 -1
- package/dist/persist.js +13 -12
- package/dist/persist.js.map +1 -1
- package/dist/plain.cjs.map +1 -1
- package/dist/plain.d.ts +0 -4
- package/dist/plain.d.ts.map +1 -1
- package/dist/plain.js.map +1 -1
- package/package.json +4 -4
package/dist/_field.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const e=require("./core/path.cjs"),t=require("./errors.cjs");let n=require("@vielzeug/arsenal");function makeFieldState(t,r){let
|
|
1
|
+
const e=require("./core/path.cjs"),t=require("./errors.cjs");let n=require("@vielzeug/arsenal");function makeFieldState(t,r,i){let a=e.readAtPath(t.value,r),o=e.readAtPath(t.baseline,r);return Object.freeze({dirty:!(0,n.isEqual)(a,o)||e.hasAtPath(t.value,r)!==e.hasAtPath(t.baseline,r),error:e.findIssue(t.issues,r),touched:t.touched.has(i),value:a})}function createField(r,i){let a=e.formatValuePath(r),readField=()=>makeFieldState(i.readState(),r,a);return Object.assign({get dirty(){return readField().dirty},get error(){return readField().error},reset(){i.ensureActive(`field().reset`),i.abortValidation(),i.resetValue(r)},set(e){i.ensureActive(`field().set`),i.abortValidation(),i.setValue(r,e)},get state(){return readField()},subscribe(e,t={}){i.ensureActive(`field().subscribe`);let n=readField();t.immediate&&i.invokeListener(()=>e(n));let filtered=t=>{let i=makeFieldState(t,r,a);(i.value!==n.value||i.error!==n.error||i.touched!==n.touched||i.dirty!==n.dirty)&&(n=i,e(i))};return i.addListener(filtered)},touch(){i.ensureActive(`field().touch`),i.setTouched(r,!0)},get touched(){return readField().touched},get value(){return readField().value}},{field(a){if(typeof a==`number`){e.assertArrayIndex(a);let n=e.readAtPath(i.readState().value,r);if(n!==void 0&&!Array.isArray(n))throw new t.ForgeConfigError(`Cannot index ${a} because the current field value is not an array.`);return createField([...r,a],i)}e.assertSafeKey(a);let o=e.readAtPath(i.readState().value,r);if(o!==void 0&&!(0,n.isPlainObject)(o))throw new t.ForgeConfigError(`Cannot select '${a}' because the current field value is not an object.`);return createField([...r,a],i)}})}exports.createField=createField;
|
|
2
2
|
//# sourceMappingURL=_field.cjs.map
|
package/dist/_field.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"_field.cjs","names":["readAtPath","isEqual","hasAtPath","findIssue","
|
|
1
|
+
{"version":3,"file":"_field.cjs","names":["readAtPath","isEqual","hasAtPath","findIssue","formatValuePath","ForgeConfigError","isRecord","require_path","require_errors","_vielzeug_arsenal","value","state","path","baseline","touchedKey","access","next","previous","subscribeOptions","listener","source","key","currentVal"],"sources":["../src/_field.ts"],"sourcesContent":["import { isEqual } from '@vielzeug/arsenal';\n\nimport {\n assertArrayIndex,\n assertSafeKey,\n findIssue,\n formatValuePath,\n hasAtPath,\n isRecord,\n readAtPath,\n} from './core/path.js';\nimport type { TouchedPaths } from './core/touched.js';\nimport { ForgeConfigError } from './errors.js';\nimport type { Field, FieldState, SubscribeOptions, Unsubscribe, ValidationIssue } from './types.js';\n\ntype FieldSourceState = {\n baseline: unknown;\n issues: readonly ValidationIssue[] | undefined;\n touched: TouchedPaths;\n value: unknown;\n};\n\nexport interface FieldAccess {\n abortValidation(): void;\n addListener(listener: (state: FieldSourceState) => void): Unsubscribe;\n ensureActive(operation: string): void;\n invokeListener(listener: () => void): void;\n readState(): FieldSourceState;\n resetValue(path: readonly (string | number)[]): void;\n setTouched(path: readonly (string | number)[], touched: boolean): void;\n setValue(path: readonly (string | number)[], next: unknown | ((previous: unknown) => unknown)): void;\n}\n\nfunction makeFieldState<V>(\n state: FieldSourceState,\n path: readonly (string | number)[],\n touchedKey: string,\n): FieldState<V> {\n const value = readAtPath<V>(state.value, path);\n const baseline = readAtPath<V>(state.baseline, path);\n\n return Object.freeze({\n dirty: !isEqual(value, baseline) || hasAtPath(state.value, path) !== hasAtPath(state.baseline, path),\n error: findIssue(state.issues, path),\n touched: state.touched.has(touchedKey),\n value: value as FieldState<V>['value'],\n });\n}\n\nexport function createField<V>(path: readonly (string | number)[], access: FieldAccess): Field<V> {\n const touchedKey = formatValuePath(path);\n const readField = (): FieldState<V> => makeFieldState<V>(access.readState(), path, touchedKey);\n const common = {\n get dirty() {\n return readField().dirty;\n },\n get error() {\n return readField().error;\n },\n reset() {\n access.ensureActive('field().reset');\n access.abortValidation();\n access.resetValue(path);\n },\n set(next: V | ((previous: FieldState<V>['value']) => V)) {\n access.ensureActive('field().set');\n access.abortValidation();\n\n access.setValue(path, next as unknown | ((previous: unknown) => unknown));\n },\n get state() {\n return readField();\n },\n subscribe(listener: (state: FieldState<V>) => void, subscribeOptions: SubscribeOptions = {}): Unsubscribe {\n access.ensureActive('field().subscribe');\n\n let previous = readField();\n\n if (subscribeOptions.immediate) access.invokeListener(() => listener(previous));\n\n const filtered = (source: FieldSourceState) => {\n const next = makeFieldState<V>(source, path, touchedKey);\n\n if (\n next.value === previous.value &&\n next.error === previous.error &&\n next.touched === previous.touched &&\n next.dirty === previous.dirty\n ) {\n return;\n }\n\n previous = next;\n listener(next);\n };\n\n return access.addListener(filtered);\n },\n touch() {\n access.ensureActive('field().touch');\n access.setTouched(path, true);\n },\n get touched() {\n return readField().touched;\n },\n get value() {\n return readField().value;\n },\n };\n\n return Object.assign(common, {\n field(key: string | number): Field<unknown> {\n if (typeof key === 'number') {\n assertArrayIndex(key);\n const currentVal = readAtPath(access.readState().value, path);\n\n if (currentVal !== undefined && !Array.isArray(currentVal)) {\n throw new ForgeConfigError(`Cannot index ${key} because the current field value is not an array.`);\n }\n\n return createField([...path, key], access);\n }\n\n assertSafeKey(key);\n\n const currentVal = readAtPath(access.readState().value, path);\n\n if (currentVal !== undefined && !isRecord(currentVal)) {\n throw new ForgeConfigError(`Cannot select '${key}' because the current field value is not an object.`);\n }\n\n return createField([...path, key], access);\n },\n }) as unknown as Field<V>;\n}\n"],"mappings":"A,gGAiCA,SAAS,eACP,EACA,EACA,EACe,CACf,IAAMU,EAAQV,EAAAA,WAAcW,EAAM,MAAOC,CAAI,EACvCC,EAAWb,EAAAA,WAAcW,EAAM,SAAUC,CAAI,EAEnD,OAAO,OAAO,OAAO,CACnB,MAAO,EAAA,EAACX,EAAAA,QAAAA,CAAQS,EAAOG,CAAQ,GAAKX,EAAAA,UAAUS,EAAM,MAAOC,CAAI,IAAMV,EAAAA,UAAUS,EAAM,SAAUC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,MAAM,EAAsC,CAC1C,GAAI,OAAOS,GAAQ,SAAU,CAC3Bd,EAAA,iBAAiBc,CAAG,EACpB,IAAMC,EAAatB,EAAAA,WAAWe,EAAO,UAAU,CAAC,CAAC,MAAOH,CAAI,EAE5D,GAAIU,IAAe,IAAA,IAAa,CAAC,MAAM,QAAQA,CAAU,EACvD,MAAM,IAAIjB,EAAAA,iBAAiB,gBAAgBgB,EAAI,kDAAkD,EAGnG,OAAO,YAAY,CAAC,GAAGT,EAAMS,CAAG,EAAGN,CAAM,CAC3C,CAEAR,EAAA,cAAcc,CAAG,EAEjB,IAAMC,EAAatB,EAAAA,WAAWe,EAAO,UAAU,CAAC,CAAC,MAAOH,CAAI,EAE5D,GAAIU,IAAe,IAAA,IAAa,EAAA,EAAChB,EAAAA,cAAAA,CAASgB,CAAU,EAClD,MAAM,IAAIjB,EAAAA,iBAAiB,kBAAkBgB,EAAI,oDAAoD,EAGvG,OAAO,YAAY,CAAC,GAAGT,EAAMS,CAAG,EAAGN,CAAM,CAC3C,CACF,CAAC,CACH,C"}
|
package/dist/_field.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { TouchedPaths } from './core/touched.js';
|
|
2
2
|
import type { Field, Unsubscribe, ValidationIssue } from './types.js';
|
|
3
3
|
type FieldSourceState = {
|
|
4
4
|
baseline: unknown;
|
|
5
5
|
issues: readonly ValidationIssue[] | undefined;
|
|
6
|
-
touched:
|
|
6
|
+
touched: TouchedPaths;
|
|
7
7
|
value: unknown;
|
|
8
8
|
};
|
|
9
9
|
export interface FieldAccess {
|
package/dist/_field.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"_field.d.ts","sourceRoot":"","sources":["../src/_field.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"_field.d.ts","sourceRoot":"","sources":["../src/_field.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,YAAY,EAAE,MA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rayIndex as e, assertSafeKey as t, findIssue as n, formatValuePath as r, hasAtPath as i, isRecord as a, readAtPath as o } from "./core/path.js";
|
|
2
2
|
import { ForgeConfigError as s } from "./errors.js";
|
|
3
3
|
import { isEqual as c } from "@vielzeug/arsenal";
|
|
4
4
|
// src/_field.ts
|
|
5
|
-
function makeFieldState(e, t) {
|
|
6
|
-
let
|
|
5
|
+
function makeFieldState(e, t, r) {
|
|
6
|
+
let a = o(e.value, t), s = o(e.baseline, t);
|
|
7
7
|
return Object.freeze({
|
|
8
|
-
dirty: !c(
|
|
8
|
+
dirty: !c(a, s) || i(e.value, t) !== i(e.baseline, t),
|
|
9
9
|
error: n(e.issues, t),
|
|
10
|
-
touched:
|
|
11
|
-
value:
|
|
10
|
+
touched: e.touched.has(r),
|
|
11
|
+
value: a
|
|
12
12
|
});
|
|
13
13
|
}
|
|
14
|
-
function createField(n,
|
|
14
|
+
function createField(n, i) {
|
|
15
|
+
let c = r(n), readField = () => makeFieldState(i.readState(), n, c);
|
|
15
16
|
return Object.assign({
|
|
16
17
|
get dirty() {
|
|
17
|
-
return
|
|
18
|
+
return readField().dirty;
|
|
18
19
|
},
|
|
19
20
|
get error() {
|
|
20
|
-
return
|
|
21
|
+
return readField().error;
|
|
21
22
|
},
|
|
22
23
|
reset() {
|
|
23
|
-
|
|
24
|
+
i.ensureActive("field().reset"), i.abortValidation(), i.resetValue(n);
|
|
24
25
|
},
|
|
25
26
|
set(e) {
|
|
26
|
-
|
|
27
|
+
i.ensureActive("field().set"), i.abortValidation(), i.setValue(n, e);
|
|
27
28
|
},
|
|
28
29
|
get state() {
|
|
29
|
-
return
|
|
30
|
+
return readField();
|
|
30
31
|
},
|
|
31
32
|
subscribe(e, t = {}) {
|
|
32
|
-
|
|
33
|
-
let
|
|
34
|
-
t.immediate &&
|
|
33
|
+
i.ensureActive("field().subscribe");
|
|
34
|
+
let r = readField();
|
|
35
|
+
t.immediate && i.invokeListener(() => e(r));
|
|
35
36
|
let filtered = (t) => {
|
|
36
|
-
let
|
|
37
|
-
(
|
|
37
|
+
let i = makeFieldState(t, n, c);
|
|
38
|
+
(i.value !== r.value || i.error !== r.error || i.touched !== r.touched || i.dirty !== r.dirty) && (r = i, e(i));
|
|
38
39
|
};
|
|
39
|
-
return
|
|
40
|
+
return i.addListener(filtered);
|
|
40
41
|
},
|
|
41
42
|
touch() {
|
|
42
|
-
|
|
43
|
+
i.ensureActive("field().touch"), i.setTouched(n, !0);
|
|
43
44
|
},
|
|
44
45
|
get touched() {
|
|
45
|
-
return
|
|
46
|
+
return readField().touched;
|
|
46
47
|
},
|
|
47
48
|
get value() {
|
|
48
|
-
return
|
|
49
|
+
return readField().value;
|
|
49
50
|
}
|
|
50
|
-
}, { field(
|
|
51
|
-
if (typeof
|
|
52
|
-
e(
|
|
53
|
-
let t =
|
|
54
|
-
if (t !== void 0 && !Array.isArray(t)) throw new s(`Cannot index ${
|
|
55
|
-
return createField([...n,
|
|
51
|
+
}, { field(r) {
|
|
52
|
+
if (typeof r == "number") {
|
|
53
|
+
e(r);
|
|
54
|
+
let t = o(i.readState().value, n);
|
|
55
|
+
if (t !== void 0 && !Array.isArray(t)) throw new s(`Cannot index ${r} because the current field value is not an array.`);
|
|
56
|
+
return createField([...n, r], i);
|
|
56
57
|
}
|
|
57
|
-
t(
|
|
58
|
-
let c =
|
|
59
|
-
if (c !== void 0 && !
|
|
60
|
-
return createField([...n,
|
|
58
|
+
t(r);
|
|
59
|
+
let c = o(i.readState().value, n);
|
|
60
|
+
if (c !== void 0 && !a(c)) throw new s(`Cannot select '${r}' because the current field value is not an object.`);
|
|
61
|
+
return createField([...n, r], i);
|
|
61
62
|
} });
|
|
62
63
|
}
|
|
63
64
|
export { createField };
|
package/dist/_field.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"_field.js","names":["value","readAtPath","state","path","baseline","isEqual","hasAtPath","findIssue","
|
|
1
|
+
{"version":3,"file":"_field.js","names":["value","readAtPath","state","path","baseline","isEqual","hasAtPath","findIssue","touchedKey","formatValuePath","access","next","previous","subscribeOptions","listener","source","key","currentVal","ForgeConfigError","isRecord"],"sources":["../src/_field.ts"],"sourcesContent":["import { isEqual } from '@vielzeug/arsenal';\n\nimport {\n assertArrayIndex,\n assertSafeKey,\n findIssue,\n formatValuePath,\n hasAtPath,\n isRecord,\n readAtPath,\n} from './core/path.js';\nimport type { TouchedPaths } from './core/touched.js';\nimport { ForgeConfigError } from './errors.js';\nimport type { Field, FieldState, SubscribeOptions, Unsubscribe, ValidationIssue } from './types.js';\n\ntype FieldSourceState = {\n baseline: unknown;\n issues: readonly ValidationIssue[] | undefined;\n touched: TouchedPaths;\n value: unknown;\n};\n\nexport interface FieldAccess {\n abortValidation(): void;\n addListener(listener: (state: FieldSourceState) => void): Unsubscribe;\n ensureActive(operation: string): void;\n invokeListener(listener: () => void): void;\n readState(): FieldSourceState;\n resetValue(path: readonly (string | number)[]): void;\n setTouched(path: readonly (string | number)[], touched: boolean): void;\n setValue(path: readonly (string | number)[], next: unknown | ((previous: unknown) => unknown)): void;\n}\n\nfunction makeFieldState<V>(\n state: FieldSourceState,\n path: readonly (string | number)[],\n touchedKey: string,\n): FieldState<V> {\n const value = readAtPath<V>(state.value, path);\n const baseline = readAtPath<V>(state.baseline, path);\n\n return Object.freeze({\n dirty: !isEqual(value, baseline) || hasAtPath(state.value, path) !== hasAtPath(state.baseline, path),\n error: findIssue(state.issues, path),\n touched: state.touched.has(touchedKey),\n value: value as FieldState<V>['value'],\n });\n}\n\nexport function createField<V>(path: readonly (string | number)[], access: FieldAccess): Field<V> {\n const touchedKey = formatValuePath(path);\n const readField = (): FieldState<V> => makeFieldState<V>(access.readState(), path, touchedKey);\n const common = {\n get dirty() {\n return readField().dirty;\n },\n get error() {\n return readField().error;\n },\n reset() {\n access.ensureActive('field().reset');\n access.abortValidation();\n access.resetValue(path);\n },\n set(next: V | ((previous: FieldState<V>['value']) => V)) {\n access.ensureActive('field().set');\n access.abortValidation();\n\n access.setValue(path, next as unknown | ((previous: unknown) => unknown));\n },\n get state() {\n return readField();\n },\n subscribe(listener: (state: FieldState<V>) => void, subscribeOptions: SubscribeOptions = {}): Unsubscribe {\n access.ensureActive('field().subscribe');\n\n let previous = readField();\n\n if (subscribeOptions.immediate) access.invokeListener(() => listener(previous));\n\n const filtered = (source: FieldSourceState) => {\n const next = makeFieldState<V>(source, path, touchedKey);\n\n if (\n next.value === previous.value &&\n next.error === previous.error &&\n next.touched === previous.touched &&\n next.dirty === previous.dirty\n ) {\n return;\n }\n\n previous = next;\n listener(next);\n };\n\n return access.addListener(filtered);\n },\n touch() {\n access.ensureActive('field().touch');\n access.setTouched(path, true);\n },\n get touched() {\n return readField().touched;\n },\n get value() {\n return readField().value;\n },\n };\n\n return Object.assign(common, {\n field(key: string | number): Field<unknown> {\n if (typeof key === 'number') {\n assertArrayIndex(key);\n const currentVal = readAtPath(access.readState().value, path);\n\n if (currentVal !== undefined && !Array.isArray(currentVal)) {\n throw new ForgeConfigError(`Cannot index ${key} because the current field value is not an array.`);\n }\n\n return createField([...path, key], access);\n }\n\n assertSafeKey(key);\n\n const currentVal = readAtPath(access.readState().value, path);\n\n if (currentVal !== undefined && !isRecord(currentVal)) {\n throw new ForgeConfigError(`Cannot select '${key}' because the current field value is not an object.`);\n }\n\n return createField([...path, key], access);\n },\n }) as unknown as Field<V>;\n}\n"],"mappings":";;;;AAiCA,SAAS,eACP,GACA,GACA,GACe;CACf,IAAMA,IAAQC,EAAcC,EAAM,OAAOC,CAAI,GACvCC,IAAWH,EAAcC,EAAM,UAAUC,CAAI;CAEnD,OAAO,OAAO,OAAO;EACnB,OAAO,CAACE,EAAQL,GAAOI,CAAQ,KAAKE,EAAUJ,EAAM,OAAOC,CAAI,MAAMG,EAAUJ,EAAM,UAAUC,CAAI;EACnG,OAAOI,EAAUL,EAAM,QAAQC,CAAI;EACnC,SAASD,EAAM,QAAQ,IAAIM,CAAU;EAC9B;CACT,CAAC;AACH;AAEA,SAAgB,YA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AAU,CAAC,CAAC,OAAOP,CAAI;GAE5D,IAAIc,MAAe,KAAA,KAAa,CAAC,MAAM,QAAQA,CAAU,GACvD,MAAM,IAAIC,EAAiB,gBAAgBF,EAAI,kDAAkD;GAGnG,OAAO,YAAY,CAAC,GAAGb,GAAMa,CAAG,GAAGN,CAAM;EAC3C;EAEA,EAAcM,CAAG;EAEjB,IAAMC,IAAahB,EAAWS,EAAO,UAAU,CAAC,CAAC,OAAOP,CAAI;EAE5D,IAAIc,MAAe,KAAA,KAAa,CAACE,EAASF,CAAU,GAClD,MAAM,IAAIC,EAAiB,kBAAkBF,EAAI,oDAAoD;EAGvG,OAAO,YAAY,CAAC,GAAGb,GAAMa,CAAG,GAAGN,CAAM;CAC3C,EACF,CAAC;AACH"}
|
package/dist/core/path.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const e=require("../errors.cjs");let t=require("@vielzeug/arsenal");var n=new Set([`__proto__`,`constructor`,`prototype`]);function isUnsafeKey(e){return!e||n.has(e)}function assertSafeKey(t){if(isUnsafeKey(t))throw new e.ForgeConfigError(`Invalid field key '${t}'.`)}function assertArrayIndex(t){if(!Number.isSafeInteger(t)||t<0)throw new e.ForgeConfigError(`Invalid array index ${t}.`)}function tag(e){return Object.prototype.toString.call(e)}function isBlobLeaf(e){return tag(e)===`[object Blob]`||tag(e)===`[object File]`}function formatValuePath(e){let t=``;for(let n of e)t+=typeof n==`number`?`[${n}]`:t?`.${n}`:n;return t}function pathSuffix(e){return e.length>0?` at '${formatValuePath(e)}'`:``}function invalidValue(t){throw new e.ForgeConfigError(`Form values must contain only finite JSON primitives, plain objects, arrays, Date, File, or Blob${pathSuffix(t)}.`)}function immutable(n,r=new Set,i=[]){if(n==null||typeof n==`string`||typeof n==`boolean`)return n;if(typeof n==`number`)return Number.isFinite(n)?n:invalidValue(i);if(typeof n!=`object`)return invalidValue(i);let a=n;if(tag(n)===`[object Date]`&&typeof a.getTime==`function`){let e=a.getTime();return Number.isFinite(e)?Object.freeze(new Date(e)):invalidValue(i)}if(isBlobLeaf(n))return n;if(r.has(n))throw new e.ForgeConfigError(`Form values must not contain circular references${pathSuffix(i)}.`);r.add(n);try{if(Array.isArray(n)){let t=[];for(let a=0;a<n.length;a++){i.push(a);try{if(!Object.hasOwn(n,a))throw new e.ForgeConfigError(`Form arrays must not contain empty slots${pathSuffix(i)}.`);t.push(immutable(n[a],r,i))}finally{i.pop()}}return Object.freeze(t)}if(!(0,t.isPlainObject)(n))return invalidValue(i);let a=Object.entries(n).map(([e,t])=>{assertSafeKey(e),i.push(e);try{return[e,immutable(t,r,i)]}finally{i.pop()}});return Object.freeze(Object.fromEntries(a))}finally{r.delete(n)}}function toPlainValues(e,t=new WeakSet){if(e==null||typeof e==`string`||typeof e==`boolean`)return e;if(typeof e==`number`)return Number.isFinite(e)?e:void 0;if(typeof e!=`object`)return;let n=e,r=e;if(tag(n)===`[object Date]`&&typeof r.getTime==`function`){let e=r.getTime();return Number.isFinite(e)?new Date(e):void 0}if(isBlobLeaf(n))return e;if(!t.has(n)){t.add(n);try{if(Array.isArray(e)){let n=[];for(let r=0;r<e.length;r++)Object.hasOwn(e,r)&&n.push(toPlainValues(e[r],t));return n}let r={};for(let[e,i]of Object.entries(n))isUnsafeKey(e)||(r[e]=toPlainValues(i,t));return r}finally{t.delete(n)}}}function
|
|
1
|
+
const e=require("../errors.cjs");let t=require("@vielzeug/arsenal");var n=new Set([`__proto__`,`constructor`,`prototype`]);function isUnsafeKey(e){return!e||e.includes(`.`)||n.has(e)}function assertSafeKey(t){if(isUnsafeKey(t))throw new e.ForgeConfigError(`Invalid field key '${t}'.`)}function assertArrayIndex(t){if(!Number.isSafeInteger(t)||t<0)throw new e.ForgeConfigError(`Invalid array index ${t}.`)}function tag(e){return Object.prototype.toString.call(e)}function isBlobLeaf(e){return tag(e)===`[object Blob]`||tag(e)===`[object File]`}function formatValuePath(e){let t=``;for(let n of e)t+=typeof n==`number`?`[${n}]`:t?`.${n}`:n;return t}function pathSuffix(e){return e.length>0?` at '${formatValuePath(e)}'`:``}function invalidValue(t){throw new e.ForgeConfigError(`Form values must contain only finite JSON primitives, plain objects, arrays, Date, File, or Blob${pathSuffix(t)}.`)}function immutable(n,r=new Set,i=[]){if(n==null||typeof n==`string`||typeof n==`boolean`)return n;if(typeof n==`number`)return Number.isFinite(n)?n:invalidValue(i);if(typeof n!=`object`)return invalidValue(i);let a=n;if(tag(n)===`[object Date]`&&typeof a.getTime==`function`){let e=a.getTime();return Number.isFinite(e)?Object.freeze(new Date(e)):invalidValue(i)}if(isBlobLeaf(n))return n;if(r.has(n))throw new e.ForgeConfigError(`Form values must not contain circular references${pathSuffix(i)}.`);r.add(n);try{if(Array.isArray(n)){let t=[];for(let a=0;a<n.length;a++){i.push(a);try{if(!Object.hasOwn(n,a))throw new e.ForgeConfigError(`Form arrays must not contain empty slots${pathSuffix(i)}.`);t.push(immutable(n[a],r,i))}finally{i.pop()}}return Object.freeze(t)}if(!(0,t.isPlainObject)(n))return invalidValue(i);let a=Object.entries(n).map(([e,t])=>{assertSafeKey(e),i.push(e);try{return[e,immutable(t,r,i)]}finally{i.pop()}});return Object.freeze(Object.fromEntries(a))}finally{r.delete(n)}}function toPlainValues(e,t=new WeakSet){if(e==null||typeof e==`string`||typeof e==`boolean`)return e;if(typeof e==`number`)return Number.isFinite(e)?e:void 0;if(typeof e!=`object`)return;let n=e,r=e;if(tag(n)===`[object Date]`&&typeof r.getTime==`function`){let e=r.getTime();return Number.isFinite(e)?new Date(e):void 0}if(isBlobLeaf(n))return e;if(!t.has(n)){t.add(n);try{if(Array.isArray(e)){let n=[];for(let r=0;r<e.length;r++)Object.hasOwn(e,r)&&n.push(toPlainValues(e[r],t));return n}let r={};for(let[e,i]of Object.entries(n))isUnsafeKey(e)||(r[e]=toPlainValues(i,t));return r}finally{t.delete(n)}}}var r=Symbol(`forge-path-not-found`);function walk(e,n){let i=e;for(let e of n)if(typeof e==`number`){if(!Array.isArray(i)||!Number.isSafeInteger(e)||e<0||e>=i.length||!Object.hasOwn(i,e))return r;i=i[e]}else{if(!(0,t.isPlainObject)(i)||!Object.hasOwn(i,e))return r;i=i[e]}return i}function readAtPath(e,t){let n=walk(e,t);return n===r?void 0:n}function hasAtPath(e,t){return walk(e,t)!==r}function writeAtPath(n,r,i,a=[]){let[o,...s]=r;if(typeof o==`number`){if(assertArrayIndex(o),!Array.isArray(n))throw new e.ForgeConfigError(`Cannot index ${o} because the current value is not an array.`);if(o<0||o>=n.length)throw new e.ForgeConfigError(`Index ${o} is out of range for array of length ${n.length}.`);if(s.length===0){let e=[...n];return e[o]=immutable(i,new Set,[...a,o]),Object.freeze(e)}let t=[...n];return t[o]=writeAtPath(n[o],s,i,[...a,o]),Object.freeze(t)}if(assertSafeKey(o),!(0,t.isPlainObject)(n))throw new e.ForgeConfigError(`Cannot select '${o}' because the current value is not an object.`);if(s.length===0)return Object.freeze({...n,[o]:immutable(i,new Set,[...a,o])});let c=n[o];if(c!==void 0&&!(0,t.isPlainObject)(c)&&!Array.isArray(c))throw new e.ForgeConfigError(`Cannot select '${o}' because it is not an object or array.`);let l=c??{};return Object.freeze({...n,[o]:writeAtPath(l,s,i,[...a,o])})}function resetAtPath(e,n,r){let[i,...a]=r;if(typeof i==`number`){if(assertArrayIndex(i),!Array.isArray(e)||!Array.isArray(n)||i>=n.length||i>=e.length)return e;if(a.length===0){let t=[...e];return t[i]=n[i],Object.freeze(t)}let t=[...e];return t[i]=resetAtPath(e[i],n[i],a),Object.freeze(t)}if(!(0,t.isPlainObject)(e))return e;if(a.length===0){if(Object.hasOwn(n,i))return Object.freeze({...e,[i]:n[i]});let{[i]:t,...r}=e;return Object.freeze(r)}let o=n[i];if(!(0,t.isPlainObject)(o)&&!Array.isArray(o)){if(Object.hasOwn(n,i))return Object.freeze({...e,[i]:o});let{[i]:t,...r}=e;return Object.freeze(r)}let s=e[i],c=(0,t.isPlainObject)(s)||Array.isArray(s)?s:{};return Object.freeze({...e,[i]:resetAtPath(c,o,a)})}function pathEquals(e,t){if(e.length!==t.length)return!1;for(let n=0;n<e.length;n++)if(e[n]!==t[n])return!1;return!0}function findIssue(e,t){if(e){for(let n of e)if(pathEquals(n.path,t))return n.message}}exports.assertArrayIndex=assertArrayIndex,exports.assertSafeKey=assertSafeKey,exports.findIssue=findIssue,exports.formatValuePath=formatValuePath,exports.hasAtPath=hasAtPath,exports.immutable=immutable,Object.defineProperty(exports,"isRecord",{enumerable:!0,get:function(){return t.isPlainObject}}),exports.isUnsafeKey=isUnsafeKey,exports.pathEquals=pathEquals,exports.readAtPath=readAtPath,exports.resetAtPath=resetAtPath,exports.toPlainValues=toPlainValues,exports.writeAtPath=writeAtPath;
|
|
2
2
|
//# sourceMappingURL=path.cjs.map
|
package/dist/core/path.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"path.cjs","names":["ForgeConfigError","isRecord","require_errors","_vielzeug_arsenal","unsafeKeys","key","index","value","out","segment","path","date","time","ancestors","copy","entries","child","object","seen","current","rest","next","fullPath","childTree","baseline","_removed","baselineChild","currentChild","stringKey","meta","updated","i","a","b","issues","issue"],"sources":["../../src/core/path.ts"],"sourcesContent":["import { isPlainObject as isRecord } from '@vielzeug/arsenal';\n\nimport { ForgeConfigError } from '../errors.js';\nimport type { ValidationIssue } from '../types.js';\n\nconst unsafeKeys = new Set(['__proto__', 'constructor', 'prototype']);\n\nexport { isRecord };\n\nexport function isUnsafeKey(key: string): boolean {\n return !key || unsafeKeys.has(key);\n}\n\ntype RecordValue = Record<string, unknown>;\ntype TreeValue = RecordValue | readonly unknown[];\ntype MetaTree = true | MetaNode;\ninterface MetaNode {\n readonly [key: string]: MetaTree;\n}\nexport type MetaRoot = MetaNode;\n\nexport function assertSafeKey(key: string): void {\n if (isUnsafeKey(key)) throw new ForgeConfigError(`Invalid field key '${key}'.`);\n}\n\nexport function assertArrayIndex(index: number): void {\n if (!Number.isSafeInteger(index) || index < 0) throw new ForgeConfigError(`Invalid array index ${index}.`);\n}\n\nfunction tag(value: object): string {\n return Object.prototype.toString.call(value);\n}\n\nfunction isBlobLeaf(value: object): boolean {\n return tag(value) === '[object Blob]' || tag(value) === '[object File]';\n}\n\n/** Formats a value path as `list[0].name` for error messages. */\nexport function formatValuePath(path: readonly (string | number)[]): string {\n let out = '';\n for (const segment of path) {\n if (typeof segment === 'number') out += `[${segment}]`;\n else out += out ? `.${segment}` : segment;\n }\n return out;\n}\n\nfunction pathSuffix(path: readonly (string | number)[]): string {\n return path.length > 0 ? ` at '${formatValuePath(path)}'` : '';\n}\n\nfunction invalidValue(path: readonly (string | number)[]): never {\n throw new ForgeConfigError(\n `Form values must contain only finite JSON primitives, plain objects, arrays, Date, File, or Blob${pathSuffix(path)}.`,\n );\n}\n\nexport function immutable<T>(value: T, ancestors = new Set<object>(), path: (string | number)[] = []): T {\n if (value === null || value === undefined || typeof value === 'string' || typeof value === 'boolean') return value;\n if (typeof value === 'number') return Number.isFinite(value) ? value : invalidValue(path);\n if (typeof value !== 'object') return invalidValue(path);\n\n const date = value as unknown as Date;\n if (tag(value) === '[object Date]' && typeof date.getTime === 'function') {\n const time = date.getTime();\n if (!Number.isFinite(time)) return invalidValue(path);\n return Object.freeze(new Date(time)) as T;\n }\n\n if (isBlobLeaf(value)) return value;\n if (ancestors.has(value))\n throw new ForgeConfigError(`Form values must not contain circular references${pathSuffix(path)}.`);\n\n ancestors.add(value);\n try {\n if (Array.isArray(value)) {\n const copy: unknown[] = [];\n for (let index = 0; index < value.length; index++) {\n path.push(index);\n try {\n if (!Object.hasOwn(value, index))\n throw new ForgeConfigError(`Form arrays must not contain empty slots${pathSuffix(path)}.`);\n copy.push(immutable(value[index], ancestors, path));\n } finally {\n path.pop();\n }\n }\n return Object.freeze(copy) as T;\n }\n\n if (!isRecord(value)) return invalidValue(path);\n const entries = Object.entries(value).map(([key, child]) => {\n assertSafeKey(key);\n path.push(key);\n try {\n return [key, immutable(child, ancestors, path)] as const;\n } finally {\n path.pop();\n }\n });\n return Object.freeze(Object.fromEntries(entries)) as T;\n } finally {\n ancestors.delete(value);\n }\n}\n\n/**\n * Deeply converts a value into the plain shape `immutable()` accepts: primitives pass\n * through, `Date` is cloned, `File`/`Blob` keep identity, arrays map, and class instances\n * flatten to their own enumerable entries. Use as the `normalize` option of `createForm`\n * to hold domain-model classes in form state.\n *\n * Lossy conversions, applied instead of the error `immutable()` would throw: `Map`, `Set`,\n * and other keyless built-ins flatten to `{}` (their entries live on the prototype); sparse\n * array slots are dropped, which shifts later indexes; `NaN`, `Infinity`, functions, and\n * circular branches become `undefined`. Normalize model classes, not arbitrary graphs.\n */\nexport function toPlainValues<T>(value: T, seen = new WeakSet<object>()): T {\n if (value === null || value === undefined || typeof value === 'string' || typeof value === 'boolean') return value;\n if (typeof value === 'number') return Number.isFinite(value) ? value : (undefined as T);\n if (typeof value !== 'object') return undefined as T;\n\n const object = value as unknown as object;\n const date = value as unknown as Date;\n if (tag(object) === '[object Date]' && typeof date.getTime === 'function') {\n const time = date.getTime();\n return (Number.isFinite(time) ? new Date(time) : undefined) as T;\n }\n if (isBlobLeaf(object)) return value;\n if (seen.has(object)) return undefined as T;\n\n seen.add(object);\n try {\n if (Array.isArray(value)) {\n const copy: unknown[] = [];\n for (let index = 0; index < value.length; index++) {\n if (Object.hasOwn(value, index)) copy.push(toPlainValues(value[index], seen));\n }\n return copy as T;\n }\n\n // Plain objects and class instances both flatten to own enumerable entries;\n // unsafe keys are dropped so the result always passes immutable()'s key checks.\n const copy: RecordValue = {};\n for (const [key, child] of Object.entries(object)) {\n if (isUnsafeKey(key)) continue;\n copy[key] = toPlainValues(child, seen);\n }\n return copy as T;\n } finally {\n seen.delete(object);\n }\n}\n\nexport function readAtPath<T>(value: unknown, path: readonly (string | number)[]): T | undefined {\n let current: unknown = value;\n\n for (const key of path) {\n if (typeof key === 'number') {\n if (\n !Array.isArray(current) ||\n !Number.isSafeInteger(key) ||\n key < 0 ||\n key >= current.length ||\n !Object.hasOwn(current, key)\n )\n return undefined;\n\n current = current[key];\n } else {\n if (!isRecord(current) || !Object.hasOwn(current, key)) return undefined;\n\n current = current[key];\n }\n }\n\n return current as T | undefined;\n}\n\nexport function hasAtPath(value: unknown, path: readonly (string | number)[]): boolean {\n let current: unknown = value;\n\n for (const key of path) {\n if (typeof key === 'number') {\n if (\n !Array.isArray(current) ||\n !Number.isSafeInteger(key) ||\n key < 0 ||\n key >= current.length ||\n !Object.hasOwn(current, key)\n )\n return false;\n\n current = current[key];\n } else {\n if (!isRecord(current) || !Object.hasOwn(current, key)) return false;\n\n current = current[key];\n }\n }\n\n return true;\n}\n\nexport function writeAtPath<T extends TreeValue>(\n value: T,\n path: readonly (string | number)[],\n next: unknown,\n fullPath: readonly (string | number)[] = [],\n): T {\n const [key, ...rest] = path;\n\n if (typeof key === 'number') {\n assertArrayIndex(key);\n if (!Array.isArray(value)) {\n throw new ForgeConfigError(`Cannot index ${key} because the current value is not an array.`);\n }\n\n if (key < 0 || key >= value.length) {\n throw new ForgeConfigError(`Index ${key} is out of range for array of length ${value.length}.`);\n }\n\n if (rest.length === 0) {\n const copy = [...value];\n\n copy[key] = immutable(next, new Set<object>(), [...fullPath, key]);\n\n return Object.freeze(copy) as T;\n }\n\n const copy = [...value];\n\n copy[key] = writeAtPath(value[key] as TreeValue, rest, next, [...fullPath, key]);\n\n return Object.freeze(copy) as T;\n }\n\n assertSafeKey(key);\n\n if (!isRecord(value)) {\n throw new ForgeConfigError(`Cannot select '${key}' because the current value is not an object.`);\n }\n\n if (rest.length === 0)\n return Object.freeze({ ...value, [key]: immutable(next, new Set<object>(), [...fullPath, key]) }) as T;\n\n const child = value[key];\n\n if (child !== undefined && !isRecord(child) && !Array.isArray(child)) {\n throw new ForgeConfigError(`Cannot select '${key}' because it is not an object or array.`);\n }\n\n const childTree = (child ?? {}) as TreeValue;\n\n return Object.freeze({ ...value, [key]: writeAtPath(childTree, rest, next, [...fullPath, key]) }) as T;\n}\n\nexport function resetAtPath<T extends TreeValue>(value: T, baseline: T, path: readonly (string | number)[]): T {\n const [key, ...rest] = path;\n\n if (typeof key === 'number') {\n assertArrayIndex(key);\n if (!Array.isArray(value)) return value;\n if (!Array.isArray(baseline) || key >= baseline.length || key >= value.length) return value;\n\n if (rest.length === 0) {\n const copy = [...value];\n\n copy[key] = baseline[key];\n\n return Object.freeze(copy) as T;\n }\n\n const copy = [...value];\n\n copy[key] = resetAtPath(value[key] as TreeValue, baseline[key] as TreeValue, rest);\n\n return Object.freeze(copy) as T;\n }\n\n if (!isRecord(value)) return value;\n\n if (rest.length === 0) {\n if (Object.hasOwn(baseline, key)) return Object.freeze({ ...value, [key]: (baseline as RecordValue)[key] }) as T;\n\n const { [key]: _removed, ...next } = value;\n\n return Object.freeze(next) as T;\n }\n\n const baselineChild = (baseline as RecordValue)[key];\n\n if (!isRecord(baselineChild) && !Array.isArray(baselineChild)) {\n if (Object.hasOwn(baseline, key)) return Object.freeze({ ...value, [key]: baselineChild }) as T;\n\n const { [key]: _removed, ...next } = value;\n\n return Object.freeze(next) as T;\n }\n\n const currentChild = value[key];\n const childTree = (isRecord(currentChild) || Array.isArray(currentChild) ? currentChild : {}) as TreeValue;\n\n return Object.freeze({ ...value, [key]: resetAtPath(childTree, baselineChild as TreeValue, rest) }) as T;\n}\n\nexport function writeMeta(meta: MetaRoot, path: readonly (string | number)[], next: boolean): MetaRoot {\n const [key, ...rest] = path;\n if (typeof key === 'number') assertArrayIndex(key);\n const stringKey = String(key);\n const copy: Record<string, MetaTree> = { ...meta };\n\n if (rest.length === 0) {\n if (next) copy[stringKey] = true;\n else delete copy[stringKey];\n } else {\n const child = isRecord(meta[stringKey]) ? (meta[stringKey] as MetaRoot) : {};\n const updated = writeMeta(child, rest, next);\n\n if (Object.keys(updated).length === 0) delete copy[stringKey];\n else copy[stringKey] = updated;\n }\n\n return Object.freeze(copy);\n}\n\nexport function readMeta(meta: MetaRoot, path: readonly (string | number)[]): boolean {\n let current: MetaTree | undefined = meta;\n\n for (const key of path) {\n if (typeof key === 'number' && (!Number.isSafeInteger(key) || key < 0)) return false;\n const stringKey = String(key);\n\n if (!isRecord(current) || !Object.hasOwn(current, stringKey)) return false;\n\n current = current[stringKey] as MetaTree;\n }\n\n return current === true;\n}\n\nexport function touchAll(value: unknown): MetaRoot {\n if (Array.isArray(value)) {\n return Object.freeze(\n Object.fromEntries(\n value.map((child, i) => [\n String(i),\n isRecord(child) || Array.isArray(child) ? touchAll(child) : (true as const),\n ]),\n ),\n );\n }\n\n if (!isRecord(value)) return {};\n\n return Object.freeze(\n Object.fromEntries(\n Object.entries(value).map(([key, child]) => [\n key,\n isRecord(child) || Array.isArray(child) ? touchAll(child) : (true as const),\n ]),\n ),\n );\n}\n\nexport function pathEquals(a: readonly (string | number)[], b: readonly (string | number)[]): boolean {\n if (a.length !== b.length) return false;\n\n for (let i = 0; i < a.length; i++) {\n if (a[i] !== b[i]) return false;\n }\n\n return true;\n}\n\n/** Derives a single field's error message from the flat issue list. */\nexport function findIssue(\n issues: readonly ValidationIssue[] | undefined,\n path: readonly (string | number)[],\n): string | undefined {\n if (!issues) return undefined;\n\n for (const issue of issues) {\n if (pathEquals(issue.path, path)) return issue.message;\n }\n\n return undefined;\n}\n"],"mappings":"A,oEAKA,IAAMI,EAAa,IAAI,IAAI,CAAC,YAAa,cAAe,WAAW,CAAC,EAIpE,SAAgB,YAAY,EAAsB,CAChD,MAAO,CAACC,GAAOD,EAAW,IAAIC,CAAG,CACnC,CAUA,SAAgB,cAAc,EAAmB,CAC/C,GAAI,YAAYA,CAAG,EAAG,MAAM,IAAIL,EAAAA,iBAAiB,sBAAsBK,EAAI,GAAG,CAChF,CAEA,SAAgB,iBAAiB,EAAqB,CACpD,GAAI,CAAC,OAAO,cAAcC,CAAK,GAAKA,EAAQ,EAAG,MAAM,IAAIN,EAAAA,iBAAiB,uBAAuBM,EAAM,EAAE,CAC3G,CAEA,SAAS,IAAI,EAAuB,CAClC,OAAO,OAAO,UAAU,SAAS,KAAKC,CAAK,CAC7C,CAEA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AAQS,EACpB,CAEA,OAAOT,IAAY,EACrB,CAEA,SAAgB,SAAS,EAA0B,CAcjD,OAbI,MAAM,QAAQZ,CAAK,EACd,OAAO,OACZ,OAAO,YACLA,EAAM,KAAK,EAAO,IAAM,CACtB,OAAOwB,CAAC,GAAA,EACR9B,EAAAA,cAAAA,CAASe,CAAK,GAAK,MAAM,QAAQA,CAAK,EAAI,SAASA,CAAK,EAAK,EAC/D,CAAC,CACH,CACF,GAGE,EAACf,EAAAA,cAAAA,CAASM,CAAK,EAEZ,OAAO,OACZ,OAAO,YACL,OAAO,QAAQA,CAAK,CAAC,CAAC,KAAK,CAACF,EAAKW,KAAW,CAC1CX,GAAA,EACAJ,EAAAA,cAAAA,CAASe,CAAK,GAAK,MAAM,QAAQA,CAAK,EAAI,SAASA,CAAK,EAAK,EAC/D,CAAC,CACH,CACF,EAT6B,CAAC,CAUhC,CAEA,SAAgB,WAAW,EAAiC,EAA0C,CACpG,GAAIgB,EAAE,SAAWC,EAAE,OAAQ,MAAO,GAElC,IAAK,IAAIF,EAAI,EAAGA,EAAIC,EAAE,OAAQ,IAC5B,GAAIA,EAAED,KAAOE,EAAEF,GAAI,MAAO,GAG5B,MAAO,EACT,CAGA,SAAgB,UACd,EACA,EACoB,CACpB,GAAKG,EAEL,KAAK,IAAMC,KAASD,EAClB,GAAI,WAAWC,EAAM,KAAMzB,CAAI,EAAG,OAAOyB,EAAM,OAAA,CAInD,C"}
|
|
1
|
+
{"version":3,"file":"path.cjs","names":["ForgeConfigError","isRecord","require_errors","_vielzeug_arsenal","unsafeKeys","key","index","value","out","segment","path","date","time","ancestors","copy","entries","child","object","seen","notFound","current","found","rest","next","fullPath","childTree","baseline","_removed","baselineChild","currentChild","a","b","i","issues","issue"],"sources":["../../src/core/path.ts"],"sourcesContent":["import { isPlainObject as isRecord } from '@vielzeug/arsenal';\n\nimport { ForgeConfigError } from '../errors.js';\nimport type { ValidationIssue } from '../types.js';\n\nconst unsafeKeys = new Set(['__proto__', 'constructor', 'prototype']);\n\nexport { isRecord };\n\n/** Dots are reserved as path separators, so keys containing them are ambiguous. */\nexport function isUnsafeKey(key: string): boolean {\n return !key || key.includes('.') || unsafeKeys.has(key);\n}\n\ntype RecordValue = Record<string, unknown>;\ntype TreeValue = RecordValue | readonly unknown[];\n\nexport function assertSafeKey(key: string): void {\n if (isUnsafeKey(key)) throw new ForgeConfigError(`Invalid field key '${key}'.`);\n}\n\nexport function assertArrayIndex(index: number): void {\n if (!Number.isSafeInteger(index) || index < 0) throw new ForgeConfigError(`Invalid array index ${index}.`);\n}\n\nfunction tag(value: object): string {\n return Object.prototype.toString.call(value);\n}\n\nfunction isBlobLeaf(value: object): boolean {\n return tag(value) === '[object Blob]' || tag(value) === '[object File]';\n}\n\n/** Formats a value path as `list[0].name` for error messages. */\nexport function formatValuePath(path: readonly (string | number)[]): string {\n let out = '';\n for (const segment of path) {\n if (typeof segment === 'number') out += `[${segment}]`;\n else out += out ? `.${segment}` : segment;\n }\n return out;\n}\n\nfunction pathSuffix(path: readonly (string | number)[]): string {\n return path.length > 0 ? ` at '${formatValuePath(path)}'` : '';\n}\n\nfunction invalidValue(path: readonly (string | number)[]): never {\n throw new ForgeConfigError(\n `Form values must contain only finite JSON primitives, plain objects, arrays, Date, File, or Blob${pathSuffix(path)}.`,\n );\n}\n\nexport function immutable<T>(value: T, ancestors = new Set<object>(), path: (string | number)[] = []): T {\n if (value === null || value === undefined || typeof value === 'string' || typeof value === 'boolean') return value;\n if (typeof value === 'number') return Number.isFinite(value) ? value : invalidValue(path);\n if (typeof value !== 'object') return invalidValue(path);\n\n const date = value as unknown as Date;\n if (tag(value) === '[object Date]' && typeof date.getTime === 'function') {\n const time = date.getTime();\n if (!Number.isFinite(time)) return invalidValue(path);\n return Object.freeze(new Date(time)) as T;\n }\n\n if (isBlobLeaf(value)) return value;\n if (ancestors.has(value))\n throw new ForgeConfigError(`Form values must not contain circular references${pathSuffix(path)}.`);\n\n ancestors.add(value);\n try {\n if (Array.isArray(value)) {\n const copy: unknown[] = [];\n for (let index = 0; index < value.length; index++) {\n path.push(index);\n try {\n if (!Object.hasOwn(value, index))\n throw new ForgeConfigError(`Form arrays must not contain empty slots${pathSuffix(path)}.`);\n copy.push(immutable(value[index], ancestors, path));\n } finally {\n path.pop();\n }\n }\n return Object.freeze(copy) as T;\n }\n\n if (!isRecord(value)) return invalidValue(path);\n const entries = Object.entries(value).map(([key, child]) => {\n assertSafeKey(key);\n path.push(key);\n try {\n return [key, immutable(child, ancestors, path)] as const;\n } finally {\n path.pop();\n }\n });\n return Object.freeze(Object.fromEntries(entries)) as T;\n } finally {\n ancestors.delete(value);\n }\n}\n\n/**\n * Deeply converts a value into the plain shape `immutable()` accepts: primitives pass\n * through, `Date` is cloned, `File`/`Blob` keep identity, arrays map, and class instances\n * flatten to their own enumerable entries. Use as the `normalize` option of `createForm`\n * to hold domain-model classes in form state.\n *\n * Lossy conversions, applied instead of the error `immutable()` would throw: `Map`, `Set`,\n * and other keyless built-ins flatten to `{}` (their entries live on the prototype); sparse\n * array slots are dropped, which shifts later indexes; `NaN`, `Infinity`, functions, and\n * circular branches become `undefined`. Normalize model classes, not arbitrary graphs.\n */\nexport function toPlainValues<T>(value: T, seen = new WeakSet<object>()): T {\n if (value === null || value === undefined || typeof value === 'string' || typeof value === 'boolean') return value;\n if (typeof value === 'number') return Number.isFinite(value) ? value : (undefined as T);\n if (typeof value !== 'object') return undefined as T;\n\n const object = value as unknown as object;\n const date = value as unknown as Date;\n if (tag(object) === '[object Date]' && typeof date.getTime === 'function') {\n const time = date.getTime();\n return (Number.isFinite(time) ? new Date(time) : undefined) as T;\n }\n if (isBlobLeaf(object)) return value;\n if (seen.has(object)) return undefined as T;\n\n seen.add(object);\n try {\n if (Array.isArray(value)) {\n const copy: unknown[] = [];\n for (let index = 0; index < value.length; index++) {\n if (Object.hasOwn(value, index)) copy.push(toPlainValues(value[index], seen));\n }\n return copy as T;\n }\n\n // Plain objects and class instances both flatten to own enumerable entries;\n // unsafe keys are dropped so the result always passes immutable()'s key checks.\n const copy: RecordValue = {};\n for (const [key, child] of Object.entries(object)) {\n if (isUnsafeKey(key)) continue;\n copy[key] = toPlainValues(child, seen);\n }\n return copy as T;\n } finally {\n seen.delete(object);\n }\n}\n\nconst notFound = Symbol('forge-path-not-found');\n\nfunction walk(value: unknown, path: readonly (string | number)[]): unknown | typeof notFound {\n let current: unknown = value;\n\n for (const key of path) {\n if (typeof key === 'number') {\n if (\n !Array.isArray(current) ||\n !Number.isSafeInteger(key) ||\n key < 0 ||\n key >= current.length ||\n !Object.hasOwn(current, key)\n )\n return notFound;\n\n current = current[key];\n } else {\n if (!isRecord(current) || !Object.hasOwn(current, key)) return notFound;\n\n current = current[key];\n }\n }\n\n return current;\n}\n\nexport function readAtPath<T>(value: unknown, path: readonly (string | number)[]): T | undefined {\n const found = walk(value, path);\n\n return found === notFound ? undefined : (found as T);\n}\n\nexport function hasAtPath(value: unknown, path: readonly (string | number)[]): boolean {\n return walk(value, path) !== notFound;\n}\n\nexport function writeAtPath<T extends TreeValue>(\n value: T,\n path: readonly (string | number)[],\n next: unknown,\n fullPath: readonly (string | number)[] = [],\n): T {\n const [key, ...rest] = path;\n\n if (typeof key === 'number') {\n assertArrayIndex(key);\n if (!Array.isArray(value)) {\n throw new ForgeConfigError(`Cannot index ${key} because the current value is not an array.`);\n }\n\n if (key < 0 || key >= value.length) {\n throw new ForgeConfigError(`Index ${key} is out of range for array of length ${value.length}.`);\n }\n\n if (rest.length === 0) {\n const copy = [...value];\n\n copy[key] = immutable(next, new Set<object>(), [...fullPath, key]);\n\n return Object.freeze(copy) as T;\n }\n\n const copy = [...value];\n\n copy[key] = writeAtPath(value[key] as TreeValue, rest, next, [...fullPath, key]);\n\n return Object.freeze(copy) as T;\n }\n\n assertSafeKey(key);\n\n if (!isRecord(value)) {\n throw new ForgeConfigError(`Cannot select '${key}' because the current value is not an object.`);\n }\n\n if (rest.length === 0)\n return Object.freeze({ ...value, [key]: immutable(next, new Set<object>(), [...fullPath, key]) }) as T;\n\n const child = value[key];\n\n if (child !== undefined && !isRecord(child) && !Array.isArray(child)) {\n throw new ForgeConfigError(`Cannot select '${key}' because it is not an object or array.`);\n }\n\n const childTree = (child ?? {}) as TreeValue;\n\n return Object.freeze({ ...value, [key]: writeAtPath(childTree, rest, next, [...fullPath, key]) }) as T;\n}\n\nexport function resetAtPath<T extends TreeValue>(value: T, baseline: T, path: readonly (string | number)[]): T {\n const [key, ...rest] = path;\n\n if (typeof key === 'number') {\n assertArrayIndex(key);\n if (!Array.isArray(value)) return value;\n if (!Array.isArray(baseline) || key >= baseline.length || key >= value.length) return value;\n\n if (rest.length === 0) {\n const copy = [...value];\n\n copy[key] = baseline[key];\n\n return Object.freeze(copy) as T;\n }\n\n const copy = [...value];\n\n copy[key] = resetAtPath(value[key] as TreeValue, baseline[key] as TreeValue, rest);\n\n return Object.freeze(copy) as T;\n }\n\n if (!isRecord(value)) return value;\n\n if (rest.length === 0) {\n if (Object.hasOwn(baseline, key)) return Object.freeze({ ...value, [key]: (baseline as RecordValue)[key] }) as T;\n\n const { [key]: _removed, ...next } = value;\n\n return Object.freeze(next) as T;\n }\n\n const baselineChild = (baseline as RecordValue)[key];\n\n if (!isRecord(baselineChild) && !Array.isArray(baselineChild)) {\n if (Object.hasOwn(baseline, key)) return Object.freeze({ ...value, [key]: baselineChild }) as T;\n\n const { [key]: _removed, ...next } = value;\n\n return Object.freeze(next) as T;\n }\n\n const currentChild = value[key];\n const childTree = (isRecord(currentChild) || Array.isArray(currentChild) ? currentChild : {}) as TreeValue;\n\n return Object.freeze({ ...value, [key]: resetAtPath(childTree, baselineChild as TreeValue, rest) }) as T;\n}\n\nexport function pathEquals(a: readonly (string | number)[], b: readonly (string | number)[]): boolean {\n if (a.length !== b.length) return false;\n\n for (let i = 0; i < a.length; i++) {\n if (a[i] !== b[i]) return false;\n }\n\n return true;\n}\n\n/** Derives a single field's error message from the flat issue list. */\nexport function findIssue(\n issues: readonly ValidationIssue[] | undefined,\n path: readonly (string | number)[],\n): string | undefined {\n if (!issues) return undefined;\n\n for (const issue of issues) {\n if (pathEquals(issue.path, path)) return issue.message;\n }\n\n return undefined;\n}\n"],"mappings":"A,oEAKA,IAAMI,EAAa,IAAI,IAAI,CAAC,YAAa,cAAe,WAAW,CAAC,EAKpE,SAAgB,YAAY,EAAsB,CAChD,MAAO,CAACC,GAAOA,EAAI,SAAS,GAAG,GAAKD,EAAW,IAAIC,CAAG,CACxD,CAKA,SAAgB,cAAc,EAAmB,CAC/C,GAAI,YAAYA,CAAG,EAAG,MAAM,IAAIL,EAAAA,iBAAiB,sBAAsBK,EAAI,GAAG,CAChF,CAEA,SAAgB,iBAAiB,EAAqB,CACpD,GAAI,CAAC,OAAO,cAAcC,CAAK,GAAKA,EAAQ,EAAG,MAAM,IAAIN,EAAAA,iBAAiB,uBAAuBM,EAAM,EAAE,CAC3G,CAEA,SAAS,IAAI,EAAuB,CAClC,OAAO,OAAO,UAAU,SAAS,KAAKC,CAAK,CAC7C,CAEA,SAAS,WAAW,EAAwB,CAC1C,OAAO,IAAIA,CAAK,IAAM,iBAAmB,IAAIA,CAAK,IAAM,eAC1D,CAGA,SAAgB,gBAAgB,EAA4C,CAC1E,IAAIC,EAAM,GACV,IAAK,IAAMC,KAAWC,EACpB,AACKF,GADD,OAAOC,GAAY,SAAiB,IAAIA,EAAQ,GACxCD,EAAM,IAAIC,IAAYA,EAEpC,OAAOD,CACT,CAEA,SAAS,WAAW,EAA4C,CAC9D,OAAOE,EAAK,OAAS,EAAI,QAAQ,gBAAgBA,CAAI,EAAE,GAAK,EAC9D,CAEA,SAAS,aAAa,EAA2C,CAC/D,MAAM,IAAIV,EAAAA,iBACR,mGAAmG,WAAWU,CAAI,EAAE,EACtH,CACF,CAEA,SAAgB,UAAa,EAAU,EAAY,IAAI,IAAe,EAA4B,CAAC,EAAM,CACvG,GAAIH,GAAU,MAA+B,OAAOA,GAAU,UAAY,OAAOA,GAAU,UAAW,OAAOA,EAC7G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
|
|
|
1
1
|
import { isPlainObject as isRecord } from '@vielzeug/arsenal';
|
|
2
2
|
import type { ValidationIssue } from '../types.js';
|
|
3
3
|
export { isRecord };
|
|
4
|
+
/** Dots are reserved as path separators, so keys containing them are ambiguous. */
|
|
4
5
|
export declare function isUnsafeKey(key: string): boolean;
|
|
5
6
|
type RecordValue = Record<string, unknown>;
|
|
6
7
|
type TreeValue = RecordValue | readonly unknown[];
|
|
7
|
-
type MetaTree = true | MetaNode;
|
|
8
|
-
interface MetaNode {
|
|
9
|
-
readonly [key: string]: MetaTree;
|
|
10
|
-
}
|
|
11
|
-
export type MetaRoot = MetaNode;
|
|
12
8
|
export declare function assertSafeKey(key: string): void;
|
|
13
9
|
export declare function assertArrayIndex(index: number): void;
|
|
14
10
|
/** Formats a value path as `list[0].name` for error messages. */
|
|
@@ -30,9 +26,6 @@ export declare function readAtPath<T>(value: unknown, path: readonly (string | n
|
|
|
30
26
|
export declare function hasAtPath(value: unknown, path: readonly (string | number)[]): boolean;
|
|
31
27
|
export declare function writeAtPath<T extends TreeValue>(value: T, path: readonly (string | number)[], next: unknown, fullPath?: readonly (string | number)[]): T;
|
|
32
28
|
export declare function resetAtPath<T extends TreeValue>(value: T, baseline: T, path: readonly (string | number)[]): T;
|
|
33
|
-
export declare function writeMeta(meta: MetaRoot, path: readonly (string | number)[], next: boolean): MetaRoot;
|
|
34
|
-
export declare function readMeta(meta: MetaRoot, path: readonly (string | number)[]): boolean;
|
|
35
|
-
export declare function touchAll(value: unknown): MetaRoot;
|
|
36
29
|
export declare function pathEquals(a: readonly (string | number)[], b: readonly (string | number)[]): boolean;
|
|
37
30
|
/** Derives a single field's error message from the flat issue list. */
|
|
38
31
|
export declare function findIssue(issues: readonly ValidationIssue[] | undefined, path: readonly (string | number)[]): string | undefined;
|
package/dist/core/path.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"path.d.ts","sourceRoot":"","sources":["../../src/core/path.ts"],"names":[],"mappings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.has(e);
|
|
10
|
+
return !e || e.includes(".") || n.has(e);
|
|
11
11
|
}
|
|
12
12
|
function assertSafeKey(t) {
|
|
13
13
|
if (isUnsafeKey(t)) throw new e(`Invalid field key '${t}'.`);
|
|
@@ -98,27 +98,24 @@ function toPlainValues(e, t = /* @__PURE__ */ new WeakSet()) {
|
|
|
98
98
|
}
|
|
99
99
|
}
|
|
100
100
|
}
|
|
101
|
-
|
|
102
|
-
|
|
101
|
+
var r = Symbol("forge-path-not-found");
|
|
102
|
+
function walk(e, n) {
|
|
103
|
+
let i = e;
|
|
103
104
|
for (let e of n) if (typeof e == "number") {
|
|
104
|
-
if (!Array.isArray(
|
|
105
|
-
|
|
105
|
+
if (!Array.isArray(i) || !Number.isSafeInteger(e) || e < 0 || e >= i.length || !Object.hasOwn(i, e)) return r;
|
|
106
|
+
i = i[e];
|
|
106
107
|
} else {
|
|
107
|
-
if (!t(
|
|
108
|
-
|
|
108
|
+
if (!t(i) || !Object.hasOwn(i, e)) return r;
|
|
109
|
+
i = i[e];
|
|
109
110
|
}
|
|
110
|
-
return
|
|
111
|
+
return i;
|
|
111
112
|
}
|
|
112
|
-
function
|
|
113
|
-
let
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
if (!t(r) || !Object.hasOwn(r, e)) return !1;
|
|
119
|
-
r = r[e];
|
|
120
|
-
}
|
|
121
|
-
return !0;
|
|
113
|
+
function readAtPath(e, t) {
|
|
114
|
+
let n = walk(e, t);
|
|
115
|
+
return n === r ? void 0 : n;
|
|
116
|
+
}
|
|
117
|
+
function hasAtPath(e, t) {
|
|
118
|
+
return walk(e, t) !== r;
|
|
122
119
|
}
|
|
123
120
|
function writeAtPath(n, r, i, a = []) {
|
|
124
121
|
let [o, ...s] = r;
|
|
@@ -180,30 +177,6 @@ function resetAtPath(e, n, r) {
|
|
|
180
177
|
[i]: resetAtPath(c, o, a)
|
|
181
178
|
});
|
|
182
179
|
}
|
|
183
|
-
function writeMeta(e, n, r) {
|
|
184
|
-
let [i, ...a] = n;
|
|
185
|
-
typeof i == "number" && assertArrayIndex(i);
|
|
186
|
-
let o = String(i), s = { ...e };
|
|
187
|
-
if (a.length === 0) r ? s[o] = !0 : delete s[o];
|
|
188
|
-
else {
|
|
189
|
-
let n = writeMeta(t(e[o]) ? e[o] : {}, a, r);
|
|
190
|
-
Object.keys(n).length === 0 ? delete s[o] : s[o] = n;
|
|
191
|
-
}
|
|
192
|
-
return Object.freeze(s);
|
|
193
|
-
}
|
|
194
|
-
function readMeta(e, n) {
|
|
195
|
-
let r = e;
|
|
196
|
-
for (let e of n) {
|
|
197
|
-
if (typeof e == "number" && (!Number.isSafeInteger(e) || e < 0)) return !1;
|
|
198
|
-
let n = String(e);
|
|
199
|
-
if (!t(r) || !Object.hasOwn(r, n)) return !1;
|
|
200
|
-
r = r[n];
|
|
201
|
-
}
|
|
202
|
-
return r === !0;
|
|
203
|
-
}
|
|
204
|
-
function touchAll(e) {
|
|
205
|
-
return Array.isArray(e) ? Object.freeze(Object.fromEntries(e.map((e, n) => [String(n), t(e) || Array.isArray(e) ? touchAll(e) : !0]))) : t(e) ? Object.freeze(Object.fromEntries(Object.entries(e).map(([e, n]) => [e, t(n) || Array.isArray(n) ? touchAll(n) : !0]))) : {};
|
|
206
|
-
}
|
|
207
180
|
function pathEquals(e, t) {
|
|
208
181
|
if (e.length !== t.length) return !1;
|
|
209
182
|
for (let n = 0; n < e.length; n++) if (e[n] !== t[n]) return !1;
|
|
@@ -214,6 +187,6 @@ function findIssue(e, t) {
|
|
|
214
187
|
for (let n of e) if (pathEquals(n.path, t)) return n.message;
|
|
215
188
|
}
|
|
216
189
|
}
|
|
217
|
-
export { assertArrayIndex, assertSafeKey, findIssue, formatValuePath, hasAtPath, immutable, t as isRecord, isUnsafeKey, pathEquals, readAtPath,
|
|
190
|
+
export { assertArrayIndex, assertSafeKey, findIssue, formatValuePath, hasAtPath, immutable, t as isRecord, isUnsafeKey, pathEquals, readAtPath, resetAtPath, toPlainValues, writeAtPath };
|
|
218
191
|
|
|
219
192
|
//# sourceMappingURL=path.js.map
|
package/dist/core/path.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"path.js","names":["isRecord","unsafeKeys","key","ForgeConfigError","index","value","out","segment","path","date","time","ancestors","copy","entries","child","object","seen","current","rest","next","fullPath","childTree","baseline","_removed","baselineChild","currentChild","stringKey","meta","updated","i","a","b","issues","issue"],"sources":["../../src/core/path.ts"],"sourcesContent":["import { isPlainObject as isRecord } from '@vielzeug/arsenal';\n\nimport { ForgeConfigError } from '../errors.js';\nimport type { ValidationIssue } from '../types.js';\n\nconst unsafeKeys = new Set(['__proto__', 'constructor', 'prototype']);\n\nexport { isRecord };\n\nexport function isUnsafeKey(key: string): boolean {\n return !key || unsafeKeys.has(key);\n}\n\ntype RecordValue = Record<string, unknown>;\ntype TreeValue = RecordValue | readonly unknown[];\ntype MetaTree = true | MetaNode;\ninterface MetaNode {\n readonly [key: string]: MetaTree;\n}\nexport type MetaRoot = MetaNode;\n\nexport function assertSafeKey(key: string): void {\n if (isUnsafeKey(key)) throw new ForgeConfigError(`Invalid field key '${key}'.`);\n}\n\nexport function assertArrayIndex(index: number): void {\n if (!Number.isSafeInteger(index) || index < 0) throw new ForgeConfigError(`Invalid array index ${index}.`);\n}\n\nfunction tag(value: object): string {\n return Object.prototype.toString.call(value);\n}\n\nfunction isBlobLeaf(value: object): boolean {\n return tag(value) === '[object Blob]' || tag(value) === '[object File]';\n}\n\n/** Formats a value path as `list[0].name` for error messages. */\nexport function formatValuePath(path: readonly (string | number)[]): string {\n let out = '';\n for (const segment of path) {\n if (typeof segment === 'number') out += `[${segment}]`;\n else out += out ? `.${segment}` : segment;\n }\n return out;\n}\n\nfunction pathSuffix(path: readonly (string | number)[]): string {\n return path.length > 0 ? ` at '${formatValuePath(path)}'` : '';\n}\n\nfunction invalidValue(path: readonly (string | number)[]): never {\n throw new ForgeConfigError(\n `Form values must contain only finite JSON primitives, plain objects, arrays, Date, File, or Blob${pathSuffix(path)}.`,\n );\n}\n\nexport function immutable<T>(value: T, ancestors = new Set<object>(), path: (string | number)[] = []): T {\n if (value === null || value === undefined || typeof value === 'string' || typeof value === 'boolean') return value;\n if (typeof value === 'number') return Number.isFinite(value) ? value : invalidValue(path);\n if (typeof value !== 'object') return invalidValue(path);\n\n const date = value as unknown as Date;\n if (tag(value) === '[object Date]' && typeof date.getTime === 'function') {\n const time = date.getTime();\n if (!Number.isFinite(time)) return invalidValue(path);\n return Object.freeze(new Date(time)) as T;\n }\n\n if (isBlobLeaf(value)) return value;\n if (ancestors.has(value))\n throw new ForgeConfigError(`Form values must not contain circular references${pathSuffix(path)}.`);\n\n ancestors.add(value);\n try {\n if (Array.isArray(value)) {\n const copy: unknown[] = [];\n for (let index = 0; index < value.length; index++) {\n path.push(index);\n try {\n if (!Object.hasOwn(value, index))\n throw new ForgeConfigError(`Form arrays must not contain empty slots${pathSuffix(path)}.`);\n copy.push(immutable(value[index], ancestors, path));\n } finally {\n path.pop();\n }\n }\n return Object.freeze(copy) as T;\n }\n\n if (!isRecord(value)) return invalidValue(path);\n const entries = Object.entries(value).map(([key, child]) => {\n assertSafeKey(key);\n path.push(key);\n try {\n return [key, immutable(child, ancestors, path)] as const;\n } finally {\n path.pop();\n }\n });\n return Object.freeze(Object.fromEntries(entries)) as T;\n } finally {\n ancestors.delete(value);\n }\n}\n\n/**\n * Deeply converts a value into the plain shape `immutable()` accepts: primitives pass\n * through, `Date` is cloned, `File`/`Blob` keep identity, arrays map, and class instances\n * flatten to their own enumerable entries. Use as the `normalize` option of `createForm`\n * to hold domain-model classes in form state.\n *\n * Lossy conversions, applied instead of the error `immutable()` would throw: `Map`, `Set`,\n * and other keyless built-ins flatten to `{}` (their entries live on the prototype); sparse\n * array slots are dropped, which shifts later indexes; `NaN`, `Infinity`, functions, and\n * circular branches become `undefined`. Normalize model classes, not arbitrary graphs.\n */\nexport function toPlainValues<T>(value: T, seen = new WeakSet<object>()): T {\n if (value === null || value === undefined || typeof value === 'string' || typeof value === 'boolean') return value;\n if (typeof value === 'number') return Number.isFinite(value) ? value : (undefined as T);\n if (typeof value !== 'object') return undefined as T;\n\n const object = value as unknown as object;\n const date = value as unknown as Date;\n if (tag(object) === '[object Date]' && typeof date.getTime === 'function') {\n const time = date.getTime();\n return (Number.isFinite(time) ? new Date(time) : undefined) as T;\n }\n if (isBlobLeaf(object)) return value;\n if (seen.has(object)) return undefined as T;\n\n seen.add(object);\n try {\n if (Array.isArray(value)) {\n const copy: unknown[] = [];\n for (let index = 0; index < value.length; index++) {\n if (Object.hasOwn(value, index)) copy.push(toPlainValues(value[index], seen));\n }\n return copy as T;\n }\n\n // Plain objects and class instances both flatten to own enumerable entries;\n // unsafe keys are dropped so the result always passes immutable()'s key checks.\n const copy: RecordValue = {};\n for (const [key, child] of Object.entries(object)) {\n if (isUnsafeKey(key)) continue;\n copy[key] = toPlainValues(child, seen);\n }\n return copy as T;\n } finally {\n seen.delete(object);\n }\n}\n\nexport function readAtPath<T>(value: unknown, path: readonly (string | number)[]): T | undefined {\n let current: unknown = value;\n\n for (const key of path) {\n if (typeof key === 'number') {\n if (\n !Array.isArray(current) ||\n !Number.isSafeInteger(key) ||\n key < 0 ||\n key >= current.length ||\n !Object.hasOwn(current, key)\n )\n return undefined;\n\n current = current[key];\n } else {\n if (!isRecord(current) || !Object.hasOwn(current, key)) return undefined;\n\n current = current[key];\n }\n }\n\n return current as T | undefined;\n}\n\nexport function hasAtPath(value: unknown, path: readonly (string | number)[]): boolean {\n let current: unknown = value;\n\n for (const key of path) {\n if (typeof key === 'number') {\n if (\n !Array.isArray(current) ||\n !Number.isSafeInteger(key) ||\n key < 0 ||\n key >= current.length ||\n !Object.hasOwn(current, key)\n )\n return false;\n\n current = current[key];\n } else {\n if (!isRecord(current) || !Object.hasOwn(current, key)) return false;\n\n current = current[key];\n }\n }\n\n return true;\n}\n\nexport function writeAtPath<T extends TreeValue>(\n value: T,\n path: readonly (string | number)[],\n next: unknown,\n fullPath: readonly (string | number)[] = [],\n): T {\n const [key, ...rest] = path;\n\n if (typeof key === 'number') {\n assertArrayIndex(key);\n if (!Array.isArray(value)) {\n throw new ForgeConfigError(`Cannot index ${key} because the current value is not an array.`);\n }\n\n if (key < 0 || key >= value.length) {\n throw new ForgeConfigError(`Index ${key} is out of range for array of length ${value.length}.`);\n }\n\n if (rest.length === 0) {\n const copy = [...value];\n\n copy[key] = immutable(next, new Set<object>(), [...fullPath, key]);\n\n return Object.freeze(copy) as T;\n }\n\n const copy = [...value];\n\n copy[key] = writeAtPath(value[key] as TreeValue, rest, next, [...fullPath, key]);\n\n return Object.freeze(copy) as T;\n }\n\n assertSafeKey(key);\n\n if (!isRecord(value)) {\n throw new ForgeConfigError(`Cannot select '${key}' because the current value is not an object.`);\n }\n\n if (rest.length === 0)\n return Object.freeze({ ...value, [key]: immutable(next, new Set<object>(), [...fullPath, key]) }) as T;\n\n const child = value[key];\n\n if (child !== undefined && !isRecord(child) && !Array.isArray(child)) {\n throw new ForgeConfigError(`Cannot select '${key}' because it is not an object or array.`);\n }\n\n const childTree = (child ?? {}) as TreeValue;\n\n return Object.freeze({ ...value, [key]: writeAtPath(childTree, rest, next, [...fullPath, key]) }) as T;\n}\n\nexport function resetAtPath<T extends TreeValue>(value: T, baseline: T, path: readonly (string | number)[]): T {\n const [key, ...rest] = path;\n\n if (typeof key === 'number') {\n assertArrayIndex(key);\n if (!Array.isArray(value)) return value;\n if (!Array.isArray(baseline) || key >= baseline.length || key >= value.length) return value;\n\n if (rest.length === 0) {\n const copy = [...value];\n\n copy[key] = baseline[key];\n\n return Object.freeze(copy) as T;\n }\n\n const copy = [...value];\n\n copy[key] = resetAtPath(value[key] as TreeValue, baseline[key] as TreeValue, rest);\n\n return Object.freeze(copy) as T;\n }\n\n if (!isRecord(value)) return value;\n\n if (rest.length === 0) {\n if (Object.hasOwn(baseline, key)) return Object.freeze({ ...value, [key]: (baseline as RecordValue)[key] }) as T;\n\n const { [key]: _removed, ...next } = value;\n\n return Object.freeze(next) as T;\n }\n\n const baselineChild = (baseline as RecordValue)[key];\n\n if (!isRecord(baselineChild) && !Array.isArray(baselineChild)) {\n if (Object.hasOwn(baseline, key)) return Object.freeze({ ...value, [key]: baselineChild }) as T;\n\n const { [key]: _removed, ...next } = value;\n\n return Object.freeze(next) as T;\n }\n\n const currentChild = value[key];\n const childTree = (isRecord(currentChild) || Array.isArray(currentChild) ? currentChild : {}) as TreeValue;\n\n return Object.freeze({ ...value, [key]: resetAtPath(childTree, baselineChild as TreeValue, rest) }) as T;\n}\n\nexport function writeMeta(meta: MetaRoot, path: readonly (string | number)[], next: boolean): MetaRoot {\n const [key, ...rest] = path;\n if (typeof key === 'number') assertArrayIndex(key);\n const stringKey = String(key);\n const copy: Record<string, MetaTree> = { ...meta };\n\n if (rest.length === 0) {\n if (next) copy[stringKey] = true;\n else delete copy[stringKey];\n } else {\n const child = isRecord(meta[stringKey]) ? (meta[stringKey] as MetaRoot) : {};\n const updated = writeMeta(child, rest, next);\n\n if (Object.keys(updated).length === 0) delete copy[stringKey];\n else copy[stringKey] = updated;\n }\n\n return Object.freeze(copy);\n}\n\nexport function readMeta(meta: MetaRoot, path: readonly (string | number)[]): boolean {\n let current: MetaTree | undefined = meta;\n\n for (const key of path) {\n if (typeof key === 'number' && (!Number.isSafeInteger(key) || key < 0)) return false;\n const stringKey = String(key);\n\n if (!isRecord(current) || !Object.hasOwn(current, stringKey)) return false;\n\n current = current[stringKey] as MetaTree;\n }\n\n return current === true;\n}\n\nexport function touchAll(value: unknown): MetaRoot {\n if (Array.isArray(value)) {\n return Object.freeze(\n Object.fromEntries(\n value.map((child, i) => [\n String(i),\n isRecord(child) || Array.isArray(child) ? touchAll(child) : (true as const),\n ]),\n ),\n );\n }\n\n if (!isRecord(value)) return {};\n\n return Object.freeze(\n Object.fromEntries(\n Object.entries(value).map(([key, child]) => [\n key,\n isRecord(child) || Array.isArray(child) ? touchAll(child) : (true as const),\n ]),\n ),\n );\n}\n\nexport function pathEquals(a: readonly (string | number)[], b: readonly (string | number)[]): boolean {\n if (a.length !== b.length) return false;\n\n for (let i = 0; i < a.length; i++) {\n if (a[i] !== b[i]) return false;\n }\n\n return true;\n}\n\n/** Derives a single field's error message from the flat issue list. */\nexport function findIssue(\n issues: readonly ValidationIssue[] | undefined,\n path: readonly (string | number)[],\n): string | undefined {\n if (!issues) return undefined;\n\n for (const issue of issues) {\n if (pathEquals(issue.path, path)) return issue.message;\n }\n\n return undefined;\n}\n"],"mappings":";;;AAKA,IAAMC,oBAAa,IAAI,IAAI;CAAC;CAAa;CAAe;AAAW,CAAC;AAIpE,SAAgB,YAAY,GAAsB;CAChD,OAAO,CAACC,KAAOD,EAAW,IAAIC,CAAG;AACnC;AAUA,SAAgB,cAAc,GAAmB;CAC/C,IAAI,YAAYA,CAAG,GAAG,MAAM,IAAIC,EAAiB,sBAAsBD,EAAI,GAAG;AAChF;AAEA,SAAgB,iBAAiB,GAAqB;CACpD,IAAI,CAAC,OAAO,cAAcE,CAAK,KAAKA,IAAQ,GAAG,MAAM,IAAID,EAAiB,uBAAuBC,EAAM,EAAE;AAC3G;AAEA,SAAS,IAAI,GAAuB;CAClC,OAAO,OAAO,UAAU,SAAS,KAAKC,CAAK;AAC7C;AAEA,SAAS,WAAW,GAAwB;CAC1C,OAAO,IAAIA,CAAK,MAAM,mBAAmB,IAAIA,CAAK,MAAM;AAC1D;AAGA,SAAgB,gBAAgB,GAA4C;CAC1E,IAAIC,IAAM;CACV,KAAK,IAAMC,KAAWC,GACpB,AACKF,KADD,OAAOC,KAAY,WAAiB,IAAIA,EAAQ,KACxCD,IAAM,IAAIC,MAAYA;CAEpC,OAAOD;AACT;AAEA,SAAS,WAAW,GAA4C;CAC9D,OAAOE,EAAK,SAAS,IAAI,QAAQ,gBAAgBA,CAAI,EAAE,KAAK;AAC9D;AAEA,SAAS,aAAa,GAA2C;CAC/D,MAAM,IAAIL,EACR,mGAAmG,WAAWK,CAAI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AOE,EAAEF,IAAI,OAAO;CAG5B,OAAO;AACT;AAGA,SAAgB,UACd,GACA,GACoB;CACpB,IAAKG,GAEL;OAAK,IAAMC,KAASD,GAClB,IAAI,WAAWC,EAAM,MAAMzB,CAAI,GAAG,OAAOyB,EAAM;CAAA;AAInD"}
|
|
1
|
+
{"version":3,"file":"path.js","names":["isRecord","unsafeKeys","key","ForgeConfigError","index","value","out","segment","path","date","time","ancestors","copy","entries","child","object","seen","notFound","current","found","rest","next","fullPath","childTree","baseline","_removed","baselineChild","currentChild","a","b","i","issues","issue"],"sources":["../../src/core/path.ts"],"sourcesContent":["import { isPlainObject as isRecord } from '@vielzeug/arsenal';\n\nimport { ForgeConfigError } from '../errors.js';\nimport type { ValidationIssue } from '../types.js';\n\nconst unsafeKeys = new Set(['__proto__', 'constructor', 'prototype']);\n\nexport { isRecord };\n\n/** Dots are reserved as path separators, so keys containing them are ambiguous. */\nexport function isUnsafeKey(key: string): boolean {\n return !key || key.includes('.') || unsafeKeys.has(key);\n}\n\ntype RecordValue = Record<string, unknown>;\ntype TreeValue = RecordValue | readonly unknown[];\n\nexport function assertSafeKey(key: string): void {\n if (isUnsafeKey(key)) throw new ForgeConfigError(`Invalid field key '${key}'.`);\n}\n\nexport function assertArrayIndex(index: number): void {\n if (!Number.isSafeInteger(index) || index < 0) throw new ForgeConfigError(`Invalid array index ${index}.`);\n}\n\nfunction tag(value: object): string {\n return Object.prototype.toString.call(value);\n}\n\nfunction isBlobLeaf(value: object): boolean {\n return tag(value) === '[object Blob]' || tag(value) === '[object File]';\n}\n\n/** Formats a value path as `list[0].name` for error messages. */\nexport function formatValuePath(path: readonly (string | number)[]): string {\n let out = '';\n for (const segment of path) {\n if (typeof segment === 'number') out += `[${segment}]`;\n else out += out ? `.${segment}` : segment;\n }\n return out;\n}\n\nfunction pathSuffix(path: readonly (string | number)[]): string {\n return path.length > 0 ? ` at '${formatValuePath(path)}'` : '';\n}\n\nfunction invalidValue(path: readonly (string | number)[]): never {\n throw new ForgeConfigError(\n `Form values must contain only finite JSON primitives, plain objects, arrays, Date, File, or Blob${pathSuffix(path)}.`,\n );\n}\n\nexport function immutable<T>(value: T, ancestors = new Set<object>(), path: (string | number)[] = []): T {\n if (value === null || value === undefined || typeof value === 'string' || typeof value === 'boolean') return value;\n if (typeof value === 'number') return Number.isFinite(value) ? value : invalidValue(path);\n if (typeof value !== 'object') return invalidValue(path);\n\n const date = value as unknown as Date;\n if (tag(value) === '[object Date]' && typeof date.getTime === 'function') {\n const time = date.getTime();\n if (!Number.isFinite(time)) return invalidValue(path);\n return Object.freeze(new Date(time)) as T;\n }\n\n if (isBlobLeaf(value)) return value;\n if (ancestors.has(value))\n throw new ForgeConfigError(`Form values must not contain circular references${pathSuffix(path)}.`);\n\n ancestors.add(value);\n try {\n if (Array.isArray(value)) {\n const copy: unknown[] = [];\n for (let index = 0; index < value.length; index++) {\n path.push(index);\n try {\n if (!Object.hasOwn(value, index))\n throw new ForgeConfigError(`Form arrays must not contain empty slots${pathSuffix(path)}.`);\n copy.push(immutable(value[index], ancestors, path));\n } finally {\n path.pop();\n }\n }\n return Object.freeze(copy) as T;\n }\n\n if (!isRecord(value)) return invalidValue(path);\n const entries = Object.entries(value).map(([key, child]) => {\n assertSafeKey(key);\n path.push(key);\n try {\n return [key, immutable(child, ancestors, path)] as const;\n } finally {\n path.pop();\n }\n });\n return Object.freeze(Object.fromEntries(entries)) as T;\n } finally {\n ancestors.delete(value);\n }\n}\n\n/**\n * Deeply converts a value into the plain shape `immutable()` accepts: primitives pass\n * through, `Date` is cloned, `File`/`Blob` keep identity, arrays map, and class instances\n * flatten to their own enumerable entries. Use as the `normalize` option of `createForm`\n * to hold domain-model classes in form state.\n *\n * Lossy conversions, applied instead of the error `immutable()` would throw: `Map`, `Set`,\n * and other keyless built-ins flatten to `{}` (their entries live on the prototype); sparse\n * array slots are dropped, which shifts later indexes; `NaN`, `Infinity`, functions, and\n * circular branches become `undefined`. Normalize model classes, not arbitrary graphs.\n */\nexport function toPlainValues<T>(value: T, seen = new WeakSet<object>()): T {\n if (value === null || value === undefined || typeof value === 'string' || typeof value === 'boolean') return value;\n if (typeof value === 'number') return Number.isFinite(value) ? value : (undefined as T);\n if (typeof value !== 'object') return undefined as T;\n\n const object = value as unknown as object;\n const date = value as unknown as Date;\n if (tag(object) === '[object Date]' && typeof date.getTime === 'function') {\n const time = date.getTime();\n return (Number.isFinite(time) ? new Date(time) : undefined) as T;\n }\n if (isBlobLeaf(object)) return value;\n if (seen.has(object)) return undefined as T;\n\n seen.add(object);\n try {\n if (Array.isArray(value)) {\n const copy: unknown[] = [];\n for (let index = 0; index < value.length; index++) {\n if (Object.hasOwn(value, index)) copy.push(toPlainValues(value[index], seen));\n }\n return copy as T;\n }\n\n // Plain objects and class instances both flatten to own enumerable entries;\n // unsafe keys are dropped so the result always passes immutable()'s key checks.\n const copy: RecordValue = {};\n for (const [key, child] of Object.entries(object)) {\n if (isUnsafeKey(key)) continue;\n copy[key] = toPlainValues(child, seen);\n }\n return copy as T;\n } finally {\n seen.delete(object);\n }\n}\n\nconst notFound = Symbol('forge-path-not-found');\n\nfunction walk(value: unknown, path: readonly (string | number)[]): unknown | typeof notFound {\n let current: unknown = value;\n\n for (const key of path) {\n if (typeof key === 'number') {\n if (\n !Array.isArray(current) ||\n !Number.isSafeInteger(key) ||\n key < 0 ||\n key >= current.length ||\n !Object.hasOwn(current, key)\n )\n return notFound;\n\n current = current[key];\n } else {\n if (!isRecord(current) || !Object.hasOwn(current, key)) return notFound;\n\n current = current[key];\n }\n }\n\n return current;\n}\n\nexport function readAtPath<T>(value: unknown, path: readonly (string | number)[]): T | undefined {\n const found = walk(value, path);\n\n return found === notFound ? undefined : (found as T);\n}\n\nexport function hasAtPath(value: unknown, path: readonly (string | number)[]): boolean {\n return walk(value, path) !== notFound;\n}\n\nexport function writeAtPath<T extends TreeValue>(\n value: T,\n path: readonly (string | number)[],\n next: unknown,\n fullPath: readonly (string | number)[] = [],\n): T {\n const [key, ...rest] = path;\n\n if (typeof key === 'number') {\n assertArrayIndex(key);\n if (!Array.isArray(value)) {\n throw new ForgeConfigError(`Cannot index ${key} because the current value is not an array.`);\n }\n\n if (key < 0 || key >= value.length) {\n throw new ForgeConfigError(`Index ${key} is out of range for array of length ${value.length}.`);\n }\n\n if (rest.length === 0) {\n const copy = [...value];\n\n copy[key] = immutable(next, new Set<object>(), [...fullPath, key]);\n\n return Object.freeze(copy) as T;\n }\n\n const copy = [...value];\n\n copy[key] = writeAtPath(value[key] as TreeValue, rest, next, [...fullPath, key]);\n\n return Object.freeze(copy) as T;\n }\n\n assertSafeKey(key);\n\n if (!isRecord(value)) {\n throw new ForgeConfigError(`Cannot select '${key}' because the current value is not an object.`);\n }\n\n if (rest.length === 0)\n return Object.freeze({ ...value, [key]: immutable(next, new Set<object>(), [...fullPath, key]) }) as T;\n\n const child = value[key];\n\n if (child !== undefined && !isRecord(child) && !Array.isArray(child)) {\n throw new ForgeConfigError(`Cannot select '${key}' because it is not an object or array.`);\n }\n\n const childTree = (child ?? {}) as TreeValue;\n\n return Object.freeze({ ...value, [key]: writeAtPath(childTree, rest, next, [...fullPath, key]) }) as T;\n}\n\nexport function resetAtPath<T extends TreeValue>(value: T, baseline: T, path: readonly (string | number)[]): T {\n const [key, ...rest] = path;\n\n if (typeof key === 'number') {\n assertArrayIndex(key);\n if (!Array.isArray(value)) return value;\n if (!Array.isArray(baseline) || key >= baseline.length || key >= value.length) return value;\n\n if (rest.length === 0) {\n const copy = [...value];\n\n copy[key] = baseline[key];\n\n return Object.freeze(copy) as T;\n }\n\n const copy = [...value];\n\n copy[key] = resetAtPath(value[key] as TreeValue, baseline[key] as TreeValue, rest);\n\n return Object.freeze(copy) as T;\n }\n\n if (!isRecord(value)) return value;\n\n if (rest.length === 0) {\n if (Object.hasOwn(baseline, key)) return Object.freeze({ ...value, [key]: (baseline as RecordValue)[key] }) as T;\n\n const { [key]: _removed, ...next } = value;\n\n return Object.freeze(next) as T;\n }\n\n const baselineChild = (baseline as RecordValue)[key];\n\n if (!isRecord(baselineChild) && !Array.isArray(baselineChild)) {\n if (Object.hasOwn(baseline, key)) return Object.freeze({ ...value, [key]: baselineChild }) as T;\n\n const { [key]: _removed, ...next } = value;\n\n return Object.freeze(next) as T;\n }\n\n const currentChild = value[key];\n const childTree = (isRecord(currentChild) || Array.isArray(currentChild) ? currentChild : {}) as TreeValue;\n\n return Object.freeze({ ...value, [key]: resetAtPath(childTree, baselineChild as TreeValue, rest) }) as T;\n}\n\nexport function pathEquals(a: readonly (string | number)[], b: readonly (string | number)[]): boolean {\n if (a.length !== b.length) return false;\n\n for (let i = 0; i < a.length; i++) {\n if (a[i] !== b[i]) return false;\n }\n\n return true;\n}\n\n/** Derives a single field's error message from the flat issue list. */\nexport function findIssue(\n issues: readonly ValidationIssue[] | undefined,\n path: readonly (string | number)[],\n): string | undefined {\n if (!issues) return undefined;\n\n for (const issue of issues) {\n if (pathEquals(issue.path, path)) return issue.message;\n }\n\n return undefined;\n}\n"],"mappings":";;;AAKA,IAAMC,oBAAa,IAAI,IAAI;CAAC;CAAa;CAAe;AAAW,CAAC;AAKpE,SAAgB,YAAY,GAAsB;CAChD,OAAO,CAACC,KAAOA,EAAI,SAAS,GAAG,KAAKD,EAAW,IAAIC,CAAG;AACxD;AAKA,SAAgB,cAAc,GAAmB;CAC/C,IAAI,YAAYA,CAAG,GAAG,MAAM,IAAIC,EAAiB,sBAAsBD,EAAI,GAAG;AAChF;AAEA,SAAgB,iBAAiB,GAAqB;CACpD,IAAI,CAAC,OAAO,cAAcE,CAAK,KAAKA,IAAQ,GAAG,MAAM,IAAID,EAAiB,uBAAuBC,EAAM,EAAE;AAC3G;AAEA,SAAS,IAAI,GAAuB;CAClC,OAAO,OAAO,UAAU,SAAS,KAAKC,CAAK;AAC7C;AAEA,SAAS,WAAW,GAAwB;CAC1C,OAAO,IAAIA,CAAK,MAAM,mBAAmB,IAAIA,CAAK,MAAM;AAC1D;AAGA,SAAgB,gBAAgB,GAA4C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,OAAO;CAElC,KAAK,IAAIC,IAAI,GAAGA,IAAIF,EAAE,QAAQ,KAC5B,IAAIA,EAAEE,OAAOD,EAAEC,IAAI,OAAO;CAG5B,OAAO;AACT;AAGA,SAAgB,UACd,GACA,GACoB;CACpB,IAAKC,GAEL;OAAK,IAAMC,KAASD,GAClB,IAAI,WAAWC,EAAM,MAAMxB,CAAI,GAAG,OAAOwB,EAAM;CAAA;AAInD"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
let e=require("@vielzeug/arsenal");var t=new Set;function withTouched(e,t,n){if(n===e.has(t))return e;let r=new Set(e);return n?r.add(t):r.delete(t),r}function pruneTouched(e,t){let n=[];for(let r of e)(r===t||r.startsWith(`${t}.`)||r.startsWith(`${t}[`))&&n.push(r);if(n.length===0)return e;let r=new Set(e);for(let e of n)r.delete(e);return r}function touchAllPaths(t){let n=new Set,collect=(t,r)=>{let i=Array.isArray(t)?t.map((e,t)=>[`[${t}]`,e]):(0,e.isPlainObject)(t)?Object.entries(t):[];for(let[t,a]of i){let i=r&&!t.startsWith(`[`)?`${r}.${t}`:`${r}${t}`;(0,e.isPlainObject)(a)||Array.isArray(a)?collect(a,i):n.add(i)}};return collect(t,``),n}exports.emptyTouched=t,exports.pruneTouched=pruneTouched,exports.touchAllPaths=touchAllPaths,exports.withTouched=withTouched;
|
|
2
|
+
//# sourceMappingURL=touched.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"touched.cjs","names":["isRecord","_vielzeug_arsenal","emptyTouched","next","paths","key","copy","doomed","path","children","node","index","child","segment","prefix","value"],"sources":["../../src/core/touched.ts"],"sourcesContent":["import { isRecord } from './path.js';\n\n/** Touched field paths as formatted leaf keys (see `formatValuePath`), e.g. `list[0].email`. */\nexport type TouchedPaths = ReadonlySet<string>;\n\nexport const emptyTouched: TouchedPaths = new Set<string>();\n\nexport function withTouched(paths: TouchedPaths, key: string, next: boolean): TouchedPaths {\n if (next === paths.has(key)) return paths;\n\n const copy = new Set(paths);\n\n if (next) copy.add(key);\n else copy.delete(key);\n\n return copy;\n}\n\n/** Removes the key and every descendant path, mirroring subtree deletion of a touched node. */\nexport function pruneTouched(paths: TouchedPaths, key: string): TouchedPaths {\n const doomed: string[] = [];\n\n for (const path of paths) {\n if (path === key || path.startsWith(`${key}.`) || path.startsWith(`${key}[`)) doomed.push(path);\n }\n\n if (doomed.length === 0) return paths;\n\n const copy = new Set(paths);\n\n for (const path of doomed) copy.delete(path);\n\n return copy;\n}\n\n/** Marks every leaf path of a value tree, matching per-field touch granularity: containers stay untouched. */\nexport function touchAllPaths(value: unknown): TouchedPaths {\n const paths = new Set<string>();\n const collect = (node: unknown, prefix: string): void => {\n const children: Iterable<readonly [string, unknown]> = Array.isArray(node)\n ? node.map((child, index) => [`[${index}]`, child] as const)\n : isRecord(node)\n ? Object.entries(node)\n : [];\n\n for (const [segment, child] of children) {\n const key = prefix && !segment.startsWith('[') ? `${prefix}.${segment}` : `${prefix}${segment}`;\n\n if (isRecord(child) || Array.isArray(child)) collect(child, key);\n else paths.add(key);\n }\n };\n\n collect(value, '');\n\n return paths;\n}\n"],"mappings":"A,mCAKA,IAAaE,EAA6B,IAAI,IAE9C,SAAgB,YAAY,EAAqB,EAAa,EAA6B,CACzF,GAAIC,IAASC,EAAM,IAAIC,CAAG,EAAG,OAAOD,EAEpC,IAAME,EAAO,IAAI,IAAIF,CAAK,EAK1B,OAHID,EAAMG,EAAK,IAAID,CAAG,EACjBC,EAAK,OAAOD,CAAG,EAEbC,CACT,CAGA,SAAgB,aAAa,EAAqB,EAA2B,CAC3E,IAAMC,EAAmB,CAAC,EAE1B,IAAK,IAAMC,KAAQJ,GACbI,IAASH,GAAOG,EAAK,WAAW,GAAGH,EAAI,EAAE,GAAKG,EAAK,WAAW,GAAGH,EAAI,EAAE,IAAGE,EAAO,KAAKC,CAAI,EAGhG,GAAID,EAAO,SAAW,EAAG,OAAOH,EAEhC,IAAME,EAAO,IAAI,IAAIF,CAAK,EAE1B,IAAK,IAAMI,KAAQD,EAAQD,EAAK,OAAOE,CAAI,EAE3C,OAAOF,CACT,CAGA,SAAgB,cAAc,EAA8B,CAC1D,IAAMF,EAAQ,IAAI,IACZ,SAAW,EAAe,IAAyB,CACvD,IAAMK,EAAiD,MAAM,QAAQC,CAAI,EACrEA,EAAK,KAAK,EAAO,IAAU,CAAC,IAAIC,EAAM,GAAIC,CAAK,CAAU,GAAA,EACzDZ,EAAAA,cAAAA,CAASU,CAAI,EACX,OAAO,QAAQA,CAAI,EACnB,CAAC,EAEP,IAAK,GAAM,CAACG,EAASD,KAAUH,EAAU,CACvC,IAAMJ,EAAMS,GAAU,CAACD,EAAQ,WAAW,GAAG,EAAI,GAAGC,EAAO,GAAGD,IAAY,GAAGC,IAASD,KAEtF,EAAIb,EAAAA,cAAAA,CAASY,CAAK,GAAK,MAAM,QAAQA,CAAK,EAAG,QAAQA,EAAOP,CAAG,EAC1DD,EAAM,IAAIC,CAAG,CACpB,CACF,EAIA,OAFA,QAAQU,EAAO,EAAE,EAEVX,CACT,C"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Touched field paths as formatted leaf keys (see `formatValuePath`), e.g. `list[0].email`. */
|
|
2
|
+
export type TouchedPaths = ReadonlySet<string>;
|
|
3
|
+
export declare const emptyTouched: TouchedPaths;
|
|
4
|
+
export declare function withTouched(paths: TouchedPaths, key: string, next: boolean): TouchedPaths;
|
|
5
|
+
/** Removes the key and every descendant path, mirroring subtree deletion of a touched node. */
|
|
6
|
+
export declare function pruneTouched(paths: TouchedPaths, key: string): TouchedPaths;
|
|
7
|
+
/** Marks every leaf path of a value tree, matching per-field touch granularity: containers stay untouched. */
|
|
8
|
+
export declare function touchAllPaths(value: unknown): TouchedPaths;
|
|
9
|
+
//# sourceMappingURL=touched.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"touched.d.ts","sourceRoot":"","sources":["../../src/core/touched.ts"],"names":[],"mappings":"AAEA,gGAAgG;AAChG,MAAM,MAAM,YAAY,GAAG,WAAW,CAAC,MAAM,CAAC,CAAC;AAE/C,eAAO,MAAM,YAAY,EAAE,YAAgC,CAAC;AAE5D,wBAAgB,WAAW,CAAC,KAAK,EAAE,YAAY,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,YAAY,CASzF;AAED,+FAA+F;AAC/F,wBAAgB,YAAY,CAAC,KAAK,EAAE,YAAY,EAAE,GAAG,EAAE,MAAM,GAAG,YAAY,CAc3E;AAED,8GAA8G;AAC9G,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,YAAY,CAoB1D"}
|