Skip to content

Commit

Permalink
fix(toJSON): handle array properties properly
Browse files Browse the repository at this point in the history
  • Loading branch information
nokome committed Oct 28, 2018
1 parent 34258d6 commit 02dd168
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/Thing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ export default class Thing {

let id = Reflect.getMetadata('property:id', this, key)
let [context, term] = id.split(':')
if (value instanceof Thing) {
if (Array.isArray(value)) {
jsonld[term] = value.map(item => (item instanceof Thing) ? item.toJSONLD(false) : value)
} else if (value instanceof Thing) {
jsonld[term] = value.toJSONLD(false)
} else {
jsonld[term] = value
Expand Down
29 changes: 24 additions & 5 deletions tests/SoftwarePackage.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
import SoftwarePackage from '../src/SoftwarePackage'

describe('SoftwarePackage', () => {
const node = new SoftwarePackage()
test('type', () => {
const pkg = new SoftwarePackage()
expect(pkg.type).toEqual('SoftwarePackage')
})

test('type', () => {
expect(node.type).toEqual('SoftwarePackage')
test('toJSON', () => {
const pkg = new SoftwarePackage()
pkg.name = 'My package'
pkg.softwareRequirements = [
new SoftwarePackage({ name: 'another'}),
new SoftwarePackage({ name: 'yetAnother'})
]
expect(pkg.toJSONLD()).toEqual({
"@context": "https://stencila.github.io/schema/context.jsonld",
"type": "SoftwarePackage",
"name": "My package",
"softwareRequirements": [
{
"type": "SoftwarePackage",
"name": "another"
}, {
"type": "SoftwarePackage",
"name": "yetAnother"
}
]
})

})

0 comments on commit 02dd168

Please sign in to comment.