ochre-sdk 1.0.79 → 1.1.1

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 (84) hide show
  1. package/README.md +60 -3
  2. package/dist/_virtual/_rolldown/runtime.mjs +13 -0
  3. package/dist/categories.d.mts +249 -0
  4. package/dist/categories.mjs +259 -0
  5. package/dist/constants.d.mts +14 -0
  6. package/dist/constants.mjs +18 -1
  7. package/dist/errors.d.mts +23 -0
  8. package/dist/{utilities.mjs → errors.mjs} +33 -84
  9. package/dist/fetchers/gallery.mjs +15 -51
  10. package/dist/fetchers/item-children.mjs +20 -66
  11. package/dist/fetchers/item-links.mjs +26 -79
  12. package/dist/fetchers/item-ocr-data.d.mts +2 -2
  13. package/dist/fetchers/item-ocr-data.mjs +11 -15
  14. package/dist/fetchers/item.d.mts +0 -15
  15. package/dist/fetchers/item.mjs +20 -72
  16. package/dist/fetchers/request.d.mts +70 -0
  17. package/dist/fetchers/request.mjs +100 -0
  18. package/dist/fetchers/set/items.mjs +27 -73
  19. package/dist/fetchers/set/property-values.d.mts +2 -3
  20. package/dist/fetchers/set/property-values.mjs +96 -130
  21. package/dist/fetchers/website-metadata.mjs +35 -57
  22. package/dist/fetchers/website.d.mts +2 -3
  23. package/dist/fetchers/website.mjs +22 -31
  24. package/dist/getters.d.mts +86 -145
  25. package/dist/getters.mjs +136 -207
  26. package/dist/helpers.d.mts +0 -4
  27. package/dist/helpers.mjs +19 -6
  28. package/dist/index.d.mts +8 -6
  29. package/dist/index.mjs +6 -4
  30. package/dist/ocr.d.mts +37 -0
  31. package/dist/ocr.mjs +52 -0
  32. package/dist/parsers/helpers.d.mts +21 -1
  33. package/dist/parsers/helpers.mjs +26 -6
  34. package/dist/parsers/index.d.mts +0 -8
  35. package/dist/parsers/index.mjs +118 -259
  36. package/dist/parsers/languages.d.mts +72 -0
  37. package/dist/parsers/languages.mjs +132 -0
  38. package/dist/parsers/multilingual.d.mts +134 -58
  39. package/dist/parsers/multilingual.mjs +191 -143
  40. package/dist/parsers/property-token.d.mts +34 -0
  41. package/dist/parsers/property-token.mjs +29 -0
  42. package/dist/parsers/string.d.mts +19 -0
  43. package/dist/parsers/string.mjs +45 -25
  44. package/dist/parsers/website/bounds.d.mts +10 -0
  45. package/dist/parsers/website/bounds.mjs +28 -0
  46. package/dist/parsers/website/components.d.mts +91 -0
  47. package/dist/parsers/website/components.mjs +681 -0
  48. package/dist/parsers/website/index.d.mts +0 -7
  49. package/dist/parsers/website/index.mjs +92 -1153
  50. package/dist/parsers/website/links.d.mts +36 -0
  51. package/dist/parsers/website/links.mjs +58 -0
  52. package/dist/parsers/website/messages.d.mts +24 -0
  53. package/dist/parsers/website/messages.mjs +31 -0
  54. package/dist/parsers/website/options.d.mts +6 -0
  55. package/dist/parsers/website/options.mjs +114 -0
  56. package/dist/parsers/website/properties.d.mts +12 -0
  57. package/dist/parsers/website/properties.mjs +158 -0
  58. package/dist/parsers/website/reader.d.mts +54 -4
  59. package/dist/parsers/website/reader.mjs +65 -20
  60. package/dist/parsers/website/slug.d.mts +64 -0
  61. package/dist/parsers/website/slug.mjs +82 -0
  62. package/dist/parsers/website/styles.d.mts +28 -0
  63. package/dist/parsers/website/styles.mjs +103 -0
  64. package/dist/parsers/website/walk.d.mts +68 -0
  65. package/dist/parsers/website/walk.mjs +116 -0
  66. package/dist/query.d.mts +66 -21
  67. package/dist/query.mjs +178 -44
  68. package/dist/reflection.d.mts +64 -0
  69. package/dist/reflection.mjs +79 -0
  70. package/dist/schemas.d.mts +7 -0
  71. package/dist/schemas.mjs +12 -3
  72. package/dist/types/index.d.mts +31 -31
  73. package/dist/types/utilities.d.mts +9 -0
  74. package/dist/types/utilities.mjs +1 -0
  75. package/dist/types/website.d.mts +54 -58
  76. package/dist/xml/metadata.d.mts +16 -0
  77. package/dist/xml/metadata.mjs +32 -11
  78. package/dist/xml/schemas.d.mts +5970 -3
  79. package/dist/xml/schemas.mjs +43 -45
  80. package/dist/xml/types.d.mts +14 -30
  81. package/dist/xquery.d.mts +46 -0
  82. package/dist/xquery.mjs +66 -0
  83. package/package.json +3 -3
  84. package/dist/utilities.d.mts +0 -54
