Skip to content

Commit 50a6694

Browse files
authored
add getters for construtor parameters (#145)
Signed-off-by: Jan Kowalleck <[email protected]>
1 parent 68bd251 commit 50a6694

17 files changed

+322
-5
lines changed

HISTORY.md

+13
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@ All notable changes to this project will be documented in this file.
44

55
## unreleased
66

7+
* Added
8+
* Getters/properties that represent the corresponding parameters of class constructor. (via [#145])
9+
* `Builders.FromPackageJson.ComponentBuilder.extRefFactory`,
10+
`Builders.FromPackageJson.ComponentBuilder.licenseFactory`
11+
* `Builders.FromPackageJson.ToolBuilder.extRefFactory`
12+
* `Factories.PackageUrlFactory.type`
13+
* `Serialize.BomRefDiscriminator.prefix`
14+
* `Serialize.JsonSerializer.normalizerFactory`
15+
* `Serialize.XmlBaseSerializer.normalizerFactory`,
16+
`Serialize.XmlSerializer.normalizerFactory`
17+
18+
[#145]: https://github.com/CycloneDX/cyclonedx-javascript-library/pull/145
19+
720
## 1.1.0 - 2022-07-29
821

922
* Added

src/builders/fromPackageJson.node.ts

+12
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ export class ToolBuilder {
3333
this.#extRefFactory = extRefFactory
3434
}
3535

36+
get extRefFactory (): Factories.FromPackageJson.ExternalReferenceFactory {
37+
return this.#extRefFactory
38+
}
39+
3640
makeTool (data: PackageJson): Models.Tool | undefined {
3741
const [name, vendor] = typeof data.name === 'string'
3842
? splitNameGroup(data.name)
@@ -61,6 +65,14 @@ export class ComponentBuilder {
6165
this.#licenseFactory = licenseFactory
6266
}
6367

68+
get extRefFactory (): Factories.FromPackageJson.ExternalReferenceFactory {
69+
return this.#extRefFactory
70+
}
71+
72+
get licenseFactory (): Factories.LicenseFactory {
73+
return this.#licenseFactory
74+
}
75+
6476
makeComponent (data: PackageJson, type: Enums.ComponentType = Enums.ComponentType.Library): Models.Component | undefined {
6577
if (typeof data.name !== 'string') {
6678
return undefined

src/factories/packageUrl.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,19 @@ import { PackageURL } from 'packageurl-js'
2222
import { ExternalReferenceType } from '../enums'
2323

2424
export class PackageUrlFactory {
25-
readonly #type: string
25+
readonly #type: PackageURL['type']
2626

2727
constructor (type: PackageURL['type']) {
2828
this.#type = type
2929
}
3030

31+
get type (): PackageURL['type'] {
32+
return this.#type
33+
}
34+
3135
makeFromComponent (component: Component): PackageURL | undefined {
32-
const qualifiers: { [key: string]: string } = {}
33-
let subpath: string | undefined
36+
const qualifiers: PackageURL['qualifiers'] = {}
37+
let subpath: PackageURL['subpath']
3438

3539
for (const e of component.externalReferences) {
3640
if (e.type === ExternalReferenceType.VCS) {

src/serialize/bomRefDiscriminator.ts

+5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ export class BomRefDiscriminator {
3131
this.#prefix = prefix
3232
}
3333

34+
get prefix (): string {
35+
return this.#prefix
36+
}
37+
38+
/** Iterate over the bomRefs. */
3439
[Symbol.iterator] (): IterableIterator<BomRef> {
3540
return this.#originalValues.keys()
3641
}

src/serialize/json/normalize.ts

+4
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ abstract class Base implements Normalizer {
104104
this._factory = factory
105105
}
106106

107+
get factory (): Factory {
108+
return this._factory
109+
}
110+
107111
abstract normalize (data: object, options: NormalizerOptions): object | undefined
108112
}
109113

src/serialize/jsonSerializer.ts

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ export class JsonSerializer extends BaseSerializer<Normalized.Bom> {
4242
this.#normalizerFactory = normalizerFactory
4343
}
4444

45+
get normalizerFactory (): NormalizerFactory {
46+
return this.#normalizerFactory
47+
}
48+
4549
protected _normalize (
4650
bom: Bom,
4751
options: NormalizerOptions = {}

src/serialize/xml/normalize.ts

+4
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ abstract class Base implements Normalizer {
104104
this._factory = factory
105105
}
106106

107+
get factory (): Factory {
108+
return this._factory
109+
}
110+
107111
/**
108112
* @param {*} data
109113
* @param {NormalizerOptions} options

src/serialize/xmlBaseSerializer.ts

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ export abstract class XmlBaseSerializer extends BaseSerializer<SimpleXml.Element
4242
this.#normalizerFactory = normalizerFactory
4343
}
4444

45+
get normalizerFactory (): NormalizerFactory {
46+
return this.#normalizerFactory
47+
}
48+
4549
protected _normalize (
4650
bom: Bom,
4751
options: NormalizerOptions = {}

tests/_helpers/stringFunctions.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,12 @@ module.exports = {
3434
upperCamelCase: s => s.replace(
3535
/\b\w/g,
3636
f => f.slice(-1).toUpperCase()
37-
).replace(/\W/g, '')
37+
).replace(/\W/g, ''),
38+
39+
/**
40+
* Generate a random string of length.
41+
* @param {number} length
42+
* @return {string}
43+
*/
44+
randomString: length => Math.random().toString(32).substring(2, 2 + length)
3845
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict'
2+
/*!
3+
This file is part of CycloneDX JavaScript Library.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
17+
SPDX-License-Identifier: Apache-2.0
18+
Copyright (c) OWASP Foundation. All Rights Reserved.
19+
*/
20+
21+
const assert = require('assert')
22+
const { suite, test } = require('mocha')
23+
24+
const {
25+
Builders: { FromPackageJson: { ComponentBuilder } },
26+
Factories: {
27+
FromPackageJson: { ExternalReferenceFactory },
28+
LicenseFactory
29+
}
30+
} = require('../../')
31+
32+
suite('Builders.FromPackageJson.ToolBuilder', () => {
33+
test('construct', () => {
34+
const extRefFactory = new ExternalReferenceFactory()
35+
const licenseFactory = new LicenseFactory()
36+
37+
const actual = new ComponentBuilder(extRefFactory, licenseFactory)
38+
39+
assert.strictEqual(actual.extRefFactory, extRefFactory)
40+
assert.strictEqual(actual.licenseFactory, licenseFactory)
41+
})
42+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict'
2+
/*!
3+
This file is part of CycloneDX JavaScript Library.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
17+
SPDX-License-Identifier: Apache-2.0
18+
Copyright (c) OWASP Foundation. All Rights Reserved.
19+
*/
20+
21+
const assert = require('assert')
22+
const { suite, test } = require('mocha')
23+
24+
const {
25+
Builders: { FromPackageJson: { ToolBuilder } },
26+
Factories: { FromPackageJson: { ExternalReferenceFactory } }
27+
} = require('../../')
28+
29+
suite('Builders.FromPackageJson.ToolBuilder', () => {
30+
test('construct', () => {
31+
const extRefFactory = new ExternalReferenceFactory()
32+
33+
const actual = new ToolBuilder(extRefFactory)
34+
35+
assert.strictEqual(actual.extRefFactory, extRefFactory)
36+
})
37+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict'
2+
/*!
3+
This file is part of CycloneDX JavaScript Library.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
17+
SPDX-License-Identifier: Apache-2.0
18+
Copyright (c) OWASP Foundation. All Rights Reserved.
19+
*/
20+
21+
const assert = require('assert')
22+
const { suite, test } = require('mocha')
23+
24+
const {
25+
Factories: { PackageUrlFactory }
26+
} = require('../../')
27+
28+
const { randomString } = require('../_helpers/stringFunctions')
29+
30+
suite('Builders.FromPackageJson.ToolBuilder', () => {
31+
test('construct', () => {
32+
const type = randomString(5)
33+
34+
const actual = new PackageUrlFactory(type)
35+
36+
assert.strictEqual(actual.type, type)
37+
})
38+
})

tests/unit/Serialize.BomRefDiscriminator.spec.js

+18
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,25 @@ const {
2626
Serialize: { BomRefDiscriminator }
2727
} = require('../../')
2828

29+
const { randomString } = require('../_helpers/stringFunctions')
30+
2931
suite('Serialize.BomRefDiscriminator', () => {
32+
test('constructor', () => {
33+
const bomRef1 = new BomRef()
34+
const bomRef2 = new BomRef('foo')
35+
const prefix = randomString(10)
36+
37+
/* eslint-disable-next-line no-unused-vars */
38+
const actual = new BomRefDiscriminator([bomRef1, bomRef2], prefix)
39+
40+
assert.strictEqual(actual.prefix, prefix)
41+
42+
const actualBomRefs = Array.from(actual) // convert the iterator to a list
43+
assert.strictEqual(actualBomRefs.length, 2)
44+
assert.strictEqual(actualBomRefs.includes(bomRef1), true)
45+
assert.strictEqual(actualBomRefs.includes(bomRef2), true)
46+
})
47+
3048
test('does not alter BomRef.value unintended', () => {
3149
const bomRef1 = new BomRef()
3250
const bomRef2 = new BomRef('foo')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict'
2+
/*!
3+
This file is part of CycloneDX JavaScript Library.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
17+
SPDX-License-Identifier: Apache-2.0
18+
Copyright (c) OWASP Foundation. All Rights Reserved.
19+
*/
20+
21+
const assert = require('assert')
22+
const { suite, test } = require('mocha')
23+
24+
const {
25+
Serialize: {
26+
JsonSerializer,
27+
JSON: { Normalize: { Factory } }
28+
},
29+
Spec: { Spec1dot4 }
30+
31+
} = require('../../')
32+
33+
suite('Serialize.JsonSerializer', () => {
34+
test('constructor', () => {
35+
const normalizerFactory = new Factory(Spec1dot4)
36+
37+
const actual = new JsonSerializer(normalizerFactory)
38+
39+
assert.strictEqual(actual.normalizerFactory, normalizerFactory)
40+
})
41+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict'
2+
/*!
3+
This file is part of CycloneDX JavaScript Library.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
17+
SPDX-License-Identifier: Apache-2.0
18+
Copyright (c) OWASP Foundation. All Rights Reserved.
19+
*/
20+
21+
const assert = require('assert')
22+
const { suite, test } = require('mocha')
23+
24+
const {
25+
Serialize: {
26+
XmlBaseSerializer,
27+
XML: { Normalize: { Factory } }
28+
},
29+
Spec: { Spec1dot4 }
30+
31+
} = require('../../')
32+
33+
suite('Serialize.XmlBaseSerializer', () => {
34+
test('constructor', () => {
35+
const normalizerFactory = new Factory(Spec1dot4)
36+
37+
class MySerializer extends XmlBaseSerializer {}
38+
39+
const actual = new MySerializer(normalizerFactory)
40+
41+
assert.strictEqual(actual.normalizerFactory, normalizerFactory)
42+
})
43+
})

0 commit comments

Comments
 (0)