Skip to content

Commit

Permalink
combine namespace and key in readObject
Browse files Browse the repository at this point in the history
  • Loading branch information
juunini committed Oct 12, 2023
1 parent 315dce1 commit 490f5e1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 36 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cloudmatelabs/jsonld-helper",
"version": "1.2.0",
"version": "1.2.1",
"description": "JSON-LD Helper",
"homepage": "https://github.com/cloudmatelabs/jsonld-helper-ts",
"repository": "cloudmatelabs/jsonld-helper-ts",
Expand Down
63 changes: 28 additions & 35 deletions src/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,29 +67,9 @@ export class JsonLDReader {
*/
public read (namespace: string, key: string): JsonLDReader
public read (namespaceOrKey: string | number, key?: string): JsonLDReader {
const _key = this.key(namespaceOrKey, key)

return typeof _key === 'number'
? this.readArray(_key)
: this.readObject(_key)
}

private key (namespace: string | number, key?: string): string | number {
if (key === undefined) {
const [key, isPreDefined] = this.preDefinedKey(namespace)
if (isPreDefined) {
return key
}

return key
}

const [_key, isPreDefined] = this.preDefinedKey(key)
if (isPreDefined) {
return _key
}

return `${this.namespace[namespace] ?? namespace}#${_key}`
return typeof namespaceOrKey === 'number'
? this.readArray(namespaceOrKey)
: this.readObject(namespaceOrKey, key)
}

private preDefinedKey (key: string | number): [string | number, boolean] {
Expand Down Expand Up @@ -223,30 +203,43 @@ export class JsonLDReader {
return JsonLDReader.of(this.value[key], this.namespace)
}

private readObject (key: string): JsonLDReader {
private readObject (namespace: string, key?: string): JsonLDReader {
if (!this.valueIsObject()) {
return new Nothing(new Error('Not an object'))
}

const scope = this.scope() as Record<string, any>
const value = scope[key]

if (value !== undefined) {
return key === '@type'
? JsonLDReader.of(this.extractType(value), this.namespace)
: JsonLDReader.of(value, this.namespace)
const [preDefinedKey, isPreDefined] = this.preDefinedKey(key ?? namespace)

if (isPreDefined) {
const value = scope[preDefinedKey]

if (value !== undefined) {
return JsonLDReader.of(
preDefinedKey === '@type' ? this.extractType(value) : value,
this.namespace
)
}
}

const _key = this.unsetPreDefinedKey(key)
const extractedKey = Object.keys(scope).find((k) => k.split('#')[1] === _key)
const combinedKey: string | undefined = this.key(namespace, key)

if (extractedKey === undefined) {
return new Nothing(new Error(`Not found key: ${key}`))
if (combinedKey === undefined) {
return new Nothing(new Error(`Not found key: ${key ?? namespace}`))
}

return key === 'type'
? JsonLDReader.of(this.extractType(scope[extractedKey]), this.namespace)
: JsonLDReader.of(scope[extractedKey], this.namespace)
? JsonLDReader.of(this.extractType(scope[combinedKey]), this.namespace)
: JsonLDReader.of(scope[combinedKey], this.namespace)
}

private key (namespace: string, key?: string): string | undefined {
if (key !== undefined) {
return `${this.namespace[namespace] ?? namespace}#${key}`
}

return Object.keys(this.scope() as Record<string, string>).find((k) => k.split('#')[1] === this.unsetPreDefinedKey(namespace))
}

private unsetPreDefinedKey (key: string): string {
Expand Down

0 comments on commit 490f5e1

Please sign in to comment.