Skip to content

Commit 3891579

Browse files
committed
Reorder methods alphabetically in utility modules
Organizes methods in alphabetical order for improved readability: - encode.ts: Move helper functions after encode functions - purl-component.ts: Swap componentComparator and componentSortOrder - strings.ts: Move localeCompare after isSemverString - validate.ts: Swap validateQualifierKey and validateQualifiers This consistent ordering makes functions easier to locate and maintain.
1 parent 346e2c6 commit 3891579

File tree

4 files changed

+78
-77
lines changed

4 files changed

+78
-77
lines changed

src/encode.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,6 @@ function encodeNamespace(namespace: unknown): string {
3434
: ''
3535
}
3636

37-
/**
38-
* Normalize URLSearchParams output for qualifier encoding.
39-
*/
40-
function normalizeSearchParamsEncoding(encoded: string): string {
41-
return encoded.replaceAll('%2520', '%20').replaceAll('+', '%2B')
42-
}
43-
44-
/**
45-
* Prepare string value for URLSearchParams encoding.
46-
*/
47-
function prepareValueForSearchParams(value: unknown): string {
48-
// Replace spaces with %20's so they don't get converted to plus signs.
49-
return String(value).replaceAll(' ', '%20')
50-
}
51-
5237
/**
5338
* Encode qualifier parameter key or value.
5439
*/
@@ -109,6 +94,21 @@ function encodeVersion(version: unknown): string {
10994
: ''
11095
}
11196

97+
/**
98+
* Normalize URLSearchParams output for qualifier encoding.
99+
*/
100+
function normalizeSearchParamsEncoding(encoded: string): string {
101+
return encoded.replaceAll('%2520', '%20').replaceAll('+', '%2B')
102+
}
103+
104+
/**
105+
* Prepare string value for URLSearchParams encoding.
106+
*/
107+
function prepareValueForSearchParams(value: unknown): string {
108+
// Replace spaces with %20's so they don't get converted to plus signs.
109+
return String(value).replaceAll(' ', '%20')
110+
}
111+
112112
export {
113113
encodeComponent,
114114
encodeName,

src/purl-component.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,6 @@ const componentSortOrderLookup = {
5252
subpath: 7,
5353
}
5454

55-
/**
56-
* Get sort order for component name.
57-
*/
58-
function componentSortOrder(comp: string): string | number {
59-
return (
60-
(componentSortOrderLookup as unknown as Record<string, number>)[comp] ??
61-
comp
62-
)
63-
}
64-
6555
/**
6656
* Compare two component names for sorting.
6757
*/
@@ -72,6 +62,16 @@ function componentComparator(compA: string, compB: string): number {
7262
)
7363
}
7464

65+
/**
66+
* Get sort order for component name.
67+
*/
68+
function componentSortOrder(comp: string): string | number {
69+
return (
70+
(componentSortOrderLookup as unknown as Record<string, number>)[comp] ??
71+
comp
72+
)
73+
}
74+
7575
/**
7676
* Encode PURL component value to string.
7777
*/

src/strings.ts

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,6 @@
33
* Includes whitespace detection, semver validation, locale comparison, and character replacement.
44
*/
55

6-
// Intl.Collator is faster than String#localeCompare
7-
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare:
8-
// > When comparing large numbers of strings, such as in sorting large arrays,
9-
// > it is better to create an Intl.Collator object and use the function provided
10-
// > by its compare() method.
11-
let _localeCompare: Intl.Collator['compare'] | undefined
12-
/**
13-
* Perform locale-aware string comparison.
14-
*/
15-
function localeCompare(x: string, y: string): number {
16-
if (_localeCompare === undefined) {
17-
// Lazily call new Intl.Collator() because in Node it can take 10-14ms.
18-
_localeCompare = new Intl.Collator().compare
19-
}
20-
return _localeCompare(x, y)
21-
}
22-
23-
// This regexp is valid as of 2024-08-01.
24-
// https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
25-
const regexSemverNumberedGroups =
26-
/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/
27-
286
/**
297
* Check if string contains only whitespace characters.
308
*/
@@ -103,13 +81,36 @@ function isNonEmptyString(value: unknown): value is string {
10381
return typeof value === 'string' && value.length > 0
10482
}
10583

