@peter.naydenov/url-pattern 1.0.0 → 1.0.1

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.
@@ -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;