Skip to content

Commit

Permalink
fix(route): RouteContextBase type
Browse files Browse the repository at this point in the history
  • Loading branch information
alimd committed Jan 29, 2023
1 parent 580f2b3 commit 9e9ee78
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 38 deletions.
68 changes: 34 additions & 34 deletions core/router/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {createLogger, globalAlwatr} from '@alwatr/logger';
import {isNumber} from '@alwatr/math';
import {contextConsumer} from '@alwatr/signal';

import {RouteContext, RoutesConfig} from './type.js';
import {RouteContext, RouteContextBase, RoutesConfig} from './type.js';

import type {ParamValueType, QueryParameters} from '@alwatr/type';

Expand All @@ -15,6 +15,8 @@ export const logger = createLogger('alwatr/router');

export const routeContextConsumer = contextConsumer.bind<RouteContext>('route-context');

const documentBaseUrl = document.querySelector('base')?.href || '/';

/**
* The result of calling the current route's render() callback base on routesConfig.
*
Expand Down Expand Up @@ -78,35 +80,6 @@ export const routerOutlet = (routesConfig: RoutesConfig): unknown => {
}
};

// ----

/**
* Sanitize string value to valid parameters types.
*/
export function sanitizeValue(value?: string | null): ParamValueType {
if (value == null) {
return null;
}
// else
value = value.trim();
if (value === '') {
return value;
}
// else
const lowerValue = value.toLocaleLowerCase();
if (lowerValue === 'true' || lowerValue === 'false') {
return lowerValue === 'true';
}
// else
if (isNumber(value)) {
return +value;
}
// else
return value;
}

const documentBaseUrl = document.querySelector('base')?.href || '/';

/**
* Make anchor valid href from RouteContext format.
*
Expand All @@ -116,7 +89,7 @@ const documentBaseUrl = document.querySelector('base')?.href || '/';
* <a href=${ url({sectionList: ['product', 100]}) }>
* ```
*/
export function url(route: Partial<Pick<RouteContext, 'sectionList' | 'queryParamList' | 'hash'>>): string {
export const url = (route: Partial<RouteContextBase>): string => {
logger.logMethodArgs('url', {route});

let href = '';
Expand All @@ -135,16 +108,43 @@ export function url(route: Partial<Pick<RouteContext, 'sectionList' | 'queryPara
}

return href;
}
};

// ----

/**
* Sanitize string value to valid parameters types.
*/
export const sanitizeValue = (value?: string | null): ParamValueType => {
if (value == null) {
return null;
}
// else
value = value.trim();
if (value === '') {
return value;
}
// else
const lowerValue = value.toLocaleLowerCase();
if (lowerValue === 'true' || lowerValue === 'false') {
return lowerValue === 'true';
}
// else
if (isNumber(value)) {
return +value;
}
// else
return value;
};

/**
* Make query string from QueryParameters object.
*/
export function toQueryParamString(parameterList?: QueryParameters): string {
export const toQueryParamString = (parameterList?: QueryParameters): string => {
if (parameterList == null) return '';
const list: Array<string> = [];
for (const key of Object.keys(parameterList)) {
list.push(`${key}=${String(parameterList[key])}`);
}
return '?' + list.join('&');
}
};
25 changes: 21 additions & 4 deletions core/router/src/type.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
import {QueryParameters} from '@alwatr/type';

/**
* Route context base type.
*
* Sample:
*
* ```js
* // http://example.com:8080/product/100/book?cart=1&color=white#description
* {
* sectionList: [product, 100, book],
* queryParamList: {cart: 1, color: 'white'},
* hash: '#description',
* }
* ```
*/
export type RouteContextBase = {
sectionList: Array<string | number | boolean | null>;
queryParamList: QueryParameters;
hash: string;
}

/**
* Global route context type.
*
Expand All @@ -19,16 +39,13 @@ import {QueryParameters} from '@alwatr/type';
* }
* ```
*/
export type RouteContext = {
export type RouteContext = RouteContextBase & {
href: string;
pathname: string;
hostname: string;
port: string;
origin: string;
protocol: 'http' | 'https';
sectionList: Array<string | number | boolean | null>;
queryParamList: QueryParameters;
hash: string;
}

// @TODO: description
Expand Down

0 comments on commit 9e9ee78

Please sign in to comment.