@deallony/shared 1.1.99 → 1.2.1

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.
@@ -1 +1,2 @@
1
1
  export * from './converter';
2
+ export * from './routes';
@@ -15,3 +15,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./converter"), exports);
18
+ __exportStar(require("./routes"), exports);
@@ -0,0 +1,11 @@
1
+ type RoutePrimitive = string | number | boolean;
2
+ type RouteParamValue = RoutePrimitive | null | undefined | RoutePrimitive[];
3
+ export type RouteLike = {
4
+ pathname: string;
5
+ params?: Record<string, RouteParamValue>;
6
+ };
7
+ export declare const routeToString: (route: RouteLike) => string;
8
+ export declare const RoutesUtils: {
9
+ routeToString: (route: RouteLike) => string;
10
+ };
11
+ export {};
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RoutesUtils = exports.routeToString = void 0;
4
+ const isNil = (value) => value == null;
5
+ const encodeParam = (value) => encodeURIComponent(String(value));
6
+ const stringifyQueryValue = (value) => {
7
+ if (isNil(value)) {
8
+ return undefined;
9
+ }
10
+ if (Array.isArray(value)) {
11
+ if (!value.length) {
12
+ return undefined;
13
+ }
14
+ return value.map((item) => encodeParam(item)).join(",");
15
+ }
16
+ return encodeParam(value);
17
+ };
18
+ const routeToString = (route) => {
19
+ const rawPathname = (route === null || route === void 0 ? void 0 : route.pathname) || "";
20
+ const params = (route === null || route === void 0 ? void 0 : route.params) || {};
21
+ let pathname = rawPathname;
22
+ const queryEntries = [];
23
+ for (const [key, value] of Object.entries(params)) {
24
+ const dynamicSegment = `[${key}]`;
25
+ if (pathname.includes(dynamicSegment) && !isNil(value) && !Array.isArray(value)) {
26
+ pathname = pathname.replace(dynamicSegment, encodeParam(value));
27
+ continue;
28
+ }
29
+ const queryValue = stringifyQueryValue(value);
30
+ if (queryValue === undefined) {
31
+ continue;
32
+ }
33
+ queryEntries.push(`${encodeURIComponent(key)}=${queryValue}`);
34
+ }
35
+ const query = queryEntries.join("&");
36
+ return query ? `${pathname}?${query}` : pathname;
37
+ };
38
+ exports.routeToString = routeToString;
39
+ exports.RoutesUtils = {
40
+ routeToString: exports.routeToString,
41
+ };
@@ -1 +1,2 @@
1
1
  export * from './converter.js';
2
+ export * from './routes.js';
@@ -0,0 +1,37 @@
1
+ const isNil = (value) => value == null;
2
+ const encodeParam = (value) => encodeURIComponent(String(value));
3
+ const stringifyQueryValue = (value) => {
4
+ if (isNil(value)) {
5
+ return undefined;
6
+ }
7
+ if (Array.isArray(value)) {
8
+ if (!value.length) {
9
+ return undefined;
10
+ }
11
+ return value.map((item) => encodeParam(item)).join(",");
12
+ }
13
+ return encodeParam(value);
14
+ };
15
+ export const routeToString = (route) => {
16
+ const rawPathname = (route === null || route === void 0 ? void 0 : route.pathname) || "";
17
+ const params = (route === null || route === void 0 ? void 0 : route.params) || {};
18
+ let pathname = rawPathname;
19
+ const queryEntries = [];
20
+ for (const [key, value] of Object.entries(params)) {
21
+ const dynamicSegment = `[${key}]`;
22
+ if (pathname.includes(dynamicSegment) && !isNil(value) && !Array.isArray(value)) {
23
+ pathname = pathname.replace(dynamicSegment, encodeParam(value));
24
+ continue;
25
+ }
26
+ const queryValue = stringifyQueryValue(value);
27
+ if (queryValue === undefined) {
28
+ continue;
29
+ }
30
+ queryEntries.push(`${encodeURIComponent(key)}=${queryValue}`);
31
+ }
32
+ const query = queryEntries.join("&");
33
+ return query ? `${pathname}?${query}` : pathname;
34
+ };
35
+ export const RoutesUtils = {
36
+ routeToString,
37
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deallony/shared",
3
- "version": "1.1.99",
3
+ "version": "1.2.01",
4
4
  "private": false,
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",