deno.land / x / shacl_to_type@v0.0.9 / frontend / responseToObjects.ts

responseToObjects.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { Context } from './Context.ts'import { RdfJsonRoot, Meta } from './types.ts'import { castValue } from './castValue.ts'
const subjectIRI = 'http://subject.com/subject'
export const responseToObjects = <Type>(results: RdfJsonRoot, context: Context, meta: Meta) => { const objects: Array<{ [key: string]: any }> = [] const typePredicate = context.compactIri('http://www.w3.org/1999/02/22-rdf-syntax-ns#type')
for (const uri of Object.keys(results)) { const object: { [key: string]: any } = { id: context.compactIri(uri) } objects.push(object) }
for (const [uri, predicates] of Object.entries(results)) { const object = objects.find(object => object.id === context.compactIri(uri))!
const type = predicates["http://www.w3.org/1999/02/22-rdf-syntax-ns#type"].find(item => item.value !== subjectIRI)!.value const compactedType = context.compactIri(type)
for (const [predicate, values] of Object.entries(predicates)) { const compactedPredicate = context.compactIri(predicate, true) const propertyConfig = meta[compactedType][compactedPredicate]
object[compactedPredicate] = propertyConfig.singular ? castValue(values[0], propertyConfig, context) : values.map(term => { if (term.type === 'uri' && compactedPredicate !== typePredicate) { const match = objects.find(object => object.id === context.compactIri(term.value)) if (match) return match } return castValue(term, propertyConfig, context) }).filter(Boolean) } }
const output: Array<any> = objects.filter(object => object[typePredicate]?.includes(subjectIRI)) as unknown as Array<Type>
for (const item of output) { item[typePredicate] = item[typePredicate].filter((item: any) => item !== subjectIRI) }
return output}
shacl_to_type

Version Info

Tagged at
a year ago