0.1.1 • Published 1 year ago

@phragon/path-to-pattern v0.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

@phragon/path-to-pattern

The project is under construction, the description will be later

❯ Install

$ npm install --save @phragon/plugin-orm

Example:

pathToPattern("/:id"); // eq. "/all" -> {id: "all"}
pathToPattern("/:language|in(ru,en,fr)/*"); // eq. "/ru/any/123" -> {language: "ru", "*": ["any", "123"]}
pathToPattern("/s-1/s-2/:name?"); // eq. "/s-1/s-2" -> {} or "/s-1/s-2/abc" -> {name: "abc"}

// escape

// spetial chars :,?|{}()

// :keyName
// :keyName? - optional
// {prefix:keyNameInGroup\suffix}
// :keyName|modifier(arg1,arg2,arg3)
pathToPattern("/segment\:id/more/:id"); // eq. "/segment:id/more/abc" -> {id: "abc"}

// group

pathToPattern("/{prefix:id|d}{-:category?|d}"); // eq. "/1" -> {id: 1} or "/1-3" -> {id: 1, category: 3}

// modifiers

pathToPattern("/:id|d"); // only number from 0 to infinity
pathToPattern("/:id|r(1,20)"); // number from 1 to 20
pathToPattern("/:id|dIn(1,2,3,4)"); // number only 1,2,3,4
pathToPattern("/:id|n(5,7,-)"); // number with length from 5 to 7 with symbol - exm. "/01234" "/123-23-23"

Functions

declare function addModifier(name: string, options: AddModifierOptions): void;
declare function compilePath<R = any>(path: string): PatternInterface<R>;
declare function matchPath(path: string | PatternInterface, pathname: string, options?: MatchOptions): any;
declare function matchToPath<R = any>(path: string | PatternInterface, options?: ReplaceOptions<R>): string;
declare function pathToPattern<R = any>(path: string, options?: PathToPatternOptions): PatternInterface<R>;

Modifiers

The parameter can have a modifier. Added after specifying the name through the sign |

Variant

/path/:id|d         ->  { id: number }
/path/:id?|d        ->  { id?: number }
/:lang|in(ru,en)/*  ->  { lang: "ru" | "en", "*": string[] }

The n modifier

String consisting of numbers

RegExp: [0-9]+

Variant

:name|n
:name|n(length: number)
:name|n(min: number, max: number)
:name|n(min: number, max: number, with_chars: string)

The d modifier

Positive number from zero or more

RegExp: 0|[1-9][0-9]*

The l modifier

Lower case

RegExp: [a-z]+

The u modifier

Upper case

RegExp: [A-Z]+

The w modifier

Letters, numbers, sign _ and -

RegExp: [a-zA-Z0-9_\-]+

The wl modifier

Lower case, numbers, sign _ and -

RegExp: [a-z0-9_\-]+

The wu modifier

Upper case, numbers, sign _ and -

RegExp: [A-Z0-9_\-]+

The in & dIn modifier

List of choices

Variant

:name|in(... args: string[])
:name|dIn(... args: number[])

RegExp: .+? & 0|[1-9][0-9]*

The not & dNot modifier

All choices except the specified list

Variant

:name|not(... args: string[])
:name|dNot(... args: number[])

RegExp: .+? & 0|[1-9][0-9]*

The uuid modifier

UUID string, lowercase only or UPPERCASE only

RegExp: [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}|[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}

The date modifier

Date string

RegExp: .+?

Variant

:name|date(format?: string, timezone_offset?: string)
:name|date
:name|date(c)
:name|date(U)
SymbolDescriptionExample
cISO 8601 date2004-02-12T15:19:21+00:00
USeconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
dDay of the month, 2 digits with leading zeros01 to 31
jDay of the month without leading zeros1 to 31
mNumeric representation of a month, with leading zeros01 through 12
nNumeric representation of a month, without leading zeros1 through 12
YA full numeric representation of a year, 4 digitsExamples: 1999 or 2003
yA two digit representation of a yearExamples: 99 or 03
aLowercase Ante meridiem and Post meridiemam or pm
AUppercase Ante meridiem and Post meridiemAM or PM
g12-hour format of an hour without leading zeros1 through 12
G24-hour format of an hour without leading zeros0 through 23
h12-hour format of an hour with leading zeros01 through 12
H24-hour format of an hour with leading zeros00 through 23
iMinutes with leading zeros00 to 59
sSeconds with leading zeros00 through 59
uMicrosecondsExample: 654321
vMillisecondsExample: 654
ODifference to Greenwich time (GMT) without colon between hours and minutesExample: +0200
PDifference to Greenwich time (GMT) with colon between hours and minutesExample: +02:00
- :Special charsdate(d-m-Y\ H:i:s)
\\Escape all symbol\d

The reg modifier

Regular expression. Returns a string or math/array result if the regular expression includes groups

Variant

:reg|not(... args: string[])

RegExp: .+?