|
1 | 1 | import {URL} from 'node:url'; |
| 2 | +import {UrlMutator} from './mutations'; |
| 3 | +import {NormalizedUrl} from './normalized-url'; |
| 4 | +import {ParsedUrl} from './parsed-url'; |
2 | 5 |
|
3 | | -export class UrlSet<T extends URL> extends Set<string> { |
4 | | - unparsable: Set<string> = new Set<string>(); |
5 | | - duplicates: Set<string> = new Set<string>(); |
| 6 | +abstract class UrlTypeSet<T extends URL> extends Set<T> { |
| 7 | + verifier = new Set<string>(); |
| 8 | + unparsable = new Set<string>(); |
6 | 9 |
|
7 | 10 | public constructor( |
8 | | - private typeConstructor: Constructor<T>, |
9 | | - values: string[], |
10 | | - public strict: boolean = false |
| 11 | + values?: T[] | string[], |
| 12 | + readonly defaultBase?: string | URL, |
| 13 | + readonly strict: boolean = false |
11 | 14 | ) { |
12 | 15 | super(); |
13 | | - this.addItems(values); |
| 16 | + if (values !== undefined) this.addItems(values); |
14 | 17 | } |
15 | 18 |
|
16 | | - override add(value: string): this { |
17 | | - const u = this.parse(value); |
18 | | - if (u) { |
19 | | - if (super.has(u.href)) { |
20 | | - this.duplicates.add(value); |
21 | | - } else { |
22 | | - super.add(u.href); |
| 19 | + override add(value: T | string): this { |
| 20 | + const incoming = typeof value === 'string' ? this.parse(value) : value; |
| 21 | + if (incoming) { |
| 22 | + if (!this.has(incoming)) { |
| 23 | + super.add(incoming); |
| 24 | + this.verifier.add(incoming.href); |
23 | 25 | } |
24 | 26 | } else { |
25 | | - this.unparsable.add(value); |
| 27 | + this.unparsable.add(value as string); |
26 | 28 | } |
27 | 29 | return this; |
28 | 30 | } |
29 | 31 |
|
30 | | - override has(value: string): boolean { |
31 | | - const parsed = this.parse(value); |
32 | | - if (parsed) return super.has(parsed.href); |
33 | | - else return false; |
| 32 | + override has(value: T | string): boolean { |
| 33 | + const incoming = typeof value === 'string' ? this.parse(value) : value; |
| 34 | + if (!incoming) return false; |
| 35 | + return this.verifier.has(incoming.href); |
34 | 36 | } |
35 | 37 |
|
36 | | - override delete(value: string): boolean { |
37 | | - const parsed = this.parse(value); |
38 | | - if (parsed) return super.delete(parsed.href); |
39 | | - else return false; |
| 38 | + override delete(value: T | string): boolean { |
| 39 | + const incoming = typeof value === 'string' ? this.parse(value) : value; |
| 40 | + if (incoming) { |
| 41 | + if (this.verifier.delete(incoming.href)) { |
| 42 | + this.forEach(v => { |
| 43 | + if (v.href === incoming.href) super.delete(v); |
| 44 | + }); |
| 45 | + return true; |
| 46 | + } |
| 47 | + } |
| 48 | + return false; |
40 | 49 | } |
41 | 50 |
|
42 | 51 | override clear(): void { |
| 52 | + this.verifier.clear(); |
43 | 53 | this.unparsable.clear(); |
44 | | - this.duplicates.clear(); |
45 | 54 | super.clear(); |
46 | 55 | } |
47 | 56 |
|
48 | | - addItems(values: Array<string>): this { |
| 57 | + addItems(values: T[] | string[]): this { |
49 | 58 | values.forEach(v => { |
50 | 59 | this.add(v); |
51 | 60 | }); |
52 | 61 | return this; |
53 | 62 | } |
54 | 63 |
|
55 | | - hydrate(): T[] { |
56 | | - return [...this].map(u => new this.typeConstructor(u)) as T[]; |
| 64 | + protected abstract parse(input: string, base?: string | URL): T | false; |
| 65 | +} |
| 66 | + |
| 67 | +export class UrlSet extends UrlTypeSet<URL> { |
| 68 | + protected override parse( |
| 69 | + input: string, |
| 70 | + base?: string | URL, |
| 71 | + recursing = false |
| 72 | + ): URL | false { |
| 73 | + try { |
| 74 | + return new URL(input, base); |
| 75 | + } catch (e: unknown) { |
| 76 | + if (!recursing && base === undefined && this.defaultBase !== undefined) { |
| 77 | + try { |
| 78 | + return this.parse(input, this.defaultBase, true); |
| 79 | + } catch { |
| 80 | + this.unparsable.add(input); |
| 81 | + return false; |
| 82 | + } |
| 83 | + } else { |
| 84 | + if (e! instanceof TypeError || this.strict) { |
| 85 | + throw e; |
| 86 | + } |
| 87 | + } |
| 88 | + this.unparsable.add(input); |
| 89 | + return false; |
| 90 | + } |
57 | 91 | } |
| 92 | +} |
58 | 93 |
|
59 | | - parse(value: string): T | false { |
| 94 | +export class ParsedUrlSet extends UrlTypeSet<ParsedUrl> { |
| 95 | + protected override parse( |
| 96 | + input: string, |
| 97 | + base?: string | URL, |
| 98 | + recursing = false |
| 99 | + ): ParsedUrl | false { |
60 | 100 | try { |
61 | | - return new this.typeConstructor(value); |
| 101 | + return new ParsedUrl(input, base); |
62 | 102 | } catch (e: unknown) { |
63 | | - if (this.strict) { |
64 | | - throw e; |
| 103 | + if (!recursing && base === undefined && this.defaultBase !== undefined) { |
| 104 | + try { |
| 105 | + return this.parse(input, this.defaultBase, true); |
| 106 | + } catch { |
| 107 | + this.unparsable.add(input); |
| 108 | + return false; |
| 109 | + } |
65 | 110 | } else { |
66 | | - return false; |
| 111 | + if (e! instanceof TypeError || this.strict) { |
| 112 | + throw e; |
| 113 | + } |
67 | 114 | } |
| 115 | + this.unparsable.add(input); |
| 116 | + return false; |
68 | 117 | } |
69 | 118 | } |
70 | 119 | } |
71 | 120 |
|
72 | | -type Constructor<T> = new (input: string, base?: string | T) => T; |
| 121 | +export class NormalizedUrlSet extends UrlTypeSet<NormalizedUrl> { |
| 122 | + public constructor( |
| 123 | + values?: NormalizedUrl[] | string[], |
| 124 | + readonly normalizeer: UrlMutator = NormalizedUrl.normalizer, |
| 125 | + readonly defaultBase?: string | URL, |
| 126 | + strict = false |
| 127 | + ) { |
| 128 | + super(values, defaultBase, strict); |
| 129 | + if (values !== undefined) this.addItems(values); |
| 130 | + } |
| 131 | + |
| 132 | + protected override parse( |
| 133 | + input: string, |
| 134 | + base?: string | URL, |
| 135 | + recursing = false |
| 136 | + ): NormalizedUrl | false { |
| 137 | + try { |
| 138 | + return new NormalizedUrl(input, base); |
| 139 | + } catch (e: unknown) { |
| 140 | + if (!recursing && base === undefined && this.defaultBase !== undefined) { |
| 141 | + try { |
| 142 | + return this.parse(input, this.defaultBase, true); |
| 143 | + } catch { |
| 144 | + this.unparsable.add(input); |
| 145 | + return false; |
| 146 | + } |
| 147 | + } else { |
| 148 | + if (e! instanceof TypeError || this.strict) { |
| 149 | + throw e; |
| 150 | + } |
| 151 | + } |
| 152 | + this.unparsable.add(input); |
| 153 | + return false; |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments