This repository has been archived by the owner on Apr 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a16a1d3
commit 4f70e60
Showing
2 changed files
with
79 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { BadRequestException, Injectable, PipeTransform } from '@nestjs/common'; | ||
import { CURSOR_PIPE_FORMAT } from '../errors/errors.constants'; | ||
import { parseObjectLiteral } from '../helpers/parse-object-literal'; | ||
|
||
/** Convert a string like "slug: "ok", name: 'Anand'" to { slug: "ok", name: "Anand" } */ | ||
@Injectable() | ||
export class CursorSlugPipe implements PipeTransform { | ||
transform(value: string): Record<string, string> | undefined { | ||
if (value == null) return undefined; | ||
if (!value.includes(':')) value = `slug:${value}`; | ||
try { | ||
const rules = parseObjectLiteral(value); | ||
const items: Record<string, string> = {}; | ||
rules.forEach((rule) => { | ||
const num = String(rule[1]); | ||
if (!num) items[rule[0]] = num; | ||
else if (rule[1]) items[rule[0]] = rule[1]; | ||
}); | ||
return items; | ||
} catch (_) { | ||
throw new BadRequestException(CURSOR_PIPE_FORMAT); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters