Skip to content

Commit

Permalink
refactor: convert all places to use the new helper
Browse files Browse the repository at this point in the history
  • Loading branch information
airjp73 committed Sep 17, 2024
1 parent 5ce6502 commit 3656121
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 16 deletions.
Binary file modified bun.lockb
Binary file not shown.
9 changes: 2 additions & 7 deletions packages/core/src/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ import {
StringToPathTuple,
ValidStringPaths,
ValueAtPath,
pathArrayToString,
stringToPathArray,
mergePathStrings,
} from "@rvf/set-get";

type SubmitTypes<FormOutputData> =
Expand Down Expand Up @@ -144,11 +143,7 @@ const instantiateFormScope = <FormInputData extends FieldValues>(
__field_prefix__: prefix,
__store__: store,
scope(field) {
const newPrefix = pathArrayToString(
[...stringToPathArray(prefix), ...stringToPathArray(field)].filter(
(segment) => segment !== "",
),
);
const newPrefix = mergePathStrings(prefix, field);
if (store.subformCache.has(newPrefix))
return store.subformCache.get(newPrefix);

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
setPath,
getPath,
stringToPathArray,
pathArrayToString,
mergePathStrings,
} from "@rvf/set-get";
import { create } from "zustand/react";
import { immer } from "./immer";
Expand Down Expand Up @@ -391,7 +391,7 @@ export const moveFieldArrayKeys = (
if (Number.isNaN(index))
throw new Error(`Attempted to update a non-array field, ${fieldName}`);
const newIndex = updater(index);
return pathArrayToString([fieldName, newIndex, ...parts]);
return mergePathStrings(fieldName, newIndex, ...parts);
});
});
};
Expand Down
5 changes: 2 additions & 3 deletions packages/react/src/base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
ValidStringPathsToArrays,
ValueAtPath,
getPath,
pathArrayToString,
mergePathStrings,
} from "@rvf/set-get";
import { FieldArrayApi, makeFieldArrayImpl } from "./array";
import { makeImplFactory } from "./implFactory";
Expand Down Expand Up @@ -463,8 +463,7 @@ export const makeBaseFormApi = <FormInputData,>({
form,
}: BaseReactFormParams<FormInputData>): FormApi<FormInputData> => {
const prefix = form.__field_prefix__;
const f = (fieldName?: string) =>
pathArrayToString([prefix, fieldName].filter(isNonNullish));
const f = (fieldName?: string) => mergePathStrings(prefix, fieldName);
const transientState = () => form.__store__.store.getState();

type WithOptionalField<T> = [string, T] | [T];
Expand Down
6 changes: 2 additions & 4 deletions packages/react/src/implFactory.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pathArrayToString } from "@rvf/set-get";
import { mergePathStrings } from "@rvf/set-get";

const isNonNullish = <T,>(value: T | null | undefined): value is T =>
value != null;
Expand All @@ -10,9 +10,7 @@ export const makeImplFactory = <Item,>(
const implCache = new Map<string, Item>();

return (fieldName?: string) => {
const fullName = pathArrayToString(
[prefix, fieldName].filter(isNonNullish),
);
const fullName = mergePathStrings(prefix, fieldName);

const existingImpl = implCache.get(fullName);
if (existingImpl) return existingImpl;
Expand Down
1 change: 1 addition & 0 deletions packages/set-get/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export { getPath } from "./getPath";
export { stringToPathArray } from "./stringToPathArray";
export { pathArrayToString } from "./pathArrayToString";
export { toPathObject } from "./toPathObject";
export { mergePathStrings } from "./mergePathStrings";
export * from "./types";

0 comments on commit 3656121

Please sign in to comment.