Skip to content

Commit

Permalink
refactor: rework set-get as a package that will be published
Browse files Browse the repository at this point in the history
  • Loading branch information
airjp73 committed Jun 18, 2024
1 parent bc26b3a commit cb4e16e
Show file tree
Hide file tree
Showing 21 changed files with 73 additions and 41 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
},
"devDependencies": {
"react-tracked": "^2.0.0",
"set-get": "*",
"@rvf/set-get": "*",
"tsconfig": "*",
"tsup-config": "*",
"typescript": "^5.4.5",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
ValidStringPaths,
ValueAtPath,
pathArrayToString,
} from "set-get";
} from "@rvf/set-get";

type SubmitTypes<FormOutputData> =
| {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/getters.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getPath } from "set-get";
import { getPath } from "@rvf/set-get";
import { FormStoreValue } from "./store";

export const getFieldValue = (
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/native-form-data/flatten.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { setPath } from "set-get";
import { setPath } from "@rvf/set-get";
import { MultiValueMap } from "./MultiValueMap";

export const objectFromPathEntries = (entries: [string, any][]) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
getPath,
stringToPathArray,
pathArrayToString,
} from "set-get";
} from "@rvf/set-get";
import { create } from "zustand/react";
import { immer } from "./immer";
import {
Expand Down
2 changes: 1 addition & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"tsconfig": "*",
"tsup-config": "*",
"typescript": "^5.4.5",
"set-get": "*",
"@rvf/set-get": "*",
"@rvf/core": "*"
}
}
2 changes: 1 addition & 1 deletion packages/react/src/base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
ValueAtPath,
getPath,
pathArrayToString,
} from "set-get";
} from "@rvf/set-get";
import { FieldArrayApi, makeFieldArrayImpl } from "./array";
import { makeImplFactory } from "./implFactory";
import {
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/implFactory.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pathArrayToString } from "set-get";
import { pathArrayToString } from "@rvf/set-get";
import * as R from "remeda";

export const makeImplFactory = <Item,>(
Expand Down
2 changes: 1 addition & 1 deletion packages/remix/src/useForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
FormEncType,
SubmitOptions,
} from "@remix-run/react";
import { toPathObject } from "../../set-get";
import { toPathObject } from "@rvf/set-get";
import { GenericObject, SubmitterOptions } from "@rvf/core";

type PartialProps<T, Props extends keyof T> = Omit<T, Props> &
Expand Down
5 changes: 5 additions & 0 deletions packages/set-get/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# RVF set-get

Internal utilities and types for working with deeply nested data.
This is primarily used internally by RVF and it's various packages.
It isn't recommended for use by most people.
6 changes: 0 additions & 6 deletions packages/set-get/index.ts

This file was deleted.

15 changes: 13 additions & 2 deletions packages/set-get/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
{
"name": "set-get",
"version": "1.0.0",
"name": "@rvf/set-get",
"version": "0.0.1",
"license": "MIT",
"main": "./dist/index.cjs.js",
"module": "./dist/index.esm.js",
"types": "./dist/index.d.ts",
"scripts": {
"build": "tsup",
"prepublishOnly": "bun run build",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"remeda": "^1.2.0"
},
"devDependencies": {
"tsconfig": "*"
}
}
6 changes: 6 additions & 0 deletions packages/set-get/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export { setPath } from "./setPath";
export { getPath } from "./getPath";
export { stringToPathArray } from "./stringToPathArray";
export { pathArrayToString } from "./pathArrayToString";
export { toPathObject } from "./toPathObject";
export * from "./types";
3 changes: 2 additions & 1 deletion packages/set-get/src/toPathObject.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { GenericObject } from "@rvf/core";
import { pathArrayToString } from "./pathArrayToString";

export type GenericObject = { [key: string]: any };

export const toPathObject = (obj: GenericObject): Record<string, unknown> => {
const entries = getLeafEntries(obj, []);
const flatEntries = entries.map(
Expand Down
8 changes: 8 additions & 0 deletions packages/set-get/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "tsconfig/tsconfig.json",
"compilerOptions": {
"module": "esnext"
},
"include": ["src/**/*.ts", "src/**/*.tsx", "src/index.ts"],
"exclude": ["node_modules"]
}
3 changes: 3 additions & 0 deletions packages/set-get/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { config } from "tsup-config";

export default config;
1 change: 1 addition & 0 deletions packages/tsup-config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ exports.config = defineConfig({
outExtension({ format }) {
return { js: `.${format}.js` };
},
noExternal: ["@rvf/set-get"],
});
2 changes: 1 addition & 1 deletion packages/zod-form-data/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"zod": ">= 3.11.0"
},
"devDependencies": {
"set-get": "*",
"@rvf/set-get": "*",
"tsconfig": "*",
"tsup-config": "*",
"typescript": "^5.4.5",
Expand Down
45 changes: 24 additions & 21 deletions packages/zod-form-data/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { setPath } from "set-get";
import { setPath } from "@rvf/set-get";
import {
z,
ZodArray,
Expand All @@ -13,7 +13,7 @@ import {
type InputType<DefaultType extends ZodTypeAny> = {
(): ZodEffects<DefaultType>;
<ProvidedType extends ZodTypeAny>(
schema: ProvidedType
schema: ProvidedType,
): ZodEffects<ProvidedType>;
};

Expand Down Expand Up @@ -50,9 +50,9 @@ export const numeric: InputType<ZodNumber> = (schema = z.number()) =>
.string()
.transform((val) => Number(val))
.refine((val) => !Number.isNaN(val)),
])
]),
),
schema
schema,
) as any;