package/dist/ocr.mjs ADDED
@@ -0,0 +1,52 @@
1
+ import { stringLiteral } from "./xquery.mjs";
2
+ //#region src/ocr.ts
3
+ const ALTO_NAMESPACE = "http://www.loc.gov/standards/alto/ns-v2#";
4
+ /**
5
+ * The local names OCHRE serves OCR word elements under
6
+ *
7
+ * OCHRE varies the casing, and serves the elements both in the ALTO namespace
8
+ * and in no namespace at all. Every selector over the layer has to accept all
9
+ * of them: an `xs:QName("String")` narrowing matches 2 of the 18,446 documents
10
+ * carrying an OCR layer, while the ALTO-namespaced `String` matches 14,650.
11
+ */
12
+ const OCR_WORD_LOCAL_NAMES = ["String", "string"];
13
+ const OCR_WORD_CASE_FOLDED_LOCAL_NAME = "string";
14
+ /**
15
+ * The name of the element wrapping an OCR layer inside an OCHRE document
16
+ * @internal
17
+ */
18
+ const OCR_LAYER_ELEMENT_NAME = "ocr";
19
+ /**
20
+ * The attribute an OCR word element holds its word in
21
+ *
22
+ * The word is in the attribute rather than in the element's text content.
23
+ * @internal
24
+ */
25
+ const OCR_WORD_CONTENT_ATTRIBUTE = "CONTENT";
26
+ /**
27
+ * An XQuery sequence of every QName an OCR word element can carry
28
+ *
29
+ * Use with the `cts:element-*` query constructors, which need concrete QNames
30
+ * and cannot match on a case-folded local name the way {@link buildOcrWordPath}
31
+ * does. This has to stay in agreement with that path: a word element the
32
+ * narrowing misses is a document the search silently drops.
33
+ * @internal
34
+ */
35
+ const OCR_WORD_QNAMES = `(${[...OCR_WORD_LOCAL_NAMES.map((localName) => `fn:QName(${stringLiteral(ALTO_NAMESPACE)}, ${stringLiteral(localName)})`), ...OCR_WORD_LOCAL_NAMES.map((localName) => `xs:QName(${stringLiteral(localName)})`)].join(", ")})`;
36
+ /**
37
+ * Build an XQuery path selecting every OCR word element beneath a node
38
+ *
39
+ * Matches the layer and the word elements on a case-folded `local-name()`
40
+ * rather than a name test, because a name test silently matches nothing when
41
+ * OCHRE varies the casing or serves the elements in a namespace. Word elements
42
+ * are selected at any depth, because the hierarchy between the layer and its
43
+ * words is irregular and is not parsed.
44
+ * @param rootExpression - The XQuery expression to select from
45
+ * @returns The XQuery path
46
+ * @internal
47
+ */
48
+ function buildOcrWordPath(rootExpression) {
49
+ return `${rootExpression}//*[lower-case(local-name(.)) = ${stringLiteral("ocr")}]//*[lower-case(local-name(.)) = ${stringLiteral(OCR_WORD_CASE_FOLDED_LOCAL_NAME)}][@${OCR_WORD_CONTENT_ATTRIBUTE}]`;
50
+ }
51
+ //#endregion
52
+ export { OCR_LAYER_ELEMENT_NAME, OCR_WORD_CONTENT_ATTRIBUTE, OCR_WORD_QNAMES, buildOcrWordPath };
@@ -6,14 +6,34 @@ export type FetchFunction = (input: string | URL | globalThis.Request, init?: Re
6
6
  export type FetchBaseOptions<TLanguages extends ReadonlyArray<string> | undefined = undefined> = {
7
7
  languages?: TLanguages;
8
8
  fetch?: FetchFunction;
9
+ signal?: AbortSignal;
10
+ timeoutMilliseconds?: number;
9
11
  };
10
12
  export type FetchRuntimeOptions = FetchBaseOptions<ReadonlyArray<string>>;
11
13
  export type FetchLanguages<TLanguages extends ReadonlyArray<string> | undefined> = TLanguages extends readonly [] ? ReadonlyArray<string> : TLanguages extends ReadonlyArray<string> ? TLanguages : ReadonlyArray<string>;
12
14
  export type ParserOptions<T extends ReadonlyArray<string>> = {
13
15
  languages: T;
16
+ /**
17
+ * The language the dataset declares as its default
18
+ *
19
+ * Threaded into every {@link MultilingualString} so a read with no language
20
+ * resolves to the dataset's default rather than whichever language happens to
21
+ * come first in the payload.
22
+ */
23
+ defaultLanguage?: T[number];
14
24
  };
15
25
  export declare function getParserOptions<T extends ReadonlyArray<string>>(options: ParserOptions<T>): ParserOptions<T>;
16
- export declare function cleanObject<T extends Record<string, unknown>>(object: T): Partial<T>;
26
+ /**
27
+ * Drop the keys whose value is undefined
28
+ *
29
+ * Constrained to `object` rather than `Record<string, unknown>`, which an
30
+ * interface never satisfies for want of an index signature. `Object.entries`
31
+ * cannot be typed precisely, so the one cast that costs is here rather than at
32
+ * the call site.
33
+ * @param object - The object to clean
34
+ * @returns A copy carrying only the defined keys
35
+ */
36
+ export declare function cleanObject<T extends object>(object: T): Partial<T>;
17
37
  export declare function parseLicense(availability: {
18
38
  license: XMLString & {
19
39
  target?: string;
@@ -4,11 +4,25 @@ import { parseXMLContent, parseXMLString } from "./string.mjs";
4
4
  //#region src/parsers/helpers.ts
5
5
  const FALLBACK_PARSER_OPTIONS = { languages: DEFAULT_LANGUAGES };
6
6
  function getParserOptions(options) {
7
- return { languages: options.languages };
7
+ return {
8
+ languages: options.languages,
9
+ defaultLanguage: options.defaultLanguage
10
+ };
8
11
  }
12
+ /**
13
+ * Drop the keys whose value is undefined
14
+ *
15
+ * Constrained to `object` rather than `Record<string, unknown>`, which an
16
+ * interface never satisfies for want of an index signature. `Object.entries`
17
+ * cannot be typed precisely, so the one cast that costs is here rather than at
18
+ * the call site.
19
+ * @param object - The object to clean
20
+ * @returns A copy carrying only the defined keys
21
+ */
9
22
  function cleanObject(object) {
23
+ const entries = Object.entries(object);
10
24
  const cleaned = {};
11
- for (const [key, value] of Object.entries(object)) if (value !== void 0) cleaned[key] = value;
25
+ for (const [key, value] of entries) if (value !== void 0) cleaned[key] = value;
12
26
  return cleaned;
13
27
  }
14
28
  function parseLicense(availability) {
@@ -29,16 +43,19 @@ function isXMLContent(value) {
29
43
  function multilingualFromText(text, options) {
30
44
  const content = {};
31
45
  for (const language of options.languages) content[language] = text;
32
- return MultilingualString.fromObject(content, options.languages);
46
+ return MultilingualString.fromObject(content, options.languages, { defaultLanguage: options.defaultLanguage });
33
47
  }
34
48
  function parseContentLike(value, options) {
35
49
  if (value == null) return null;
36
50
  if (typeof value === "string") return multilingualFromText(value, options);
37
51
  if (!isXMLContent(value)) return multilingualFromText(parseXMLString(value), options);
38
- return parseXMLContent(value, { languages: options.languages });
52
+ return parseXMLContent(value, {
53
+ languages: options.languages,
54
+ defaultLanguage: options.defaultLanguage
55
+ });
39
56
  }
40
57
  function parseRequiredContentLike(value, options) {
41
- return parseContentLike(value, options) ?? MultilingualString.empty(options.languages);
58
+ return parseContentLike(value, options) ?? MultilingualString.empty(options.languages, { defaultLanguage: options.defaultLanguage });
42
59
  }
43
60
  function parseContentLikeText(value, options) {
44
61
  return parseContentLike(value, options)?.getText().trim() ?? "";
@@ -46,7 +63,10 @@ function parseContentLikeText(value, options) {
46
63
  function parseStringContent(value, options = FALLBACK_PARSER_OPTIONS) {
47
64
  if (value == null) return "";
48
65
  if (typeof value === "string") return value;
49
- if (isXMLContent(value)) return parseXMLContent(value, { languages: options.languages }).getText();
66
+ if (isXMLContent(value)) return parseXMLContent(value, {
67
+ languages: options.languages,
68
+ defaultLanguage: options.defaultLanguage
69
+ }).getText();
50
70
  return parseXMLString(value).text;
51
71
  }
52
72
  //#endregion
@@ -21,14 +21,6 @@ export declare function parseBibliographyList<T extends ReadonlyArray<string>>(r
21
21
  bibliography: Array<XMLBibliography>;
22
22
  } | undefined, options: ParserOptions<T>): Array<Bibliography<T, "embedded">>;
23
23
  export declare function parsePersonList<T extends ReadonlyArray<string>>(rawPersons: Array<XMLPerson> | undefined, options: ParserOptions<T>): Array<Person<T, "embedded">>;
24
- export declare function parseMetadataLanguages(rawOchre: {
25
- metadata: XMLMetadata;
26
- languages?: string;
27
- }): Array<string>;
28
- export declare function resolveLanguages<T extends ReadonlyArray<string>>(requestedLanguages: T | undefined, metadataLanguages: Array<string>): T;
29
- export declare function resolveDefaultLanguage<T extends ReadonlyArray<string>>(rawOchre: {
30
- metadata: XMLMetadata;
31
- }, languages: T): T[number];
32
24
  export declare function parseMetadata<T extends ReadonlyArray<string>>(rawOchre: {
33
25
  uuidBelongsTo: string;
34
26
  metadata: XMLMetadata;