Skip to content

Commit 8c4ee36

Browse files
committed
feat: initial commit
0 parents  commit 8c4ee36

File tree

13 files changed

+1006
-0
lines changed

13 files changed

+1006
-0
lines changed

.gitignore

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# dependencies (bun install)
2+
node_modules
3+
4+
# output
5+
out
6+
dist
7+
lib
8+
*.tgz
9+
10+
# code coverage
11+
coverage
12+
*.lcov
13+
14+
# logs
15+
logs
16+
_.log
17+
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
18+
19+
# dotenv environment variable files
20+
.env
21+
.env.development.local
22+
.env.test.local
23+
.env.production.local
24+
.env.local
25+
26+
# caches
27+
.eslintcache
28+
.cache
29+
*.tsbuildinfo
30+
31+
# IntelliJ based IDEs
32+
.idea
33+
34+
# Finder (MacOS) folder config
35+
.DS_Store

README.md

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
# @mk-singh/web-utils
2+
3+
## Why This Library?
4+
5+
When building React applications, you often need to:
6+
7+
- Manage complex nested routes
8+
- Handle multi-step forms with navigation
9+
- Avoid hardcoding paths throughout your app
10+
- Maintain consistent navigation patterns
11+
- Build breadcrumbs and back buttons
12+
13+
This library solves these problems by providing a declarative way to define your route structure once and generate all the paths automatically.
14+
15+
## Compatibility
16+
17+
-**React.js** (Perfect for SPA routing)
18+
-**Vue.js** (Works with Vue Router)
19+
-**Next.js** (App Router & Pages Router)
20+
-**Vanilla JavaScript** (Framework agnostic)
21+
-**TypeScript** (Full type support)
22+
-**Any JavaScript bundler** (Webpack, Vite, Rollup, etc.)# @mksingh/utils
23+
24+
A collection of JavaScript utility functions designed for modern web development, with special focus on React.js applications. Modular and tree-shakeable for optimal bundle sizes.
25+
26+
## Installation
27+
28+
```javascript
29+
import { createRoutes } from "@mksingh/utils";
30+
```
31+
32+
## Modular Imports
33+
34+
Import only what you need to keep your bundle size small:
35+
36+
```javascript
37+
// Import specific utilities
38+
import { createRoutes } from "@mksingh/utils/factories/route-factory";
39+
```
40+
41+
## Features
42+
43+
### Route Factory
44+
45+
Create nested route structures with automatic path resolution and navigation helpers.
46+
47+
#### `createRoutes(basePath, routeConfig)`
48+
49+
Transforms a route configuration object into a fully resolved route structure with absolute paths.
50+
51+
**Parameters:**
52+
53+
- `basePath` (string): The base path to prepend to all routes
54+
- `routeConfig` (object): Route configuration with relative paths and navigation data
55+
56+
**Returns:** Object with resolved absolute paths and preserved navigation data
57+
58+
#### React.js Example
59+
60+
```tsx
61+
import React from 'react';
62+
import { BrowserRouter, Routes, Route, Link } from 'react-router-dom';
63+
import { createRoutes } from "@mksingh/utils/factories/route-factory";
64+
65+
// Define your route configuration
66+
const routeConfig = {
67+
dashboard: { path: "dashboard" },
68+
user: {
69+
path: "user",
70+
profile: {
71+
path: "profile",
72+
step: 1,
73+
prevPath: "../dashboard",
74+
},
75+
settings: {
76+
path: "settings",
77+
step: 2,
78+
prevPath: "./profile",
79+
},
80+
},
81+
admin: {
82+
path: "admin",
83+
step: 3,
84+
prevPath: "./user/settings",
85+
},
86+
};
87+
88+
// Generate routes with base path
89+
const routes = createRoutes("/app", routeConfig);
90+
91+
function App() {
92+
return (
93+
<BrowserRouter>
94+
<nav>
95+
<Link to={routes.dashboard.path}>Dashboard</Link>
96+
<Link to={routes.user.profile.path}>Profile</Link>
97+
<Link to={routes.user.settings.path}>Settings</Link>
98+
<Link to={routes.admin.path}>Admin</Link>
99+
</nav>
100+
101+
<Routes>
102+
<Route path={routes.dashboard.path} element={<Dashboard />} />
103+
<Route path={routes.user.profile.path} element={<Profile />} />
104+
<Route path={routes.user.settings.path} element={<Settings />} />
105+
<Route path={routes.admin.path} element={<Admin />} />
106+
</Routes>
107+
</BrowserRouter>
108+
);
109+
}
110+
111+
// Use in components for navigation
112+
function Settings() {
113+
const handleBack = () => {
114+
navigate(routes.user.settings.prevPath); // Goes to /app/user/profile
115+
};
116+
117+
return (
118+
<div>
119+
<button onClick={handleBack}>← Back</button>
120+
<h1>Settings (Step {routes.user.settings.step})</h1>
121+
</div>
122+
);
123+
}
124+
```
125+
126+
#### Basic Example
127+
128+
```javascript
129+
import { createRoutes } from "@mksingh/utils/factories/route-factory";
130+
131+
const routeConfig = {
132+
start: { path: "start" },
133+
onboarding: {
134+
path: "onboarding",
135+
basicDetails: {
136+
path: "basic-details",
137+
step: 1,
138+
prevPath: "../start",
139+
},
140+
businessDetails: {
141+
path: "business-details",
142+
step: 2,
143+
prevPath: "./basicDetails",
144+
},
145+
},
146+
review: {
147+
path: "review",
148+
step: 3,
149+
prevPath: "./onboarding/businessDetails",
150+
},
151+
};
152+
153+
const routes = createRoutes("/client", routeConfig);
154+
155+
console.log(routes);
156+
// Output:
157+
// {
158+
// start: { path: "/client/start" },
159+
// onboarding: {
160+
// path: "/client/onboarding",
161+
// basicDetails: {
162+
// path: "/client/onboarding/basic-details",
163+
// step: 1,
164+
// prevPath: "/client/start",
165+
// },
166+
// businessDetails: {
167+
// path: "/client/onboarding/business-details",
168+
// step: 2,
169+
// prevPath: "/client/onboarding/basic-details",
170+
// },
171+
// },
172+
// review: {
173+
// path: "/client/review",
174+
// step: 3,
175+
// prevPath: "/client/onboarding/business-details",
176+
// },
177+
// }
178+
```
179+
180+
#### Path Resolution Rules
181+
182+
The `createRoutes` function intelligently resolves different types of paths in `prevPath`:
183+
184+
- **Relative paths with `../`**: Navigate up one level
185+
- `../start` from `/client/onboarding/basic-details``/client/start`
186+
187+
- **Current directory paths with `./`**: Reference sibling routes
188+
- `./basicDetails` from `/client/onboarding/business-details``/client/onboarding/basic-details`
189+
190+
- **Nested paths**: Navigate to nested routes
191+
- `./onboarding/businessDetails` from `/client/review``/client/onboarding/business-details`
192+
193+
#### Use Cases
194+
195+
Perfect for:
196+
197+
- **React.js Single Page Applications** with complex routing
198+
- **Multi-step forms and wizards** in React components
199+
- **Navigation breadcrumbs** with automatic path resolution
200+
- **Dynamic route generation** for React Router
201+
- **Type-safe route management** in TypeScript React apps
202+
- **Centralized route configuration** to avoid hardcoded paths
203+
- **Back/Next navigation** in form workflows
204+
205+
## API Reference
206+
207+
### Route Factory
208+
209+
```typescript
210+
createRoutes(basePath: string, routeConfig: RouteConfig): ResolvedRoutes
211+
```
212+
213+
## Development
214+
215+
```bash
216+
# Run tests
217+
npm test
218+
219+
# Format code
220+
npm run format
221+
```
222+
223+
## License
224+
225+
MIT
226+
227+
## Contributing
228+
229+
Contributions are welcome! Please feel free to submit a Pull Request.

exports.config.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// exports.config.js - Define your exports structure
2+
export default {
3+
// Auto-generate from file structure
4+
autoGenerate: true,
5+
6+
// Manual overrides/additions
7+
customExports: {
8+
// Add any custom exports here if needed
9+
},
10+
11+
// Directories to scan
12+
include: ['src'],
13+
14+
// Patterns to exclude
15+
exclude: ['.test.', '.spec.'],
16+
17+
// Export path mapping rules
18+
pathMappings: {
19+
// Map src/factories/route-factory.ts to ./route-factory
20+
'factories/route-factory': 'route-factory',
21+
22+
// Map src/zod/get-defaults.ts to ./zod/get-defaults
23+
'zod/get-defaults': 'zod/get-defaults',
24+
25+
// Map src/zod/index.ts to ./zod
26+
'zod/index': 'zod',
27+
28+
// Map src/factories/index.ts to ./factories
29+
'factories/index': 'factories'
30+
},
31+
32+
// Output paths
33+
paths: {
34+
jsOutput: './lib/',
35+
typesOutput: './lib/types/'
36+
}
37+
};

package-lock.json

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)