Skip to content

Commit

Permalink
feat(common): improve extensibility on built-in pipes
Browse files Browse the repository at this point in the history
  • Loading branch information
micalevisk committed Apr 24, 2022
1 parent 1f6fca5 commit 4ce5877
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
22 changes: 20 additions & 2 deletions packages/common/pipes/parse-bool.pipe.ts
Expand Up @@ -48,14 +48,32 @@ export class ParseBoolPipe
value: string | boolean,
metadata: ArgumentMetadata,
): Promise<boolean> {
if (value === true || value === 'true') {
if (this.isTrue(value)) {
return true;
}
if (value === false || value === 'false') {
if (this.isFalse(value)) {
return false;
}
throw this.exceptionFactory(
'Validation failed (boolean string is expected)',
);
}

/**
* @param value currently processed route argument
* @returns `true` if `value` is said 'true', ie., if it is equal to the boolean
* `true` or the string `"true"`
*/
protected isTrue(value: string | boolean): boolean {
return value === true || value === 'true';
}

/**
* @param value currently processed route argument
* @returns `true` if `value` is said 'false', ie., if it is equal to the boolean
* `false` or the string `"false"`
*/
protected isFalse(value: string | boolean): boolean {
return value === false || value === 'false';
}
}
18 changes: 13 additions & 5 deletions packages/common/pipes/parse-float.pipe.ts
Expand Up @@ -39,15 +39,23 @@ export class ParseFloatPipe implements PipeTransform<string> {
* @param metadata contains metadata about the currently processed route argument
*/
async transform(value: string, metadata: ArgumentMetadata): Promise<number> {
const isNumeric =
['string', 'number'].includes(typeof value) &&
!isNaN(parseFloat(value)) &&
isFinite(value as any);
if (!isNumeric) {
if (!this.isNumeric(value)) {
throw this.exceptionFactory(
'Validation failed (numeric string is expected)',
);
}
return parseFloat(value);
}

/**
* @param value currently processed route argument
* @returns `true` if `value` is a valid float number
*/
protected isNumeric(value: string): boolean {
return (
['string', 'number'].includes(typeof value) &&
!isNaN(parseFloat(value)) &&
isFinite(value as any)
);
}
}
18 changes: 13 additions & 5 deletions packages/common/pipes/parse-int.pipe.ts
Expand Up @@ -44,15 +44,23 @@ export class ParseIntPipe implements PipeTransform<string> {
* @param metadata contains metadata about the currently processed route argument
*/
async transform(value: string, metadata: ArgumentMetadata): Promise<number> {
const isNumeric =
['string', 'number'].includes(typeof value) &&
/^-?\d+$/.test(value) &&
isFinite(value as any);
if (!isNumeric) {
if (!this.isNumeric(value)) {
throw this.exceptionFactory(
'Validation failed (numeric string is expected)',
);
}
return parseInt(value, 10);
}

/**
* @param value currently processed route argument
* @returns `true` if `value` is a valid integer number
*/
protected isNumeric(value: string): boolean {
return (
['string', 'number'].includes(typeof value) &&
/^-?\d+$/.test(value) &&
isFinite(value as any)
);
}
}

0 comments on commit 4ce5877

Please sign in to comment.