* Parses a JSON-AD Value. If it's a string, it takes its URL. If it's an * Object, it will parse it as a Resource. It will add the string property to * the Resource.
( store: Store, value: JSONValue, resource: Resource, key: string, )
| 77 | * the Resource. |
| 78 | */ |
| 79 | function parseJsonAdResourceValue( |
| 80 | store: Store, |
| 81 | value: JSONValue, |
| 82 | resource: Resource, |
| 83 | key: string, |
| 84 | ): StringOrNestedResource { |
| 85 | if (typeof value === 'string') { |
| 86 | return value; |
| 87 | } |
| 88 | if (value.constructor === {}.constructor) { |
| 89 | if (Object.keys(value).includes('@id')) { |
| 90 | // It's a named resource that needs to be put in the store |
| 91 | const nestedSubject = value['@id']; |
| 92 | const nestedResource = new Resource(nestedSubject); |
| 93 | parseJsonADResource(value as JSONObject, nestedResource, store); |
| 94 | return nestedSubject; |
| 95 | } else { |
| 96 | // It's an anonymous nested Resource |
| 97 | return value as JSONObject; |
| 98 | } |
| 99 | } |
| 100 | throw new Error(`Value ${value} in ${key} not a string or a nested Resource`); |
| 101 | } |
| 102 | |
| 103 | /** Parsees a JSON-AD array string, returns array of Resources */ |
| 104 | export function parseJsonADArray(jsonArray: any[]): Resource[] { |
no test coverage detected