ochre-sdk 0.20.4 → 0.20.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -1254,9 +1254,11 @@ declare function fetchSetPropertyValuesByPropertyVariables(params: {
1254
1254
  version?: ApiVersion;
1255
1255
  }): Promise<{
1256
1256
  propertyValues: Array<PropertyValueQueryItem> | null;
1257
+ propertyValuesByPropertyVariableUuid: Record<string, Array<PropertyValueQueryItem>> | null;
1257
1258
  error: null;
1258
1259
  } | {
1259
1260
  propertyValues: null;
1261
+ propertyValuesByPropertyVariableUuid: null;
1260
1262
  error: string;
1261
1263
  }>;
1262
1264
  //#endregion
package/dist/index.mjs CHANGED
@@ -2343,11 +2343,59 @@ async function fetchSetItems(params, itemCategories, options) {
2343
2343
 
2344
2344
  //#endregion
2345
2345
  //#region src/utils/fetchers/set/property-values-by-property-variables.ts
2346
+ function parsePropertyValueLabel(content) {
2347
+ if (content == null || content === "") return null;
2348
+ if (typeof content === "object") return parseStringContent({ content });
2349
+ return parseFakeString(content);
2350
+ }
2351
+ function parsePropertyValueBooleanContent(rawValue) {
2352
+ if (rawValue == null || rawValue === "") return null;
2353
+ if (typeof rawValue === "boolean") return rawValue;
2354
+ return rawValue.toString().toLocaleLowerCase("en-US") === "true";
2355
+ }
2356
+ function getPropertyValueGroupKey(value) {
2357
+ const contentKey = value.content == null ? "null" : `${typeof value.content}:${value.content.toLocaleString("en-US")}`;
2358
+ return `${value.dataType}|${contentKey}`;
2359
+ }
2360
+ function aggregatePropertyValues(values) {
2361
+ const groupedPropertyValuesMap = /* @__PURE__ */ new Map();
2362
+ for (const value of values) {
2363
+ const key = getPropertyValueGroupKey(value);
2364
+ const existing = groupedPropertyValuesMap.get(key);
2365
+ if (existing == null) {
2366
+ groupedPropertyValuesMap.set(key, {
2367
+ dataType: value.dataType,
2368
+ content: value.content,
2369
+ label: value.label,
2370
+ itemUuids: new Set([value.itemUuid])
2371
+ });
2372
+ continue;
2373
+ }
2374
+ existing.itemUuids.add(value.itemUuid);
2375
+ if (existing.label == null && value.label != null) existing.label = value.label;
2376
+ }
2377
+ const groupedPropertyValues = [];
2378
+ for (const group of groupedPropertyValuesMap.values()) {
2379
+ if (group.content == null) continue;
2380
+ groupedPropertyValues.push({
2381
+ count: group.itemUuids.size,
2382
+ dataType: group.dataType,
2383
+ content: group.content,
2384
+ label: group.label
2385
+ });
2386
+ }
2387
+ return groupedPropertyValues.toSorted((a, b) => {
2388
+ if (a.count !== b.count) return b.count - a.count;
2389
+ if (a.label !== b.label) return a.label?.localeCompare(b.label ?? "") ?? 0;
2390
+ return a.content?.toString().localeCompare(b.content?.toString() ?? "") ?? 0;
2391
+ });
2392
+ }
2346
2393
  /**
2347
2394
  * Schema for a single property value query item in the OCHRE API response
2348
2395
  */
2349
2396
  const propertyValueQueryItemSchema = z.object({
2350
2397
  uuid: z.string(),
2398
+ variableUuid: z.string().optional(),
2351
2399
  itemUuid: z.string().optional(),
2352
2400
  dataType: z.string(),
2353
2401
  rawValue: fakeStringSchema.optional(),
@@ -2358,6 +2406,7 @@ const propertyValueQueryItemSchema = z.object({
2358
2406
  ]).optional()
2359
2407
  }).transform((val) => {
2360
2408
  const returnValue = {
2409
+ variableUuid: val.variableUuid != null && val.variableUuid !== "" ? val.variableUuid : null,
2361
2410
  itemUuid: val.itemUuid != null && val.itemUuid !== "" ? val.itemUuid : null,
2362
2411
  dataType: val.dataType,
2363
2412
  content: null,
@@ -2366,21 +2415,24 @@ const propertyValueQueryItemSchema = z.object({
2366
2415
  switch (val.dataType) {
2367
2416
  case "IDREF":
2368
2417
  returnValue.content = val.uuid !== "" ? val.uuid : null;
2369
- returnValue.label = val.content != null && val.content !== "" ? typeof val.content === "object" ? parseStringContent({ content: val.content }) : parseFakeString(val.content) : null;
2418
+ returnValue.label = parsePropertyValueLabel(val.content);
2370
2419
  break;
2371
2420
  case "integer":
2372
2421
  case "decimal":
2373
2422
  case "time":
2374
- returnValue.content = val.rawValue != null && val.rawValue !== "" ? Number(val.rawValue) : null;
2375
- returnValue.label = val.content != null && val.content !== "" ? val.content.toString() : null;
2423
+ if (val.rawValue != null && val.rawValue !== "") {
2424
+ const numericContent = Number(val.rawValue);
2425
+ returnValue.content = Number.isNaN(numericContent) ? null : numericContent;
2426
+ }
2427
+ returnValue.label = parsePropertyValueLabel(val.content);
2376
2428
  break;
2377
2429
  case "boolean":
2378
- returnValue.content = val.rawValue != null && val.rawValue !== "" ? Boolean(val.rawValue) : null;
2379
- returnValue.label = val.content != null && val.content !== "" ? val.content.toString() : null;
2430
+ returnValue.content = parsePropertyValueBooleanContent(val.rawValue);
2431
+ returnValue.label = parsePropertyValueLabel(val.content);
2380
2432
  break;
2381
2433
  default:
2382
2434
  returnValue.content = val.rawValue != null && val.rawValue !== "" ? val.rawValue.toString() : null;
2383
- returnValue.label = val.content != null && val.content !== "" ? val.content.toString() : null;
2435
+ returnValue.label = parsePropertyValueLabel(val.content);
2384
2436
  break;
2385
2437
  }
2386
2438
  return returnValue;
@@ -2413,10 +2465,12 @@ function buildXQuery(params, options) {
2413
2465
  ${collectionScopeFilter}
2414
2466
  //property[label/(${propertyVariableFilters})]
2415
2467
 
2416
- for $v in $matching-props/value${isLimitedToLeafPropertyValues ? "[not(@i)]" : ""}
2468
+ for $p in $matching-props
2469
+ for $v in $p/value${isLimitedToLeafPropertyValues ? "[not(@i)]" : ""}
2417
2470
  let $item-uuid := $v/ancestor::*[parent::items]/@uuid
2418
- return <propertyValue uuid="{$v/@uuid}" rawValue="{$v/@rawValue}" dataType="{$v/@dataType}" itemUuid="{$item-uuid}">{
2419
- if ($v/content) then $v/content else $v/text()
2471
+ let $variable-uuid := $p/label/@uuid
2472
+ return <propertyValue uuid="{$v/@uuid}" rawValue="{$v/@rawValue}" dataType="{$v/@dataType}" itemUuid="{$item-uuid}" variableUuid="{$variable-uuid}">{
2473
+ if ($v/content) then string-join($v/content[@xml:lang="eng"]/string, "") else $v/text()
2420
2474
  }</propertyValue>`}}</ochre>`;
2421
2475
  }
2422
2476
  /**
@@ -2448,36 +2502,34 @@ async function fetchSetPropertyValuesByPropertyVariables(params, options) {
2448
2502
  const parsedResultRaw = responseSchema.parse(data);
2449
2503
  if (Array.isArray(parsedResultRaw.result)) throw new TypeError("No property values found");
2450
2504
  const parsedPropertyValues = Array.isArray(parsedResultRaw.result.ochre.propertyValue) ? parsedResultRaw.result.ochre.propertyValue : [parsedResultRaw.result.ochre.propertyValue];
2451
- const groupedPropertyValuesMap = /* @__PURE__ */ new Map();
2505
+ const propertyValuesByPropertyVariableUuidRaw = {};
2506
+ const flattenedPropertyValues = [];
2452
2507
  for (const propertyValue of parsedPropertyValues) {
2453
- const existing = groupedPropertyValuesMap.get(propertyValue.content);
2454
- if (existing == null) groupedPropertyValuesMap.set(propertyValue.content, {
2508
+ const aggregatePropertyValueItem = {
2509
+ itemUuid: propertyValue.itemUuid,
2455
2510
  dataType: propertyValue.dataType,
2456
2511
  content: propertyValue.content,
2457
- label: propertyValue.label,
2458
- itemUuids: new Set([propertyValue.itemUuid])
2459
- });
2460
- else existing.itemUuids.add(propertyValue.itemUuid);
2512
+ label: propertyValue.label
2513
+ };
2514
+ flattenedPropertyValues.push(aggregatePropertyValueItem);
2515
+ if (propertyValue.variableUuid == null) continue;
2516
+ (propertyValuesByPropertyVariableUuidRaw[propertyValue.variableUuid] ??= []).push(aggregatePropertyValueItem);
2517
+ }
2518
+ const propertyValuesByPropertyVariableUuid = {};
2519
+ for (const [propertyVariableUuid, values] of Object.entries(propertyValuesByPropertyVariableUuidRaw)) {
2520
+ const aggregatedValues = aggregatePropertyValues(values);
2521
+ if (aggregatedValues.length > 0) propertyValuesByPropertyVariableUuid[propertyVariableUuid] = aggregatedValues;
2461
2522
  }
2462
- const groupedPropertyValues = [];
2463
- for (const group of groupedPropertyValuesMap.values()) groupedPropertyValues.push({
2464
- count: group.itemUuids.size,
2465
- dataType: group.dataType,
2466
- content: group.content,
2467
- label: group.label
2468
- });
2469
2523
  return {
2470
- propertyValues: groupedPropertyValues.filter((propertyValue) => propertyValue.content !== null).toSorted((a, b) => {
2471
- if (a.count !== b.count) return b.count - a.count;
2472
- if (a.label !== b.label) return a.label?.localeCompare(b.label ?? "") ?? 0;
2473
- return a.content?.toString().localeCompare(b.content?.toString() ?? "") ?? 0;
2474
- }),
2524
+ propertyValues: aggregatePropertyValues(flattenedPropertyValues),
2525
+ propertyValuesByPropertyVariableUuid,
2475
2526
  error: null
2476
2527
  };
2477
2528
  } catch (error) {
2478
2529
  console.error(error);
2479
2530
  return {
2480
2531
  propertyValues: null,
2532
+ propertyValuesByPropertyVariableUuid: null,
2481
2533
  error: error instanceof Error ? error.message : "Failed to fetch property values by property variables"
2482
2534
  };
2483
2535
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ochre-sdk",
3
- "version": "0.20.4",
3
+ "version": "0.20.6",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "description": "Node.js library for working with OCHRE (Online Cultural and Historical Research Environment) data",