84+
// This regexp is valid as of 2024-08-01.
85+
// https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
86+
const regexSemverNumberedGroups =
87+
/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/
88+
10689
/**
10790
* Check if value is a valid semantic version string.
10891
*/
10992
function isSemverString(value: unknown): value is string {
11093
return typeof value === 'string' && regexSemverNumberedGroups.test(value)
11194
}
11295

96+
// Intl.Collator is faster than String#localeCompare
97+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare:
98+
// > When comparing large numbers of strings, such as in sorting large arrays,
99+
// > it is better to create an Intl.Collator object and use the function provided
100+
// > by its compare() method.
101+
let _localeCompare: Intl.Collator['compare'] | undefined
102+
103+
/**
104+
* Perform locale-aware string comparison.
105+
*/
106+
function localeCompare(x: string, y: string): number {
107+
if (_localeCompare === undefined) {
108+
// Lazily call new Intl.Collator() because in Node it can take 10-14ms.
109+
_localeCompare = new Intl.Collator().compare
110+
}
111+
return _localeCompare(x, y)
112+
}
113+
113114
/**
114115
* Convert package name to lowercase.
115116
*/

src/validate.ts

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -51,36 +51,6 @@ function validateNamespace(namespace: unknown, throws: boolean): boolean {
5151
return validateStrings('namespace', namespace, throws)
5252
}
5353

54-
/**
55-
* Validate qualifiers object structure and keys.
56-
* @throws {PurlError} When validation fails and throws is true.
57-
*/
58-
function validateQualifiers(qualifiers: unknown, throws: boolean): boolean {
59-
if (qualifiers === null || qualifiers === undefined) {
60-
return true
61-
}
62-
if (typeof qualifiers !== 'object') {
63-
if (throws) {
64-
throw new PurlError('"qualifiers" must be an object')
65-
}
66-
return false
67-
}
68-
const qualifiersObj = qualifiers as QualifiersObject | URLSearchParams
69-
const keysProperty = (qualifiersObj as QualifiersObject)['keys']
70-
const keysIterable: Iterable<string> =
71-
// URLSearchParams instances have a "keys" method that returns an iterator.
72-
typeof keysProperty === 'function'
73-
? (ReflectApply(keysProperty, qualifiersObj, []) as Iterable<string>)
74-
: (Object.keys(qualifiers as QualifiersObject) as Iterable<string>)
75-
// Use for-of to work with URLSearchParams#keys iterators.
76-
for (const key of keysIterable) {
77-
if (!validateQualifierKey(key, throws)) {
78-
return false
79-
}
80-
}
81-
return true
82-
}
83-
8454
/**
8555
* Validate qualifier key format and characters.
8656
* @throws {PurlError} When validation fails and throws is true.
@@ -122,6 +92,36 @@ function validateQualifierKey(key: string, throws: boolean): boolean {
12292
return true
12393
}
12494

95+
/**
96+
* Validate qualifiers object structure and keys.
97+
* @throws {PurlError} When validation fails and throws is true.
98+
*/
99+
function validateQualifiers(qualifiers: unknown, throws: boolean): boolean {
100+
if (qualifiers === null || qualifiers === undefined) {
101+
return true
102+
}
103+
if (typeof qualifiers !== 'object') {
104+
if (throws) {
105+
throw new PurlError('"qualifiers" must be an object')
106+
}
107+
return false
108+
}
109+
const qualifiersObj = qualifiers as QualifiersObject | URLSearchParams
110+
const keysProperty = (qualifiersObj as QualifiersObject)['keys']
111+
const keysIterable: Iterable<string> =
112+
// URLSearchParams instances have a "keys" method that returns an iterator.
113+
typeof keysProperty === 'function'
114+
? (ReflectApply(keysProperty, qualifiersObj, []) as Iterable<string>)
115+
: (Object.keys(qualifiers as QualifiersObject) as Iterable<string>)
116+
// Use for-of to work with URLSearchParams#keys iterators.
117+
for (const key of keysIterable) {
118+
if (!validateQualifierKey(key, throws)) {
119+
return false
120+
}
121+
}
122+
return true
123+
}
124+
125125
/**
126126
* Validate that component is present and not empty.
127127
* @throws {PurlError} When validation fails and throws is true.

0 commit comments

Comments
 (0)