@peter.naydenov/url-pattern 1.0.0 → 1.0.2
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.
- package/LICENSE +21 -0
- package/{readme.md → README.md} +17 -4
- package/dist/url-pattern.cjs.js +503 -0
- package/dist/url-pattern.es.js +492 -0
- package/dist/url-pattern.umd.js +1 -0
- package/package.json +34 -10
- package/{dist/index.js → src/main.js} +1 -1
- package/types/main.d.ts +200 -0
- package/dist/index.d.ts +0 -131
package/types/main.d.ts
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
export default UrlPattern;
|
|
2
|
+
export type UrlPatternOptions = {
|
|
3
|
+
/**
|
|
4
|
+
* - Character used for escaping special characters
|
|
5
|
+
*/
|
|
6
|
+
escapeChar?: string;
|
|
7
|
+
/**
|
|
8
|
+
* - Character that starts a named segment
|
|
9
|
+
*/
|
|
10
|
+
segmentNameStartChar?: string;
|
|
11
|
+
/**
|
|
12
|
+
* - Characters allowed in segment names
|
|
13
|
+
*/
|
|
14
|
+
segmentNameCharset?: string;
|
|
15
|
+
/**
|
|
16
|
+
* - Characters allowed in segment values
|
|
17
|
+
*/
|
|
18
|
+
segmentValueCharset?: string;
|
|
19
|
+
/**
|
|
20
|
+
* - Character that starts an optional segment
|
|
21
|
+
*/
|
|
22
|
+
optionalSegmentStartChar?: string;
|
|
23
|
+
/**
|
|
24
|
+
* - Character that ends an optional segment
|
|
25
|
+
*/
|
|
26
|
+
optionalSegmentEndChar?: string;
|
|
27
|
+
/**
|
|
28
|
+
* - Character that denotes a wildcard
|
|
29
|
+
*/
|
|
30
|
+
wildcardChar?: string;
|
|
31
|
+
};
|
|
32
|
+
export type ParsedSegment = {
|
|
33
|
+
/**
|
|
34
|
+
* - Segment name
|
|
35
|
+
*/
|
|
36
|
+
name: string;
|
|
37
|
+
/**
|
|
38
|
+
* - Segment type ('named' | 'wildcard' | 'literal')
|
|
39
|
+
*/
|
|
40
|
+
type: string;
|
|
41
|
+
/**
|
|
42
|
+
* - Whether the segment is optional
|
|
43
|
+
*/
|
|
44
|
+
optional?: boolean;
|
|
45
|
+
/**
|
|
46
|
+
* - Compiled regex string
|
|
47
|
+
*/
|
|
48
|
+
regex: string;
|
|
49
|
+
};
|
|
50
|
+
export type SegmentName = {
|
|
51
|
+
/**
|
|
52
|
+
* - Segment name
|
|
53
|
+
*/
|
|
54
|
+
name: string;
|
|
55
|
+
/**
|
|
56
|
+
* - Capture group index
|
|
57
|
+
*/
|
|
58
|
+
index: number;
|
|
59
|
+
/**
|
|
60
|
+
* - Segment type ('named' | 'wildcard')
|
|
61
|
+
*/
|
|
62
|
+
type: string;
|
|
63
|
+
};
|
|
64
|
+
export type CompiledPattern = {
|
|
65
|
+
/**
|
|
66
|
+
* - Compiled regex string
|
|
67
|
+
*/
|
|
68
|
+
regex: string;
|
|
69
|
+
/**
|
|
70
|
+
* - Compiled regex object
|
|
71
|
+
*/
|
|
72
|
+
regexObj: RegExp;
|
|
73
|
+
/**
|
|
74
|
+
* - Parsed segments
|
|
75
|
+
*/
|
|
76
|
+
segments: Array<ParsedSegment>;
|
|
77
|
+
/**
|
|
78
|
+
* - Segment name mappings
|
|
79
|
+
*/
|
|
80
|
+
segmentNames: Array<SegmentName>;
|
|
81
|
+
/**
|
|
82
|
+
* - Options used
|
|
83
|
+
*/
|
|
84
|
+
options: UrlPatternOptions;
|
|
85
|
+
/**
|
|
86
|
+
* - Whether pattern was created from regex
|
|
87
|
+
*/
|
|
88
|
+
isRegex: boolean;
|
|
89
|
+
/**
|
|
90
|
+
* - Original pattern string
|
|
91
|
+
*/
|
|
92
|
+
pattern?: string;
|
|
93
|
+
/**
|
|
94
|
+
* - Keys for regex patterns
|
|
95
|
+
*/
|
|
96
|
+
keys?: Array<string>;
|
|
97
|
+
};
|
|
98
|
+
/**
|
|
99
|
+
* UrlPattern class for matching and generating URLs
|
|
100
|
+
*/
|
|
101
|
+
export class UrlPattern {
|
|
102
|
+
/**
|
|
103
|
+
* @param {string|RegExp} pattern - Pattern string or regex
|
|
104
|
+
* @param {UrlPatternOptions|Array<string>} [options={}] - Options or keys (for regex)
|
|
105
|
+
*/
|
|
106
|
+
constructor(pattern: string | RegExp, options?: UrlPatternOptions | Array<string>);
|
|
107
|
+
/** @type {CompiledPattern} */
|
|
108
|
+
compiled: CompiledPattern;
|
|
109
|
+
/**
|
|
110
|
+
* Match a string against the pattern
|
|
111
|
+
* @param {string} str - String to match
|
|
112
|
+
* @returns {Object|null} Extracted values or null if no match
|
|
113
|
+
*/
|
|
114
|
+
match(str: string): any | null;
|
|
115
|
+
/**
|
|
116
|
+
* Generate a string from the pattern
|
|
117
|
+
* @param {Object} [values={}] - Values to stringify
|
|
118
|
+
* @returns {string} Generated string
|
|
119
|
+
*/
|
|
120
|
+
stringify(values?: any): string;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Creates a new UrlPattern instance (functional API)
|
|
124
|
+
* @param {string|RegExp} pattern - Pattern string or regex
|
|
125
|
+
* @param {UrlPatternOptions|Array<string>} [options={}] - Options or keys
|
|
126
|
+
* @returns {UrlPattern} UrlPattern instance
|
|
127
|
+
*/
|
|
128
|
+
export function urlPattern(pattern: string | RegExp, options?: UrlPatternOptions | Array<string>): UrlPattern;
|
|
129
|
+
/**
|
|
130
|
+
* Creates a compiled pattern from a string
|
|
131
|
+
* @param {string} pattern - Pattern string
|
|
132
|
+
* @param {UrlPatternOptions} [options={}] - Options
|
|
133
|
+
* @returns {CompiledPattern} Compiled pattern
|
|
134
|
+
*/
|
|
135
|
+
export function makePattern(pattern: string, options?: UrlPatternOptions): CompiledPattern;
|
|
136
|
+
/**
|
|
137
|
+
* Creates a compiled pattern from a regex
|
|
138
|
+
* @param {RegExp} regex - Regex pattern
|
|
139
|
+
* @param {Array<string>} [keys=[]] - Array of key names for captured groups
|
|
140
|
+
* @returns {CompiledPattern} Compiled pattern
|
|
141
|
+
*/
|
|
142
|
+
export function makePatternFromRegex(regex: RegExp, keys?: Array<string>): CompiledPattern;
|
|
143
|
+
/**
|
|
144
|
+
* Matches a string against a compiled pattern
|
|
145
|
+
* @param {CompiledPattern} compiled - Compiled pattern
|
|
146
|
+
* @param {string} str - String to match
|
|
147
|
+
* @returns {Object|null} Extracted values or null if no match
|
|
148
|
+
*/
|
|
149
|
+
export function match(compiled: CompiledPattern, str: string): any | null;
|
|
150
|
+
/**
|
|
151
|
+
* Stringifies a pattern with given values
|
|
152
|
+
* @param {CompiledPattern} compiled - Compiled pattern
|
|
153
|
+
* @param {Object} [values={}] - Values to stringify
|
|
154
|
+
* @returns {string} Generated string
|
|
155
|
+
* @throws {Error} If required values are missing
|
|
156
|
+
*/
|
|
157
|
+
export function stringify(compiled: CompiledPattern, values?: any): string;
|
|
158
|
+
/**
|
|
159
|
+
* @fileoverview URL pattern matching library
|
|
160
|
+
* @module url-pattern
|
|
161
|
+
*/
|
|
162
|
+
/**
|
|
163
|
+
* @typedef {Object} UrlPatternOptions
|
|
164
|
+
* @property {string} [escapeChar='\\'] - Character used for escaping special characters
|
|
165
|
+
* @property {string} [segmentNameStartChar=':'] - Character that starts a named segment
|
|
166
|
+
* @property {string} [segmentNameCharset='a-zA-Z0-9'] - Characters allowed in segment names
|
|
167
|
+
* @property {string} [segmentValueCharset='a-zA-Z0-9-_~ %'] - Characters allowed in segment values
|
|
168
|
+
* @property {string} [optionalSegmentStartChar='('] - Character that starts an optional segment
|
|
169
|
+
* @property {string} [optionalSegmentEndChar=')'] - Character that ends an optional segment
|
|
170
|
+
* @property {string} [wildcardChar='*'] - Character that denotes a wildcard
|
|
171
|
+
*/
|
|
172
|
+
/**
|
|
173
|
+
* @typedef {Object} ParsedSegment
|
|
174
|
+
* @property {string} name - Segment name
|
|
175
|
+
* @property {string} type - Segment type ('named' | 'wildcard' | 'literal')
|
|
176
|
+
* @property {boolean} [optional=false] - Whether the segment is optional
|
|
177
|
+
* @property {string} regex - Compiled regex string
|
|
178
|
+
*/
|
|
179
|
+
/**
|
|
180
|
+
* @typedef {Object} SegmentName
|
|
181
|
+
* @property {string} name - Segment name
|
|
182
|
+
* @property {number} index - Capture group index
|
|
183
|
+
* @property {string} type - Segment type ('named' | 'wildcard')
|
|
184
|
+
*/
|
|
185
|
+
/**
|
|
186
|
+
* @typedef {Object} CompiledPattern
|
|
187
|
+
* @property {string} regex - Compiled regex string
|
|
188
|
+
* @property {RegExp} regexObj - Compiled regex object
|
|
189
|
+
* @property {Array<ParsedSegment>} segments - Parsed segments
|
|
190
|
+
* @property {Array<SegmentName>} segmentNames - Segment name mappings
|
|
191
|
+
* @property {UrlPatternOptions} options - Options used
|
|
192
|
+
* @property {boolean} isRegex - Whether pattern was created from regex
|
|
193
|
+
* @property {string} [pattern] - Original pattern string
|
|
194
|
+
* @property {Array<string>} [keys] - Keys for regex patterns
|
|
195
|
+
*/
|
|
196
|
+
/**
|
|
197
|
+
* Default options for URL pattern matching
|
|
198
|
+
* @type {UrlPatternOptions}
|
|
199
|
+
*/
|
|
200
|
+
export const DEFAULT_OPTIONS: UrlPatternOptions;
|
package/dist/index.d.ts
DELETED
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Options for customizing the pattern syntax
|
|
3
|
-
*/
|
|
4
|
-
export interface UrlPatternOptions {
|
|
5
|
-
/** Character used for escaping special characters (default: '\\') */
|
|
6
|
-
escapeChar?: string;
|
|
7
|
-
/** Character that starts a named segment (default: ':') */
|
|
8
|
-
segmentNameStartChar?: string;
|
|
9
|
-
/** Characters allowed in segment names (default: 'a-zA-Z0-9') */
|
|
10
|
-
segmentNameCharset?: string;
|
|
11
|
-
/** Characters allowed in segment values (default: 'a-zA-Z0-9-_~ %') */
|
|
12
|
-
segmentValueCharset?: string;
|
|
13
|
-
/** Character that starts an optional segment (default: '(') */
|
|
14
|
-
optionalSegmentStartChar?: string;
|
|
15
|
-
/** Character that ends an optional segment (default: ')') */
|
|
16
|
-
optionalSegmentEndChar?: string;
|
|
17
|
-
/** Character that denotes a wildcard (default: '*') */
|
|
18
|
-
wildcardChar?: string;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Result of matching a pattern against a string
|
|
23
|
-
*/
|
|
24
|
-
export interface MatchResult {
|
|
25
|
-
[key: string]: string | string[] | null;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Compiled pattern object
|
|
30
|
-
*/
|
|
31
|
-
export interface CompiledPattern {
|
|
32
|
-
regex: string;
|
|
33
|
-
regexObj: RegExp;
|
|
34
|
-
segments: ParsedSegment[];
|
|
35
|
-
segmentNames: SegmentName[];
|
|
36
|
-
options: UrlPatternOptions;
|
|
37
|
-
isRegex: boolean;
|
|
38
|
-
pattern?: string;
|
|
39
|
-
keys?: string[];
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Parsed segment
|
|
44
|
-
*/
|
|
45
|
-
export interface ParsedSegment {
|
|
46
|
-
name: string;
|
|
47
|
-
type: 'named' | 'wildcard' | 'literal';
|
|
48
|
-
optional?: boolean;
|
|
49
|
-
regex: string;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Segment name mapping
|
|
54
|
-
*/
|
|
55
|
-
export interface SegmentName {
|
|
56
|
-
name: string;
|
|
57
|
-
index: number;
|
|
58
|
-
type: 'named' | 'wildcard';
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* UrlPattern class for matching and generating URLs
|
|
63
|
-
*/
|
|
64
|
-
export class UrlPattern {
|
|
65
|
-
/**
|
|
66
|
-
* @param pattern - Pattern string or RegExp
|
|
67
|
-
* @param options - Options object or keys array (for regex patterns)
|
|
68
|
-
*/
|
|
69
|
-
constructor(pattern: string | RegExp, options?: UrlPatternOptions | string[]);
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Match a string against the pattern
|
|
73
|
-
* @param str - String to match
|
|
74
|
-
* @returns Extracted values or null if no match
|
|
75
|
-
*/
|
|
76
|
-
match(str: string): MatchResult | null;
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Generate a string from the pattern
|
|
80
|
-
* @param values - Values to stringify
|
|
81
|
-
* @returns Generated string
|
|
82
|
-
*/
|
|
83
|
-
stringify(values?: Record<string, any>): string;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Creates a new UrlPattern instance (functional API)
|
|
88
|
-
* @param pattern - Pattern string or RegExp
|
|
89
|
-
* @param options - Options object or keys array (for regex patterns)
|
|
90
|
-
* @returns UrlPattern instance
|
|
91
|
-
*/
|
|
92
|
-
export function urlPattern(pattern: string | RegExp, options?: UrlPatternOptions | string[]): UrlPattern;
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Creates a compiled pattern from a string
|
|
96
|
-
* @param pattern - Pattern string
|
|
97
|
-
* @param options - Options
|
|
98
|
-
* @returns Compiled pattern
|
|
99
|
-
*/
|
|
100
|
-
export function makePattern(pattern: string, options?: UrlPatternOptions): CompiledPattern;
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Creates a compiled pattern from a regex
|
|
104
|
-
* @param regex - Regex pattern
|
|
105
|
-
* @param keys - Array of key names for captured groups
|
|
106
|
-
* @returns Compiled pattern
|
|
107
|
-
*/
|
|
108
|
-
export function makePatternFromRegex(regex: RegExp, keys?: string[]): CompiledPattern;
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* Matches a string against a compiled pattern
|
|
112
|
-
* @param compiled - Compiled pattern
|
|
113
|
-
* @param str - String to match
|
|
114
|
-
* @returns Extracted values or null
|
|
115
|
-
*/
|
|
116
|
-
export function match(compiled: CompiledPattern, str: string): MatchResult | null;
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* Stringifies a pattern with given values
|
|
120
|
-
* @param compiled - Compiled pattern
|
|
121
|
-
* @param values - Values to stringify
|
|
122
|
-
* @returns Generated string
|
|
123
|
-
*/
|
|
124
|
-
export function stringify(compiled: CompiledPattern, values?: Record<string, any>): string;
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Default options
|
|
128
|
-
*/
|
|
129
|
-
export const DEFAULT_OPTIONS: UrlPatternOptions;
|
|
130
|
-
|
|
131
|
-
export default UrlPattern;
|