Skip to content

Commit 3b517b7

Browse files
committed
Use dot notation for property access instead of bracket notation
TypeScript correctly handles type safety with dot notation for known properties. Bracket notation was unnecessarily verbose and reduced code readability.
1 parent 7f1fbf8 commit 3b517b7

File tree

4 files changed

+40
-40
lines changed

4 files changed

+40
-40
lines changed

src/normalize.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ function qualifiersToEntries(
134134
if (isObject(rawQualifiers)) {
135135
// URLSearchParams instances have an "entries" method that returns an iterator
136136
const rawQualifiersObj = rawQualifiers as QualifiersObject | URLSearchParams
137-
const entriesProperty = (rawQualifiersObj as QualifiersObject)['entries']
137+
const entriesProperty = (rawQualifiersObj as QualifiersObject).entries
138138
return typeof entriesProperty === 'function'
139139
? (ReflectApply(entriesProperty, rawQualifiersObj, []) as Iterable<
140140
[string, string]

src/package-url.ts

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2020
SOFTWARE.
2121
*/
2222

23+
import { decodePurlComponent } from './decode.js'
24+
import { PurlError } from './error.js'
25+
import { isObject, recursiveFreeze } from './objects.js'
2326
/**
2427
* @fileoverview Package URL parsing and construction utilities.
2528
*
@@ -28,9 +31,6 @@ SOFTWARE.
2831
* instanceof checks may fail due to module system interoperability issues.
2932
* See package-url-builder.ts for detailed explanation and workarounds.
3033
*/
31-
import { decodePurlComponent } from './decode.js'
32-
import { PurlError } from './error.js'
33-
import { isObject, recursiveFreeze } from './objects.js'
3434
import { PurlComponent } from './purl-component.js'
3535
import { PurlQualifierNames } from './purl-qualifier-names.js'
3636
import { PurlType } from './purl-type.js'
@@ -99,47 +99,47 @@ class PackageURL {
9999
rawSubpath: unknown,
100100
) {
101101
const type = isNonEmptyString(rawType)
102-
? (PurlComponent['type']?.['normalize'] as ComponentNormalizer)?.(rawType)
102+
? (PurlComponent.type?.normalize as ComponentNormalizer)?.(rawType)
103103
: rawType
104-
;(PurlComponent['type']?.['validate'] as ComponentValidator)?.(type, true)
104+
;(PurlComponent.type?.validate as ComponentValidator)?.(type, true)
105105

106106
const namespace = isNonEmptyString(rawNamespace)
107-
? (PurlComponent['namespace']?.['normalize'] as ComponentNormalizer)?.(
107+
? (PurlComponent.namespace?.normalize as ComponentNormalizer)?.(
108108
rawNamespace,
109109
)
110110
: rawNamespace
111-
;(PurlComponent['namespace']?.['validate'] as ComponentValidator)?.(
111+
;(PurlComponent.namespace?.validate as ComponentValidator)?.(
112112
namespace,
113113
true,
114114
)
115115

116116
const name = isNonEmptyString(rawName)
117-
? (PurlComponent['name']?.['normalize'] as ComponentNormalizer)?.(rawName)
117+
? (PurlComponent.name?.normalize as ComponentNormalizer)?.(rawName)
118118
: rawName
119-
;(PurlComponent['name']?.['validate'] as ComponentValidator)?.(name, true)
119+
;(PurlComponent.name?.validate as ComponentValidator)?.(name, true)
120120

121121
const version = isNonEmptyString(rawVersion)
122-
? (PurlComponent['version']?.['normalize'] as ComponentNormalizer)?.(rawVersion)
122+
? (PurlComponent.version?.normalize as ComponentNormalizer)?.(rawVersion)
123123
: rawVersion
124-
;(PurlComponent['version']?.['validate'] as ComponentValidator)?.(version, true)
124+
;(PurlComponent.version?.validate as ComponentValidator)?.(version, true)
125125

126126
const qualifiers =
127127
typeof rawQualifiers === 'string' || isObject(rawQualifiers)
128128
? (
129-
PurlComponent['qualifiers']?.['normalize'] as (
129+
PurlComponent.qualifiers?.normalize as (
130130
_value: string | QualifiersObject,
131131
) => Record<string, string> | undefined
132132
)?.(rawQualifiers as string | QualifiersObject)
133133
: rawQualifiers
134-
;(PurlComponent['qualifiers']?.['validate'] as ComponentValidator)?.(
134+
;(PurlComponent.qualifiers?.validate as ComponentValidator)?.(
135135
qualifiers,
136136
true,
137137
)
138138

139139
const subpath = isNonEmptyString(rawSubpath)
140-
? (PurlComponent['subpath']?.['normalize'] as ComponentNormalizer)?.(rawSubpath)
140+
? (PurlComponent.subpath?.normalize as ComponentNormalizer)?.(rawSubpath)
141141
: rawSubpath
142-
;(PurlComponent['subpath']?.['validate'] as ComponentValidator)?.(subpath, true)
142+
;(PurlComponent.subpath?.validate as ComponentValidator)?.(subpath, true)
143143

144144
this.type = type as string
145145
this.name = name as string
@@ -156,9 +156,9 @@ class PackageURL {
156156

157157
const typeHelpers = PurlType[type as string]
158158
if (typeHelpers) {
159-
;(typeHelpers?.['normalize'] as (_purl: PackageURL) => void)?.(this)
159+
;(typeHelpers?.normalize as (_purl: PackageURL) => void)?.(this)
160160
;(
161-
typeHelpers?.['validate'] as (
161+
typeHelpers?.validate as (
162162
_purl: PackageURL,
163163
_throws: boolean,
164164
) => boolean
@@ -222,19 +222,19 @@ class PackageURL {
222222
type?: string | undefined
223223
version?: string | undefined
224224
} = this
225-
/* c8 ignore next - Type encoder uses default PurlComponentEncoder, never returns null/undefined. */ let purlStr = `pkg:${(PurlComponent['type']?.['encode'] as ComponentEncoder)?.(type) ?? ''}/`
225+
/* c8 ignore next - Type encoder uses default PurlComponentEncoder, never returns null/undefined. */ let purlStr = `pkg:${(PurlComponent.type?.encode as ComponentEncoder)?.(type) ?? ''}/`
226226
if (namespace) {
227-
/* c8 ignore next - Namespace encoder always returns string, never null/undefined. */ purlStr = `${purlStr}${(PurlComponent['namespace']?.['encode'] as ComponentEncoder)?.(namespace) ?? ''}/`
227+
/* c8 ignore next - Namespace encoder always returns string, never null/undefined. */ purlStr = `${purlStr}${(PurlComponent.namespace?.encode as ComponentEncoder)?.(namespace) ?? ''}/`
228228
}
229-
/* c8 ignore next - Name encoder always returns string, never null/undefined. */ purlStr = `${purlStr}${(PurlComponent['name']?.['encode'] as ComponentEncoder)?.(name) ?? ''}`
229+
/* c8 ignore next - Name encoder always returns string, never null/undefined. */ purlStr = `${purlStr}${(PurlComponent.name?.encode as ComponentEncoder)?.(name) ?? ''}`
230230
if (version) {
231-
/* c8 ignore next - Version encoder always returns string, never null/undefined. */ purlStr = `${purlStr}@${(PurlComponent['version']?.['encode'] as ComponentEncoder)?.(version) ?? ''}`
231+
/* c8 ignore next - Version encoder always returns string, never null/undefined. */ purlStr = `${purlStr}@${(PurlComponent.version?.encode as ComponentEncoder)?.(version) ?? ''}`
232232
}
233233
if (qualifiers) {
234-
/* c8 ignore next - Qualifiers encoder always returns string, never null/undefined. */ purlStr = `${purlStr}?${(PurlComponent['qualifiers']?.['encode'] as ComponentEncoder)?.(qualifiers) ?? ''}`
234+
/* c8 ignore next - Qualifiers encoder always returns string, never null/undefined. */ purlStr = `${purlStr}?${(PurlComponent.qualifiers?.encode as ComponentEncoder)?.(qualifiers) ?? ''}`
235235
}
236236
if (subpath) {
237-
/* c8 ignore next - Subpath encoder always returns string, never null/undefined. */ purlStr = `${purlStr}#${(PurlComponent['subpath']?.['encode'] as ComponentEncoder)?.(subpath) ?? ''}`
237+
/* c8 ignore next - Subpath encoder always returns string, never null/undefined. */ purlStr = `${purlStr}#${(PurlComponent.subpath?.encode as ComponentEncoder)?.(subpath) ?? ''}`
238238
}
239239
return purlStr
240240
}
@@ -278,12 +278,12 @@ class PackageURL {
278278
// Create a safe object without prototype chain to prevent prototype pollution
279279
const safeObject: PackageURLObject = {
280280
__proto__: null,
281-
type: parsedRecord['type'] as string | undefined,
282-
namespace: parsedRecord['namespace'] as string | undefined,
283-
name: parsedRecord['name'] as string | undefined,
284-
version: parsedRecord['version'] as string | undefined,
285-
qualifiers: parsedRecord['qualifiers'] as Record<string, string> | undefined,
286-
subpath: parsedRecord['subpath'] as string | undefined,
281+
type: parsedRecord.type as string | undefined,
282+
namespace: parsedRecord.namespace as string | undefined,
283+
name: parsedRecord.name as string | undefined,
284+
version: parsedRecord.version as string | undefined,
285+
qualifiers: parsedRecord.qualifiers as Record<string, string> | undefined,
286+
subpath: parsedRecord.subpath as string | undefined,
287287
} as PackageURLObject
288288

289289
return PackageURL.fromObject(safeObject)
@@ -298,12 +298,12 @@ class PackageURL {
298298
}
299299
const typedObj = obj as Record<string, unknown>
300300
return new PackageURL(
301-
typedObj['type'],
302-
typedObj['namespace'],
303-
typedObj['name'],
304-
typedObj['version'],
305-
typedObj['qualifiers'],
306-
typedObj['subpath'],
301+
typedObj.type,
302+
typedObj.namespace,
303+
typedObj.name,
304+
typedObj.version,
305+
typedObj.qualifiers,
306+
typedObj.subpath,
307307
)
308308
}
309309

src/purl-type.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ const PurlType = createHelpersNamespaceObject(
224224
},
225225
// https://github.com/package-url/purl-spec/blob/master/PURL-TYPES.rst#mlflow
226226
mlflow(purl: PurlObject) {
227-
if (purl.qualifiers?.['repository_url']?.includes('databricks')) {
227+
if (purl.qualifiers?.repository_url?.includes('databricks')) {
228228
lowerName(purl)
229229
}
230230
return purl
@@ -306,7 +306,7 @@ const PurlType = createHelpersNamespaceObject(
306306
// https://github.com/package-url/purl-spec/blob/master/PURL-TYPES.rst#conan
307307
conan(purl: PurlObject, throws: boolean) {
308308
if (isNullishOrEmptyString(purl.namespace)) {
309-
if (purl.qualifiers?.['channel']) {
309+
if (purl.qualifiers?.channel) {
310310
if (throws) {
311311
throw new PurlError(
312312
'conan requires a "namespace" component when a "channel" qualifier is present',
@@ -542,7 +542,7 @@ const PurlType = createHelpersNamespaceObject(
542542
swid(purl: PurlObject, throws: boolean) {
543543
const { qualifiers } = purl
544544
// SWID requires a tag_id qualifier
545-
const tagId = qualifiers?.['tag_id']
545+
const tagId = qualifiers?.tag_id
546546
if (!tagId) {
547547
if (throws) {
548548
throw new PurlError('swid requires a "tag_id" qualifier')

src/validate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ function validateQualifiers(
170170
return false
171171
}
172172
const qualifiersObj = qualifiers as QualifiersObject | URLSearchParams
173-
const keysProperty = (qualifiersObj as QualifiersObject)['keys']
173+
const keysProperty = (qualifiersObj as QualifiersObject).keys
174174
// type-coverage:ignore-next-line -- TypeScript correctly infers this type through the ternary and cast
175175
const keysIterable: Iterable<string> =
176176
// URLSearchParams instances have a "keys" method that returns an iterator

0 commit comments

Comments
 (0)