type CheckboxOpts = {
Expand Down Expand Up @@ -95,7 +95,7 @@ export const file: InputType<z.ZodType<File>> = (schema = z.instanceof(File)) =>
* and will not require any values to be present.
*/
export const repeatable: InputType<ZodArray<any>> = (
schema = z.array(text())
schema = z.array(text()),
) => {
return z.preprocess((val) => {
if (Array.isArray(val)) return val;
Expand All @@ -109,7 +109,7 @@ export const repeatable: InputType<ZodArray<any>> = (
* Instead of passing the schema for an entire array, you pass in the schema for the item type.
*/
export const repeatableOfType = <T extends ZodTypeAny>(
schema: T
schema: T,
): ZodEffects<ZodArray<T>> => repeatable(z.array(schema));

const entries = z.array(z.tuple([z.string(), z.any()]));
Expand All @@ -120,16 +120,16 @@ type FormDataLikeInput = {
};

type FormDataType = {
<T extends z.ZodRawShape>(shape: T): ZodEffects<
<T extends z.ZodRawShape>(
shape: T,
): ZodEffects<
ZodObject<T>,
z.output<ZodObject<T>>,
FormData | FormDataLikeInput
>;
<T extends z.ZodTypeAny>(schema: T): ZodEffects<
T,
z.output<T>,
FormData | FormDataLikeInput
>;
<T extends z.ZodTypeAny>(
schema: T,
): ZodEffects<T, z.output<T>, FormData | FormDataLikeInput>;
};

const safeParseJson = (jsonString: string) => {
Expand All @@ -143,9 +143,9 @@ const safeParseJson = (jsonString: string) => {
export const json = <T extends ZodTypeAny>(schema: T): ZodEffects<T> =>
z.preprocess(
preprocessIfValid(
z.union([stripEmpty, z.string().transform((val) => safeParseJson(val))])
z.union([stripEmpty, z.string().transform((val) => safeParseJson(val))]),
),
schema
schema,
);

const processFormData = preprocessIfValid(
Expand All @@ -156,7 +156,7 @@ const processFormData = preprocessIfValid(
.refine((val) => Symbol.iterator in val)
.transform((val) => [...val])
.refine(
(val): val is z.infer<typeof entries> => entries.safeParse(val).success
(val): val is z.infer<typeof entries> => entries.safeParse(val).success,
)
.transform((data): Record<string, unknown | unknown[]> => {
const map: Map<string, unknown[]> = new Map();
Expand All @@ -168,14 +168,17 @@ const processFormData = preprocessIfValid(
}
}

return [...map.entries()].reduce((acc, [key, value]) => {
return setPath(acc, key, value.length === 1 ? value[0] : value);
}, {} as Record<string, unknown | unknown[]>);
})
return [...map.entries()].reduce(
(acc, [key, value]) => {
return setPath(acc, key, value.length === 1 ? value[0] : value);
},
{} as Record<string, unknown | unknown[]>,
);
}),
);

export const preprocessFormData = processFormData as (
formData: unknown
formData: unknown,
) => Record<string, unknown>;

/**
Expand All @@ -188,5 +191,5 @@ export const preprocessFormData = processFormData as (
export const formData: FormDataType = (shapeOrSchema: any): any =>
z.preprocess(
processFormData,
shapeOrSchema instanceof ZodType ? shapeOrSchema : z.object(shapeOrSchema)
shapeOrSchema instanceof ZodType ? shapeOrSchema : z.object(shapeOrSchema),
);
2 changes: 1 addition & 1 deletion packages/zod/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"@types/semver": "^7.5.0",
"@rvf/core": "*",
"semver": "^7.5.1",
"set-get": "*",
"@rvf/set-get": "*",
"tsconfig": "*",
"tsup-config": "*",
"typescript": "^5.4.5",
Expand Down

0 comments on commit cb4e16e

Please sign in to comment.