Skip to content

Commit d6197b3

Browse files
authored
refactor: optimize maps (#997)
--------- Signed-off-by: Jan Kowalleck <[email protected]>
1 parent 27aa038 commit d6197b3

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

src/serialize/bomRefDiscriminator.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,24 @@ Copyright (c) OWASP Foundation. All Rights Reserved.
2020
import type { BomRef } from '../models'
2121

2222
export class BomRefDiscriminator {
23-
readonly #originalValues: ReadonlyMap<BomRef, string | undefined>
23+
readonly #originalValues: ReadonlyArray<readonly [BomRef, string | undefined]>
2424

2525
readonly #prefix: string
2626

2727
constructor (bomRefs: Iterable<BomRef>, prefix: string = 'BomRef') {
28-
this.#originalValues = new Map(
29-
Array.from(bomRefs, r => [r, r.value])
30-
)
28+
this.#originalValues = Array.from(bomRefs, r => [r, r.value])
3129
this.#prefix = prefix
3230
}
3331

3432
get prefix (): string {
3533
return this.#prefix
3634
}
3735

38-
/** Iterate over the bomRefs. */
39-
[Symbol.iterator] (): IterableIterator<BomRef> {
40-
return this.#originalValues.keys()
36+
/** Iterate over the {@link BomRef}s. */
37+
* [Symbol.iterator] (): IterableIterator<BomRef> {
38+
for (const [bomRef] of this.#originalValues) {
39+
yield bomRef
40+
}
4141
}
4242

4343
discriminate (): void {

src/spdx.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Copyright (c) OWASP Foundation. All Rights Reserved.
2020
import * as spdxExpressionParse from 'spdx-expression-parse'
2121

2222
/* @ts-expect-error: TS6059 -- this works as long as the file/path is available in dist-package. */
23-
import { enum as _spdxSpecEnum } from '../res/schema/spdx.SNAPSHOT.schema.json' assert { type: 'json' }
23+
import { enum as _spdxSpecEnum } from '../res/schema/spdx.SNAPSHOT.schema.json' assert {type: 'json'}
2424

2525
/**
2626
* One of the known SPDX licence identifiers.
@@ -33,9 +33,9 @@ export type SpdxId = string
3333

3434
const spdxIds: ReadonlySet<SpdxId> = new Set(_spdxSpecEnum)
3535

36-
const spdxLowerToActual: ReadonlyMap<string, SpdxId> = new Map(
36+
const spdxLowerToActual: Readonly<Record<string, SpdxId>> = Object.freeze(Object.fromEntries(
3737
_spdxSpecEnum.map(spdxId => [spdxId.toLowerCase(), spdxId])
38-
)
38+
))
3939

4040
export function isSupportedSpdxId (value: SpdxId | any): value is SpdxId {
4141
return spdxIds.has(value)
@@ -44,7 +44,7 @@ export function isSupportedSpdxId (value: SpdxId | any): value is SpdxId {
4444
/** Try to convert a `string`-like to a valid/known {@link SpdxId}. */
4545
export function fixupSpdxId (value: string | any): SpdxId | undefined {
4646
return typeof value === 'string' && value.length > 0
47-
? spdxLowerToActual.get(value.toLowerCase())
47+
? spdxLowerToActual[value.toLowerCase()]
4848
: undefined
4949
}
5050

0 commit comments

Comments
 (0)