controlresell 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const zod_1 = require("zod");
4
+ // Function to convert snake_case to camelCase
5
+ const toCamelCase = (str) => str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
6
+ // Function to convert camelCase to snake_case
7
+ const toSnakeCase = (str) => str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
8
+ // Preprocessing function to handle input
9
+ const snakeToCamel = (schema) => zod_1.z.preprocess((data) => {
10
+ if (typeof data !== "object" || data === null)
11
+ return data;
12
+ const converted = Object.fromEntries(Object.entries(data).map(([key, value]) => [toCamelCase(key), value]));
13
+ return { ...data, ...converted }; // Preserve both keys
14
+ }, schema);
15
+ // Post-processing function to add snake_case keys back
16
+ const addSnakeCaseKeys = (schema) => schema.transform((data) => {
17
+ if (typeof data !== "object" || data === null)
18
+ return data;
19
+ const converted = Object.fromEntries(Object.entries(data).map(([key, value]) => [toSnakeCase(key), value]));
20
+ return { ...data, ...converted }; // Preserve both keys
21
+ });
22
+ // Define your schema
23
+ const schema = addSnakeCaseKeys(snakeToCamel(zod_1.z.object({
24
+ firstName: zod_1.z.string(),
25
+ lastName: zod_1.z.string(),
26
+ age: zod_1.z.number(),
27
+ })));
28
+ // Example input with snake_case
29
+ const input = {
30
+ first_name: "John",
31
+ last_name: "Doe",
32
+ age: 30,
33
+ };
34
+ // Parsing input
35
+ const parsed = schema.parse(input);
36
+ console.log(parsed);
37
+ /*
38
+ Output:
39
+ {
40
+ firstName: 'John',
41
+ lastName: 'Doe',
42
+ age: 30,
43
+ first_name: 'John',
44
+ last_name: 'Doe'
45
+ }
46
+ */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "controlresell",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "module": "dist/index.js",
@@ -10,7 +10,8 @@
10
10
  "scripts": {
11
11
  "generate-index": "barrelsby --delete --directory src",
12
12
  "build": "npm run generate-index && tsc",
13
- "test": "echo \"Error: no test specified\" && exit 1"
13
+ "test": "echo \"Error: no test specified\" && exit 1",
14
+ "lint": "eslint ."
14
15
  },
15
16
  "keywords": [],
16
17
  "author": "",
@@ -18,7 +19,7 @@
18
19
  "description": "",
19
20
  "devDependencies": {
20
21
  "@eslint/js": "^9.21.0",
21
- "@guimauvedigital/eslint-plugin-safety": "^1.0.8",
22
+ "@guimauvedigital/eslint-plugin-safety": "^1.0.9",
22
23
  "barrelsby": "^2.8.1",
23
24
  "eslint": "^9.21.0",
24
25
  "globals": "^16.0.0",