Skip to content

Commit f1a2b6a

Browse files
committed
Add documentation and reorder methods alphabetically in purl-type.ts
Improves code organization and documentation: - Add JSDoc comments for PurlTypNormalizer and PurlTypeValidator constants - Reorder helper functions alphabetically (create*, getNpm*, isNpm*) The alphabetic ordering groups related functions together and makes them easier to locate when navigating the file.
1 parent 3891579 commit f1a2b6a

File tree

1 file changed

+38
-31
lines changed

1 file changed

+38
-31
lines changed

src/purl-type.ts

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,39 @@ interface PurlObject {
2525
version?: string
2626
}
2727

28+
/**
29+
* Default normalizer for PURL types without specific normalization rules.
30+
*/
2831
const PurlTypNormalizer = (purl: PurlObject): PurlObject => purl
32+
33+
/**
34+
* Default validator for PURL types without specific validation rules.
35+
*/
2936
const PurlTypeValidator = (_purl: PurlObject, _throws: boolean): boolean => true
3037

38+
/**
39+
* Create normalizer that only lowercases name.
40+
*/
41+
function createLowerNameNormalizer(): (_purl: PurlObject) => PurlObject {
42+
return (purl: PurlObject) => {
43+
lowerName(purl)
44+
return purl
45+
}
46+
}
47+
48+
/**
49+
* Create normalizer that lowercases both namespace and name.
50+
*/
51+
function createLowerNamespaceAndNameNormalizer(): (
52+
_purl: PurlObject,
53+
) => PurlObject {
54+
return (purl: PurlObject) => {
55+
lowerNamespace(purl)
56+
lowerName(purl)
57+
return purl
58+
}
59+
}
60+
3161
/**
3262
* Get list of Node.js built-in module names.
3363
*/
@@ -94,6 +124,14 @@ const getNpmBuiltinNames = (() => {
94124
}
95125
})()
96126

127+
/**
128+
* Get npm package identifier with optional namespace.
129+
*/
130+
function getNpmId(purl: PurlObject): string {
131+
const { name, namespace } = purl
132+
return `${namespace && namespace.length > 0 ? `${namespace}/` : ''}${name}`
133+
}
134+
97135
/**
98136
* Get list of npm legacy package names.
99137
*/
@@ -127,14 +165,6 @@ const getNpmLegacyNames = (() => {
127165
}
128166
})()
129167

130-
/**
131-
* Get npm package identifier with optional namespace.
132-
*/
133-
function getNpmId(purl: PurlObject): string {
134-
const { name, namespace } = purl
135-
return `${namespace && namespace.length > 0 ? `${namespace}/` : ''}${name}`
136-
}
137-
138168
/**
139169
* Check if npm identifier is a built-in module name.
140170
*/
@@ -147,29 +177,6 @@ const isNpmBuiltinName = (id: string): boolean =>
147177
const isNpmLegacyName = (id: string): boolean =>
148178
getNpmLegacyNames().includes(id)
149179

150-
/**
151-
* Create normalizer that lowercases both namespace and name.
152-
*/
153-
function createLowerNamespaceAndNameNormalizer(): (
154-
_purl: PurlObject,
155-
) => PurlObject {
156-
return (purl: PurlObject) => {
157-
lowerNamespace(purl)
158-
lowerName(purl)
159-
return purl
160-
}
161-
}
162-
163-
/**
164-
* Create normalizer that only lowercases name.
165-
*/
166-
function createLowerNameNormalizer(): (_purl: PurlObject) => PurlObject {
167-
return (purl: PurlObject) => {
168-
lowerName(purl)
169-
return purl
170-
}
171-
}
172-
173180
// PURL types:
174181
// https://github.com/package-url/purl-spec/blob/master/PURL-TYPES.rst
175182
const PurlType = createHelpersNamespaceObject(

0 commit comments

Comments
 (0)