@rebasepro/formex 0.0.1-canary.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json ADDED
@@ -0,0 +1,78 @@
1
+ {
2
+ "name": "@rebasepro/formex",
3
+ "type": "module",
4
+ "version": "0.0.1-canary.0",
5
+ "publishConfig": {
6
+ "access": "public"
7
+ },
8
+ "keywords": [
9
+ "firebase",
10
+ "cms",
11
+ "admin",
12
+ "admin panel",
13
+ "firebase panel",
14
+ "firestore",
15
+ "headless",
16
+ "headless cms",
17
+ "content manager"
18
+ ],
19
+ "exports": {
20
+ ".": {
21
+ "development": "./src/index.ts",
22
+ "import": "./dist/index.es.js",
23
+ "require": "./dist/index.umd.js",
24
+ "types": "./dist/index.d.ts"
25
+ },
26
+ "./package.json": "./package.json"
27
+ },
28
+ "main": "./dist/index.umd.js",
29
+ "module": "./dist/index.es.js",
30
+ "types": "dist/index.d.ts",
31
+ "source": "src/index.ts",
32
+ "peerDependencies": {
33
+ "react": ">=19.0.0",
34
+ "react-dom": ">=19.0.0"
35
+ },
36
+ "dependencies": {
37
+ "fast-equals": "5.3.0"
38
+ },
39
+ "devDependencies": {
40
+ "@jest/globals": "^30.2.0",
41
+ "@types/jest": "^29.5.14",
42
+ "@types/node": "^20.19.17",
43
+ "@types/react": "^19.0.8",
44
+ "@types/react-dom": "^19.0.3",
45
+ "babel-plugin-react-compiler": "^19.0.0-beta-af1b7da-20250417",
46
+ "eslint-plugin-react-compiler": "^19.1.0-rc.2",
47
+ "jest": "^29.7.0",
48
+ "ts-jest": "^29.4.5",
49
+ "typescript": "^5.9.3",
50
+ "vite": "^7.2.4"
51
+ },
52
+ "scripts": {
53
+ "dev": "vite",
54
+ "build": "vite build && tsc --emitDeclarationOnly -p tsconfig.prod.json",
55
+ "clean": "rm -rf dist && find ./src -name '*.js' -type f | xargs rm -f",
56
+ "test": "jest --passWithNoTests"
57
+ },
58
+ "files": [
59
+ "dist",
60
+ "src",
61
+ "bin"
62
+ ],
63
+ "jest": {
64
+ "transform": {
65
+ "^.+\\.tsx?$": "ts-jest"
66
+ },
67
+ "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
68
+ "moduleFileExtensions": [
69
+ "ts",
70
+ "tsx",
71
+ "js",
72
+ "jsx",
73
+ "json",
74
+ "node"
75
+ ]
76
+ },
77
+ "gitHead": "646afae9c387c3ce02c699d98cd8c7272bdd1929"
78
+ }
package/src/Field.tsx ADDED
@@ -0,0 +1,158 @@
1
+ import * as React from "react";
2
+ import { useFormex } from "./Formex";
3
+ import { getIn, isFunction, isObject } from "./utils";
4
+ import { FormexController } from "./types";
5
+
6
+ export interface FieldInputProps<Value> {
7
+ /** Value of the field */
8
+ value: Value;
9
+ /** Name of the field */
10
+ name: string;
11
+ /** Multiple select? */
12
+ multiple?: boolean;
13
+ /** Is the field checked? */
14
+ checked?: boolean;
15
+ /** Change event handler */
16
+ onChange: (event: React.SyntheticEvent) => void,
17
+ /** Blur event handler */
18
+ onBlur: (event: React.FocusEvent) => void,
19
+ }
20
+
21
+ export interface FormexFieldProps<Value = any, FormValues extends object = any> {
22
+ field: FieldInputProps<Value>;
23
+ form: FormexController<FormValues>;
24
+ }
25
+
26
+ export interface FieldConfig<Value, C extends React.ElementType | undefined = undefined> {
27
+
28
+ /**
29
+ * Component to render. Can either be a string e.g. 'select', 'input', or 'textarea', or a component.
30
+ */
31
+ as?:
32
+ | C
33
+ | string
34
+ | React.ForwardRefExoticComponent<any>;
35
+
36
+ /**
37
+ * Children render function <Field name>{props => ...}</Field>)
38
+ */
39
+ children?: ((props: FormexFieldProps<Value>) => React.ReactNode) | React.ReactNode;
40
+
41
+ /**
42
+ * Validate a single field value independently
43
+ */
44
+ // validate?: FieldValidator;
45
+
46
+ /**
47
+ * Used for 'select' and related input types.
48
+ */
49
+ multiple?: boolean;
50
+
51
+ /**
52
+ * Field name
53
+ */
54
+ name: string;
55
+
56
+ /** HTML input type */
57
+ type?: string;
58
+
59
+ /** Field value */
60
+ value?: any;
61
+
62
+ /** Inner ref */
63
+ innerRef?: (instance: any) => void;
64
+
65
+ }
66
+
67
+ export type FieldProps<T, C extends React.ElementType | undefined> = {
68
+ as?: C;
69
+ } & (C extends React.ElementType ? (React.ComponentProps<C> & FieldConfig<T, C>) : FieldConfig<T, C>);
70
+
71
+ export function Field<T, C extends React.ElementType | undefined = undefined>({
72
+ validate,
73
+ name,
74
+ children,
75
+ as: is, // `as` is reserved in typescript lol
76
+ // component,
77
+ className,
78
+ ...props
79
+ }: FieldProps<T, C>) {
80
+ const formex = useFormex();
81
+
82
+ const field = getFieldProps({ name, ...props }, formex);
83
+
84
+ if (isFunction(children)) {
85
+ return children({ field, form: formex });
86
+ }
87
+
88
+ // if (component) {
89
+ // if (typeof component === "string") {
90
+ // const { innerRef, ...rest } = props;
91
+ // return React.createElement(
92
+ // component,
93
+ // { ref: innerRef, ...field, ...rest, className },
94
+ // children
95
+ // );
96
+ // }
97
+ // return React.createElement(
98
+ // component,
99
+ // { field, form: formex, ...props, className },
100
+ // children
101
+ // );
102
+ // }
103
+
104
+ // default to input here so we can check for both `as` and `children` above
105
+ const asElement = is || "input";
106
+
107
+ if (typeof asElement === "string") {
108
+ const { innerRef, ...rest } = props;
109
+ return React.createElement(
110
+ asElement,
111
+ { ref: innerRef, ...field, ...rest, className },
112
+ children
113
+ );
114
+ }
115
+
116
+ return React.createElement(asElement, { ...field, ...props, className }, children);
117
+ }
118
+
119
+ const getFieldProps = (nameOrOptions: string | FieldConfig<any>, formex: FormexController<any>): FieldInputProps<any> => {
120
+ const isAnObject = isObject(nameOrOptions);
121
+ const name = isAnObject
122
+ ? (nameOrOptions as FieldConfig<any>).name
123
+ : nameOrOptions;
124
+ const valueState = getIn(formex.values, name);
125
+
126
+ const field: FieldInputProps<any> = {
127
+ name,
128
+ value: valueState,
129
+ onChange: formex.handleChange,
130
+ onBlur: formex.handleBlur,
131
+ };
132
+ if (isAnObject) {
133
+ const {
134
+ type,
135
+ value: valueProp, // value is special for checkboxes
136
+ as: is,
137
+ multiple,
138
+ } = nameOrOptions as FieldConfig<any>;
139
+
140
+ if (type === "checkbox") {
141
+ if (valueProp === undefined) {
142
+ field.checked = !!valueState;
143
+ } else {
144
+ field.checked = !!(
145
+ Array.isArray(valueState) && ~valueState.indexOf(valueProp)
146
+ );
147
+ field.value = valueProp;
148
+ }
149
+ } else if (type === "radio") {
150
+ field.checked = valueState === valueProp;
151
+ field.value = valueProp;
152
+ } else if (is === "select" && multiple) {
153
+ field.value = field.value || [];
154
+ field.multiple = true;
155
+ }
156
+ }
157
+ return field;
158
+ };
package/src/Formex.tsx ADDED
@@ -0,0 +1,10 @@
1
+ import React, { useContext } from "react";
2
+ import { FormexController } from "./types";
3
+
4
+ const FormexContext = React.createContext<FormexController<any>>({} as any);
5
+
6
+ export const useFormex = <T extends object>() => useContext<FormexController<T>>(FormexContext);
7
+
8
+ export const Formex = ({ value, children }: { value: FormexController<any>, children: React.ReactNode }) => {
9
+ return <FormexContext.Provider value={value}>{children}</FormexContext.Provider>;
10
+ };
package/src/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ export * from "./Field";
2
+ export * from "./Formex";
3
+ export * from "./types";
4
+ export * from "./utils";
5
+ export * from "./useCreateFormex";
package/src/types.ts ADDED
@@ -0,0 +1,45 @@
1
+ import React, { FormEvent } from "react";
2
+
3
+ export type FormexController<T extends object> = {
4
+ values: T;
5
+ initialValues: T;
6
+ setValues: (values: T) => void;
7
+ setFieldValue: (key: string, value: any, shouldValidate?: boolean) => void;
8
+ touched: Record<string, boolean>;
9
+ setFieldTouched: (key: string, touched: boolean, shouldValidate?: boolean) => void;
10
+ setTouched: (touched: Record<string, boolean>) => void;
11
+ dirty: boolean;
12
+ setDirty: (dirty: boolean) => void;
13
+ setSubmitCount: (submitCount: number) => void;
14
+ errors: Record<string, string>;
15
+ setFieldError: (key: string, error?: string) => void;
16
+ handleChange: (event: React.SyntheticEvent) => void,
17
+ handleBlur: (event: React.FocusEvent) => void,
18
+ handleSubmit: (event?: FormEvent<HTMLFormElement>) => void;
19
+ validate: () => void;
20
+ resetForm: (props?: FormexResetProps<T>) => void;
21
+ submitCount: number;
22
+ isSubmitting: boolean;
23
+ setSubmitting: (isSubmitting: boolean) => void;
24
+ isValidating: boolean;
25
+ /**
26
+ * The version of the form. This is incremented every time the form is reset
27
+ * or the form is submitted.
28
+ */
29
+ version: number;
30
+
31
+ debugId?: string;
32
+
33
+ undo: () => void;
34
+ redo: () => void;
35
+
36
+ canUndo: boolean;
37
+ canRedo: boolean;
38
+ }
39
+
40
+ export type FormexResetProps<T extends object> = {
41
+ values?: T;
42
+ submitCount?: number;
43
+ errors?: Record<string, string>;
44
+ touched?: Record<string, boolean>;
45
+ };
@@ -0,0 +1,291 @@
1
+ import React, { useCallback, useEffect, useMemo, useRef, useState } from "react";
2
+ import { getIn, setIn } from "./utils";
3
+ import { deepEqual as equal } from "fast-equals";
4
+
5
+ import { FormexController, FormexResetProps } from "./types";
6
+
7
+ export function useCreateFormex<T extends object>({
8
+ initialValues,
9
+ initialErrors,
10
+ initialDirty,
11
+ initialTouched,
12
+ validation,
13
+ validateOnChange = false,
14
+ validateOnInitialRender = false,
15
+ onSubmit,
16
+ onReset,
17
+ onValuesChangeDeferred,
18
+ debugId,
19
+ }: {
20
+ initialValues: T;
21
+ initialErrors?: Record<string, string>;
22
+ initialDirty?: boolean;
23
+ initialTouched?: Record<string, boolean>;
24
+ validateOnChange?: boolean;
25
+ validateOnInitialRender?: boolean;
26
+ validation?: (
27
+ values: T
28
+ ) =>
29
+ | Record<string, string>
30
+ | Promise<Record<string, string>>
31
+ | undefined
32
+ | void;
33
+ onValuesChangeDeferred?: (values: T, controller: FormexController<T>) => void;
34
+ onSubmit?: (values: T, controller: FormexController<T>) => void | Promise<void>;
35
+ onReset?: (controller: FormexController<T>) => void | Promise<void>;
36
+ debugId?: string;
37
+ }): FormexController<T> {
38
+ const initialValuesRef = useRef<T>(initialValues);
39
+ const valuesRef = useRef<T>(initialValues);
40
+ const debugIdRef = useRef<string | undefined>(debugId);
41
+
42
+ const [values, setValuesInner] = useState<T>(initialValues);
43
+ const [touchedState, setTouchedState] = useState<Record<string, boolean>>(initialTouched ?? {});
44
+ const [errors, setErrors] = useState<Record<string, string>>(initialErrors ?? {});
45
+ const [dirty, setDirty] = useState(initialDirty ?? false);
46
+ const [submitCount, setSubmitCount] = useState(0);
47
+ const [isSubmitting, setIsSubmitting] = useState(false);
48
+ const [isValidating, setIsValidating] = useState(false);
49
+ const [version, setVersion] = useState(0);
50
+
51
+ const onValuesChangeRef = useRef(onValuesChangeDeferred);
52
+ onValuesChangeRef.current = onValuesChangeDeferred;
53
+ const debounceTimeoutRef = useRef<any>(undefined);
54
+
55
+ const callDebouncedOnValuesChange = useCallback((values: T) => {
56
+ if (onValuesChangeRef.current) {
57
+ if (debounceTimeoutRef.current) {
58
+ clearTimeout(debounceTimeoutRef.current);
59
+ }
60
+ debounceTimeoutRef.current = setTimeout(() => {
61
+ onValuesChangeRef.current?.(values, controllerRef.current);
62
+ }, 300);
63
+ }
64
+ }, []);
65
+
66
+ // Replace state for history with refs
67
+ const historyRef = useRef<T[]>([initialValues]);
68
+ const historyIndexRef = useRef<number>(0);
69
+
70
+ useEffect(() => {
71
+ if (validateOnInitialRender) {
72
+ validate();
73
+ }
74
+ }, []);
75
+
76
+ const setValues = useCallback((newValues: T) => {
77
+ valuesRef.current = newValues;
78
+ setValuesInner(newValues);
79
+ setDirty(!equal(initialValuesRef.current, newValues));
80
+ // Update history using refs
81
+ const newHistory = historyRef.current.slice(0, historyIndexRef.current + 1);
82
+ newHistory.push(newValues);
83
+ historyRef.current = newHistory;
84
+ historyIndexRef.current = newHistory.length - 1;
85
+ callDebouncedOnValuesChange(newValues);
86
+ }, [callDebouncedOnValuesChange]);
87
+
88
+ const validate = useCallback(async () => {
89
+ setIsValidating(true);
90
+ const validationErrors = await validation?.(valuesRef.current);
91
+ setErrors(validationErrors ?? {});
92
+ setIsValidating(false);
93
+ return validationErrors;
94
+ }, [validation]);
95
+
96
+ const setFieldValue = useCallback(
97
+ (key: string, value: any, shouldValidate?: boolean) => {
98
+ const newValues = setIn(valuesRef.current, key, value);
99
+ valuesRef.current = newValues;
100
+ setValuesInner(newValues);
101
+ if (!equal(getIn(initialValuesRef.current, key), value)) {
102
+ setDirty(true);
103
+ }
104
+ if (shouldValidate) {
105
+ validate();
106
+ }
107
+ // Update history using refs
108
+ const newHistory = historyRef.current.slice(0, historyIndexRef.current + 1);
109
+ newHistory.push(newValues);
110
+ historyRef.current = newHistory;
111
+ historyIndexRef.current = newHistory.length - 1;
112
+ callDebouncedOnValuesChange(newValues);
113
+ },
114
+ [validate, callDebouncedOnValuesChange]
115
+ );
116
+
117
+ const setFieldError = useCallback((key: string, error: string | undefined) => {
118
+ setErrors((prevErrors) => {
119
+ const newErrors = { ...prevErrors };
120
+ if (error) {
121
+ newErrors[key] = error;
122
+ } else {
123
+ delete newErrors[key];
124
+ }
125
+ return newErrors;
126
+ });
127
+ }, []);
128
+
129
+ const setFieldTouched = useCallback(
130
+ (key: string, touched: boolean, shouldValidate?: boolean) => {
131
+ setTouchedState((prev) => ({
132
+ ...prev,
133
+ [key]: touched,
134
+ }));
135
+ if (shouldValidate) {
136
+ validate();
137
+ }
138
+ },
139
+ [validate]
140
+ );
141
+
142
+ const handleChange = useCallback(
143
+ (event: React.SyntheticEvent) => {
144
+ const target = event.target as HTMLInputElement;
145
+ let value;
146
+ if (target.type === "checkbox") {
147
+ value = target.checked;
148
+ } else if (target.type === "number") {
149
+ value = target.valueAsNumber;
150
+ } else {
151
+ value = target.value;
152
+ }
153
+ const name = target.name;
154
+ setFieldValue(name, value, validateOnChange);
155
+ setFieldTouched(name, true);
156
+ },
157
+ [setFieldValue, setFieldTouched, validateOnChange]
158
+ );
159
+
160
+ const handleBlur = useCallback((event: React.FocusEvent) => {
161
+ const target = event.target as HTMLInputElement;
162
+ const name = target.name;
163
+ setFieldTouched(name, true);
164
+ }, [setFieldTouched]);
165
+
166
+ const submit = useCallback(
167
+ async (e?: React.FormEvent<HTMLFormElement>) => {
168
+ e?.preventDefault();
169
+ e?.stopPropagation();
170
+ setIsSubmitting(true);
171
+ setSubmitCount((prev) => prev + 1);
172
+ const validationErrors = await validation?.(valuesRef.current);
173
+ if (validationErrors && Object.keys(validationErrors).length > 0) {
174
+ setErrors(validationErrors);
175
+ } else {
176
+ setErrors({});
177
+ await onSubmit?.(valuesRef.current, controllerRef.current);
178
+ }
179
+ setIsSubmitting(false);
180
+ setVersion((prev) => prev + 1);
181
+ },
182
+ [onSubmit, validation]
183
+ );
184
+
185
+ const resetForm = useCallback((props?: FormexResetProps<T>) => {
186
+ const {
187
+ submitCount: submitCountProp,
188
+ values: valuesProp,
189
+ errors: errorsProp,
190
+ touched: touchedProp
191
+ } = props ?? {};
192
+ valuesRef.current = valuesProp ?? initialValuesRef.current;
193
+ initialValuesRef.current = valuesProp ?? initialValuesRef.current;
194
+ setValuesInner(valuesProp ?? initialValuesRef.current);
195
+ setErrors(errorsProp ?? {});
196
+ setTouchedState(touchedProp ?? initialTouched ?? {});
197
+ setDirty(false);
198
+ setSubmitCount(submitCountProp ?? 0);
199
+ setVersion((prev) => prev + 1);
200
+ onReset?.(controllerRef.current);
201
+ // Reset history with refs
202
+ historyRef.current = [valuesProp ?? initialValuesRef.current];
203
+ historyIndexRef.current = 0;
204
+ }, [onReset, initialTouched]);
205
+
206
+ const undo = useCallback(() => {
207
+ if (historyIndexRef.current > 0) {
208
+ const newIndex = historyIndexRef.current - 1;
209
+ const newValues = historyRef.current[newIndex];
210
+ setValuesInner(newValues);
211
+ valuesRef.current = newValues;
212
+ historyIndexRef.current = newIndex;
213
+ setDirty(!equal(initialValuesRef.current, newValues));
214
+ callDebouncedOnValuesChange(newValues);
215
+ }
216
+ }, [callDebouncedOnValuesChange]);
217
+
218
+ const redo = useCallback(() => {
219
+ if (historyIndexRef.current < historyRef.current.length - 1) {
220
+ const newIndex = historyIndexRef.current + 1;
221
+ const newValues = historyRef.current[newIndex];
222
+ setValuesInner(newValues);
223
+ valuesRef.current = newValues;
224
+ historyIndexRef.current = newIndex;
225
+ setDirty(!equal(initialValuesRef.current, newValues));
226
+ callDebouncedOnValuesChange(newValues);
227
+ }
228
+ }, [callDebouncedOnValuesChange]);
229
+
230
+ const controllerRef = useRef<FormexController<T>>({} as FormexController<T>);
231
+
232
+ const controller = useMemo<FormexController<T>>(
233
+ () => ({
234
+ values,
235
+ initialValues: initialValuesRef.current,
236
+ handleChange,
237
+ isSubmitting,
238
+ setSubmitting: setIsSubmitting,
239
+ setValues,
240
+ setFieldValue,
241
+ errors,
242
+ setFieldError,
243
+ touched: touchedState,
244
+ setFieldTouched,
245
+ setTouched: setTouchedState,
246
+ dirty,
247
+ setDirty,
248
+ handleSubmit: submit,
249
+ submitCount,
250
+ setSubmitCount,
251
+ handleBlur,
252
+ validate,
253
+ isValidating,
254
+ resetForm,
255
+ version,
256
+ debugId: debugIdRef.current,
257
+ undo,
258
+ redo,
259
+ canUndo: historyIndexRef.current > 0,
260
+ canRedo: historyIndexRef.current < historyRef.current.length - 1,
261
+ }),
262
+ [
263
+ values,
264
+ errors,
265
+ touchedState,
266
+ dirty,
267
+ isSubmitting,
268
+ submitCount,
269
+ isValidating,
270
+ version,
271
+ handleChange,
272
+ handleBlur,
273
+ setValues,
274
+ setFieldValue,
275
+ setFieldTouched,
276
+ setTouchedState,
277
+ setFieldError,
278
+ validate,
279
+ submit,
280
+ resetForm,
281
+ undo,
282
+ redo,
283
+ ]
284
+ );
285
+
286
+ useEffect(() => {
287
+ controllerRef.current = controller;
288
+ }, [controller]);
289
+
290
+ return controller;
291
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,100 @@
1
+ /** @private is the value an empty array? */
2
+ export const isEmptyArray = (value?: any) =>
3
+ Array.isArray(value) && value.length === 0;
4
+
5
+ /** @private is the given object a Function? */
6
+ export const isFunction = (obj: any): obj is Function =>
7
+ typeof obj === "function";
8
+
9
+ /** @private is the given object an Object? */
10
+ export const isObject = (obj: any): obj is Object =>
11
+ obj !== null && typeof obj === "object";
12
+
13
+ /** @private is the given object an integer? */
14
+ export const isInteger = (obj: any): boolean =>
15
+ String(Math.floor(Number(obj))) === obj;
16
+
17
+ /** @private is the given object a NaN? */
18
+ // eslint-disable-next-line no-self-compare
19
+ export const isNaN = (obj: any): boolean => obj !== obj;
20
+
21
+ /**
22
+ * Deeply get a value from an object via its path.
23
+ */
24
+ export function getIn(
25
+ obj: any,
26
+ key: string | string[],
27
+ def?: any,
28
+ p = 0
29
+ ) {
30
+ const path = toPath(key);
31
+ while (obj && p < path.length) {
32
+ obj = obj[path[p++]];
33
+ }
34
+
35
+ // check if path is not in the end
36
+ if (p !== path.length && !obj) {
37
+ return def;
38
+ }
39
+
40
+ return obj === undefined ? def : obj;
41
+ }
42
+
43
+ export function setIn(obj: any, path: string, value: any): any {
44
+ const res: any = clone(obj); // this keeps inheritance when obj is a class
45
+ let resVal: any = res;
46
+ let i = 0;
47
+ const pathArray = toPath(path);
48
+
49
+ for (; i < pathArray.length - 1; i++) {
50
+ const currentPath: string = pathArray[i];
51
+ const currentObj: any = getIn(obj, pathArray.slice(0, i + 1));
52
+
53
+ if (currentObj && (isObject(currentObj) || Array.isArray(currentObj))) {
54
+ resVal = resVal[currentPath] = clone(currentObj);
55
+ } else {
56
+ const nextPath: string = pathArray[i + 1];
57
+ resVal = resVal[currentPath] =
58
+ isInteger(nextPath) && Number(nextPath) >= 0 ? [] : {};
59
+ }
60
+ }
61
+
62
+ // Return original object if new value is the same as current
63
+ if ((i === 0 ? obj : resVal)[pathArray[i]] === value) {
64
+ return obj;
65
+ }
66
+
67
+ if (value === undefined) {
68
+ delete resVal[pathArray[i]];
69
+ } else {
70
+ resVal[pathArray[i]] = value;
71
+ }
72
+
73
+ // If the path array has a single element, the loop did not run.
74
+ // Deleting on `resVal` had no effect in this scenario, so we delete on the result instead.
75
+ if (i === 0 && value === undefined) {
76
+ delete res[pathArray[i]];
77
+ }
78
+
79
+ return res;
80
+ }
81
+
82
+ export function clone(value: any) {
83
+ if (Array.isArray(value)) {
84
+ return [...value];
85
+ } else if (typeof value === "object" && value !== null) {
86
+ // Preserve class instances (EntityReference, GeoPoint, etc.) - don't spread them
87
+ if (Object.getPrototypeOf(value) !== Object.prototype) {
88
+ return value;
89
+ }
90
+ return { ...value };
91
+ } else {
92
+ return value; // This is for primitive types which do not need cloning.
93
+ }
94
+ }
95
+
96
+ function toPath(value: string | string[]) {
97
+ if (Array.isArray(value)) return value; // Already in path array form.
98
+ // Replace brackets with dots, remove leading/trailing dots, then split by dot.
99
+ return value.replace(/\[(\d+)]/g, ".$1").replace(/^\./, "").replace(/\.$/, "").split(".");
100
+ }