MCPcopy Index your code
hub / github.com/atomicdata-dev/atomic-data-browser / parseJsonAdResourceValue

Function parseJsonAdResourceValue

lib/src/parse.ts:79–101  ·  view source on GitHub ↗

* 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,
)

Source from the content-addressed store, hash-verified

77 * the Resource.
78 */
79function 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 */
104export function parseJsonADArray(jsonArray: any[]): Resource[] {

Callers 1

parseJsonADResourceFunction · 0.85

Calls 1

parseJsonADResourceFunction · 0.85

Tested by

no test coverage detected