ochre-sdk 0.20.28 → 0.20.30

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.
Files changed (2) hide show
  1. package/dist/index.mjs +63 -6
  2. package/package.json +1 -1
package/dist/index.mjs CHANGED
@@ -2409,6 +2409,52 @@ function buildOrCtsQueryExpression(queries) {
2409
2409
  if (queries.length === 1) return queries[0] ?? "cts:false-query()";
2410
2410
  return `cts:or-query((${queries.join(", ")}))`;
2411
2411
  }
2412
+ function buildPredicateGroups(predicateParts) {
2413
+ const groups = [];
2414
+ let currentGroup = [];
2415
+ for (const predicatePart of predicateParts) {
2416
+ if (predicatePart.operator == null || predicatePart.operator === "AND") {
2417
+ currentGroup.push(predicatePart.predicate);
2418
+ continue;
2419
+ }
2420
+ if (currentGroup.length > 0) groups.push(currentGroup);
2421
+ currentGroup = [predicatePart.predicate];
2422
+ }
2423
+ if (currentGroup.length > 0) groups.push(currentGroup);
2424
+ return groups;
2425
+ }
2426
+ function buildGroupedPredicateExpression(predicateGroups) {
2427
+ if (predicateGroups.length === 0) return "";
2428
+ const groupExpressions = [];
2429
+ for (let index = 0; index < predicateGroups.length;) {
2430
+ const predicateGroup = predicateGroups[index];
2431
+ const firstPredicate = predicateGroup?.[0];
2432
+ if (predicateGroup == null || predicateGroup.length < 2 || firstPredicate == null) {
2433
+ groupExpressions.push(buildAndPredicate(predicateGroup ?? []));
2434
+ index += 1;
2435
+ continue;
2436
+ }
2437
+ let sharedGroupEnd = index + 1;
2438
+ while (sharedGroupEnd < predicateGroups.length) {
2439
+ const sharedGroup = predicateGroups[sharedGroupEnd];
2440
+ if (sharedGroup == null || sharedGroup.length < 2 || sharedGroup[0] !== firstPredicate) break;
2441
+ sharedGroupEnd += 1;
2442
+ }
2443
+ if (sharedGroupEnd === index + 1) {
2444
+ groupExpressions.push(buildAndPredicate(predicateGroup));
2445
+ index += 1;
2446
+ continue;
2447
+ }
2448
+ const suffixGroupExpressions = [];
2449
+ for (const sharedGroup of predicateGroups.slice(index, sharedGroupEnd)) {
2450
+ const suffixPredicates = sharedGroup.slice(1);
2451
+ suffixGroupExpressions.push(buildAndPredicate(suffixPredicates));
2452
+ }
2453
+ groupExpressions.push(buildAndPredicate([firstPredicate, buildOrPredicate(suffixGroupExpressions)]));
2454
+ index = sharedGroupEnd;
2455
+ }
2456
+ return buildOrPredicate(groupExpressions);
2457
+ }
2412
2458
  function buildContentTargetCandidateBranch(params) {
2413
2459
  const { containerElementName, termExpression, isCaseSensitive, language } = params;
2414
2460
  return `cts:element-query(xs:QName("${containerElementName}"),
@@ -2507,12 +2553,12 @@ function buildPropertyStringIncludesGroupMember(query) {
2507
2553
  const propertyValue = query.propertyValues[0] ?? "";
2508
2554
  if (propertyVariables.length > 0) predicateParts.push(buildPropertyLabelPredicate(propertyVariables));
2509
2555
  return {
2510
- rawPredicate: `.//properties//property[${buildAndPredicate([...predicateParts, buildRawStringMatchPredicate({
2556
+ rawPredicate: buildPropertyPredicateExpression([...predicateParts, buildRawStringMatchPredicate({
2511
2557
  valueExpression,
2512
2558
  value: propertyValue,
2513
2559
  matchMode: "includes",
2514
2560
  isCaseSensitive: query.isCaseSensitive
2515
- })])}]`,
2561
+ })]),
2516
2562
  buildCandidateTermQuery: (termExpression) => buildPropertyStringCandidateBranch({
2517
2563
  termExpression,
2518
2564
  isCaseSensitive: query.isCaseSensitive,
@@ -2658,6 +2704,11 @@ function buildPropertyValueAttributePredicate(params) {
2658
2704
  for (const propertyValue of propertyValues) valuePredicates.push(`value[@${attributeName}=${stringLiteral(propertyValue)}]`);
2659
2705
  return buildOrPredicate(valuePredicates);
2660
2706
  }
2707
+ function buildPropertyPredicateExpression(propertyPredicates) {
2708
+ let propertyExpression = ".//properties//property";
2709
+ for (const propertyPredicate of propertyPredicates) propertyExpression += `[${propertyPredicate}]`;
2710
+ return propertyExpression;
2711
+ }
2661
2712
  function buildPropertyStringValueClause(params) {
2662
2713
  const { query, version, queryKey } = params;
2663
2714
  const propertyContentNodesExpression = query.matchMode === "includes" && version === 2 ? `value[not(@inherited="true")]/content[@xml:lang="${query.language}"]` : `value/content[@xml:lang="${query.language}"]`;
@@ -2743,7 +2794,7 @@ function buildPropertyClause(params) {
2743
2794
  }
2744
2795
  return {
2745
2796
  declarations,
2746
- predicate: `.//properties//property[${buildAndPredicate(predicateParts)}]`,
2797
+ predicate: buildPropertyPredicateExpression(predicateParts),
2747
2798
  candidateQueryVar
2748
2799
  };
2749
2800
  }
@@ -2880,11 +2931,17 @@ function buildQueryPlan(params) {
2880
2931
  }
2881
2932
  if (compiledClause.candidateQueryVar != null) candidateQueryVars.push(compiledClause.candidateQueryVar);
2882
2933
  if (index === 0) {
2883
- predicateParts.push(compiledClause.predicate);
2934
+ predicateParts.push({
2935
+ operator: null,
2936
+ predicate: compiledClause.predicate
2937
+ });
2884
2938
  index += groupedQueries?.length ?? 1;
2885
2939
  continue;
2886
2940
  }
2887
- predicateParts.push(`${query.operator === "AND" ? "and" : "or"} ${compiledClause.predicate}`);
2941
+ predicateParts.push({
2942
+ operator: query.operator ?? "OR",
2943
+ predicate: compiledClause.predicate
2944
+ });
2888
2945
  index += groupedQueries?.length ?? 1;
2889
2946
  }
2890
2947
  if (hasCtsIncludesClauses) declarations.unshift(`let ${CTS_INCLUDES_STOP_WORDS_VAR} := (${CTS_INCLUDES_STOP_WORDS.map((stopWord) => stringLiteral(stopWord)).join(", ")})`);
@@ -2905,7 +2962,7 @@ function buildQueryPlan(params) {
2905
2962
  return {
2906
2963
  declarations,
2907
2964
  itemsExpression,
2908
- predicate: predicateParts.join(" ")
2965
+ predicate: buildGroupedPredicateExpression(buildPredicateGroups(predicateParts))
2909
2966
  };
2910
2967
  }
2911
2968
  //#endregion
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ochre-sdk",
3
- "version": "0.20.28",
3
+ "version": "0.20.30",
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",