@allmaps/iiif-inspector 1.0.0-beta.2

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/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright 2026 Bert Spaan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # @allmaps/iiif-inspector
2
+
3
+ Inspect IIIF resources and extract useful metadata for Allmaps.
4
+
5
+ ## Usage
6
+
7
+ ```ts
8
+ import { findYearInManifest } from '@allmaps/iiif-inspector'
9
+
10
+ const year = findYearInManifest(manifest)
11
+ ```
@@ -0,0 +1,3 @@
1
+ export { parseLanguageString } from './language-string.js';
2
+ export { findBestYearInIIIFResource, findYearCandidatesInIIIFResource, findYearInCanvas, findYearInIIIFResource, findYearInManifest, findYearsInLanguageString, findYearsInMetadata, findYearsInValue, scoreYear } from './years.js';
3
+ export type { IIIFInspectableResource, ScoredYearCandidate, YearCandidateOptions, YearCandidateSource, YearCandidate } from './years.js';
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { parseLanguageString } from './language-string.js';
2
+ export { findBestYearInIIIFResource, findYearCandidatesInIIIFResource, findYearInCanvas, findYearInIIIFResource, findYearInManifest, findYearsInLanguageString, findYearsInMetadata, findYearsInValue, scoreYear } from './years.js';
@@ -0,0 +1,2 @@
1
+ import type { LanguageString } from '@allmaps/iiif-parser';
2
+ export declare function parseLanguageString(languageString?: LanguageString, preferredLanguage?: string): string;
@@ -0,0 +1,18 @@
1
+ export function parseLanguageString(languageString, preferredLanguage) {
2
+ if (!languageString) {
3
+ return '';
4
+ }
5
+ let strings = [];
6
+ if (preferredLanguage && languageString[preferredLanguage]) {
7
+ strings = languageString[preferredLanguage];
8
+ }
9
+ else if (languageString['none']) {
10
+ strings = languageString['none'];
11
+ }
12
+ else if (typeof languageString === 'object' && languageString !== null) {
13
+ if (Object.values(languageString)[0]) {
14
+ strings = Object.values(languageString)[0];
15
+ }
16
+ }
17
+ return strings.map((string) => String(string)).join(' ');
18
+ }
@@ -0,0 +1,35 @@
1
+ import type { Canvas as IIIFCanvas, LanguageString, Manifest as IIIFManifest, Metadata } from '@allmaps/iiif-parser';
2
+ export type YearCandidateSource = 'navDate' | 'metadata' | 'label' | 'summary' | 'description' | 'value';
3
+ export type YearCandidate = {
4
+ value: string;
5
+ year: number;
6
+ endYear?: number;
7
+ source?: YearCandidateSource;
8
+ metadataLabel?: string;
9
+ language?: string;
10
+ approximate?: boolean;
11
+ };
12
+ export type ScoredYearCandidate = YearCandidate & {
13
+ score: number;
14
+ };
15
+ export type YearCandidateOptions = {
16
+ source?: YearCandidateSource;
17
+ metadataLabel?: string;
18
+ language?: string;
19
+ };
20
+ export type IIIFInspectableResource = {
21
+ navDate?: Date;
22
+ metadata?: Metadata;
23
+ description?: LanguageString;
24
+ label?: LanguageString;
25
+ summary?: LanguageString;
26
+ };
27
+ export declare function scoreYear(candidate: YearCandidate): number;
28
+ export declare function findYearsInLanguageString(languageString?: LanguageString, options?: Omit<YearCandidateOptions, 'language'>): YearCandidate[];
29
+ export declare function findYearsInValue(value: string | number | boolean, options?: YearCandidateOptions): YearCandidate[];
30
+ export declare function findYearInCanvas(canvas?: IIIFCanvas): number | undefined;
31
+ export declare function findYearInManifest(manifest?: IIIFManifest): number | undefined;
32
+ export declare function findYearInIIIFResource(iiifResource: IIIFInspectableResource): number | undefined;
33
+ export declare function findBestYearInIIIFResource(iiifResource: IIIFInspectableResource): ScoredYearCandidate;
34
+ export declare function findYearCandidatesInIIIFResource(iiifResource: IIIFInspectableResource): ScoredYearCandidate[];
35
+ export declare function findYearsInMetadata(metadata?: Metadata): YearCandidate[];
package/dist/years.js ADDED
@@ -0,0 +1,246 @@
1
+ const YEAR_MIN = 1500;
2
+ const YEAR_MAX = 2200;
3
+ const SOURCE_PENALTIES = {
4
+ navDate: -1000,
5
+ metadata: 100,
6
+ label: 260,
7
+ summary: 420,
8
+ description: 620,
9
+ value: 500
10
+ };
11
+ const DATE_METADATA_LABEL_PATTERNS = [
12
+ /\bdate\b/i,
13
+ /\byear\b/i,
14
+ /\bcreated?\b/i,
15
+ /\bcreation\b/i,
16
+ /\bissued?\b/i,
17
+ /\bpublished?\b/i,
18
+ /\bpublication\b/i,
19
+ /\btemporal\b/i,
20
+ /\bcoverage\b/i
21
+ ];
22
+ const WEAK_DATE_METADATA_LABEL_PATTERNS = [
23
+ /\bpublisher\b/i,
24
+ /\bprovenance\b/i,
25
+ /\borigin\b/i
26
+ ];
27
+ const NEGATIVE_METADATA_LABEL_PATTERNS = [
28
+ /\bidentifier\b/i,
29
+ /\bcall\s*number\b/i,
30
+ /\bshelf\s*mark\b/i,
31
+ /\bshelfmark\b/i,
32
+ /\baccession\b/i,
33
+ /\bdigitized?\b/i,
34
+ /\bmodified\b/i,
35
+ /\bright(s)?\b/i,
36
+ /\burl\b/i,
37
+ /\bpermalink\b/i,
38
+ /\blink\b/i,
39
+ /\blicen[cs]e\b/i
40
+ ];
41
+ const NEGATIVE_VALUE_PATTERNS = [
42
+ /\bdigitized?\b/i,
43
+ /\bmodified\b/i,
44
+ /\baccessed\b/i,
45
+ /\bdownloaded\b/i,
46
+ /\bidentifier\b/i,
47
+ /\bpermalink\b/i,
48
+ /\blicen[cs]e\b/i
49
+ ];
50
+ function isDateMetadataLabel(metadataLabel) {
51
+ return (metadataLabel &&
52
+ DATE_METADATA_LABEL_PATTERNS.some((pattern) => pattern.test(metadataLabel)));
53
+ }
54
+ function isWeakDateMetadataLabel(metadataLabel) {
55
+ return (metadataLabel &&
56
+ WEAK_DATE_METADATA_LABEL_PATTERNS.some((pattern) => pattern.test(metadataLabel)));
57
+ }
58
+ function isNegativeMetadataLabel(metadataLabel) {
59
+ return (metadataLabel &&
60
+ NEGATIVE_METADATA_LABEL_PATTERNS.some((pattern) => pattern.test(metadataLabel)));
61
+ }
62
+ function getMetadataLabelPenalty(candidate) {
63
+ if (candidate.source !== 'metadata') {
64
+ return 0;
65
+ }
66
+ if (isDateMetadataLabel(candidate.metadataLabel)) {
67
+ return -120;
68
+ }
69
+ else if (isWeakDateMetadataLabel(candidate.metadataLabel)) {
70
+ return 80;
71
+ }
72
+ else if (isNegativeMetadataLabel(candidate.metadataLabel)) {
73
+ return 650;
74
+ }
75
+ return 180;
76
+ }
77
+ function getValueContextPenalty(candidate) {
78
+ let penalty = 0;
79
+ if (NEGATIVE_VALUE_PATTERNS.some((pattern) => pattern.test(candidate.value))) {
80
+ penalty += 350;
81
+ }
82
+ if (candidate.approximate) {
83
+ penalty += 20;
84
+ }
85
+ return penalty;
86
+ }
87
+ function parseMetadataLabel(label) {
88
+ return Object.values(label)
89
+ .flat()
90
+ .map((value) => String(value))
91
+ .join(' ');
92
+ }
93
+ function isValidYear(year) {
94
+ return year > YEAR_MIN && year < YEAR_MAX;
95
+ }
96
+ function getTokenAtIndex(value, index) {
97
+ const before = value.slice(0, index);
98
+ const after = value.slice(index);
99
+ const start = Math.max(before.lastIndexOf(' '), before.lastIndexOf('\n'), before.lastIndexOf('\t'), before.lastIndexOf('"'), before.lastIndexOf("'"));
100
+ const endMatch = after.search(/[\s"'<>()[\]]/);
101
+ const end = endMatch === -1 ? value.length : index + endMatch;
102
+ return value.slice(start + 1, end);
103
+ }
104
+ function isYearInUrl(value, index) {
105
+ const token = getTokenAtIndex(value, index);
106
+ return /^https?:\/\//i.test(token) || /^www\./i.test(token);
107
+ }
108
+ function getFollowingRangeYear(value, index) {
109
+ const rangeMatch = value
110
+ .slice(index + 4)
111
+ .match(/^\s*(?:[-/–—]|\bto\b|\band\b)\s*(?<year>\d{4})\b/i);
112
+ const yearStr = rangeMatch?.groups?.year;
113
+ if (!yearStr) {
114
+ return;
115
+ }
116
+ const year = parseInt(yearStr, 10);
117
+ if (isValidYear(year)) {
118
+ return year;
119
+ }
120
+ }
121
+ function isApproximateYear(value, index) {
122
+ const prefix = value.slice(Math.max(0, index - 12), index);
123
+ return /\b(ca\.?|circa|around|about|approx\.?)\s*$/i.test(prefix);
124
+ }
125
+ // Prefer explicit IIIF and metadata date signals over incidental years in long
126
+ // descriptions. Earlier years and shorter values only break ties after source
127
+ // and context have been considered.
128
+ export function scoreYear(candidate) {
129
+ const sourcePenalty = SOURCE_PENALTIES[candidate.source ?? 'value'];
130
+ const metadataLabelPenalty = getMetadataLabelPenalty(candidate);
131
+ const valueContextPenalty = getValueContextPenalty(candidate);
132
+ const valueLengthPenalty = Math.min(candidate.value.length, 400) * 2;
133
+ const yearPenalty = (candidate.year - YEAR_MIN) * 0.05;
134
+ const score = sourcePenalty +
135
+ metadataLabelPenalty +
136
+ valueContextPenalty +
137
+ valueLengthPenalty +
138
+ yearPenalty;
139
+ return score;
140
+ }
141
+ export function findYearsInLanguageString(languageString, options = {}) {
142
+ if (!languageString) {
143
+ return [];
144
+ }
145
+ return Object.entries(languageString)
146
+ .map(([language, values]) => values.map((value) => findYearsInValue(value, {
147
+ ...options,
148
+ language
149
+ })))
150
+ .flat(2);
151
+ }
152
+ export function findYearsInValue(value, options = {}) {
153
+ const regex = /\b(?<year>\d{4})\b/g;
154
+ const stringValue = String(value);
155
+ const candidates = [];
156
+ for (const match of stringValue.matchAll(regex)) {
157
+ const yearStr = match.groups?.year;
158
+ if (yearStr) {
159
+ const year = parseInt(yearStr, 10);
160
+ if (isValidYear(year) && !isYearInUrl(stringValue, match.index)) {
161
+ const endYear = getFollowingRangeYear(stringValue, match.index);
162
+ const approximate = isApproximateYear(stringValue, match.index);
163
+ candidates.push({
164
+ value: stringValue,
165
+ year,
166
+ ...(endYear ? { endYear } : {}),
167
+ ...(approximate ? { approximate } : {}),
168
+ ...options
169
+ });
170
+ }
171
+ }
172
+ }
173
+ return candidates;
174
+ }
175
+ export function findYearInCanvas(canvas) {
176
+ if (canvas) {
177
+ return findYearInIIIFResource(canvas);
178
+ }
179
+ }
180
+ export function findYearInManifest(manifest) {
181
+ if (manifest) {
182
+ return findYearInIIIFResource(manifest);
183
+ }
184
+ }
185
+ export function findYearInIIIFResource(iiifResource) {
186
+ const candidate = findBestYearInIIIFResource(iiifResource);
187
+ if (candidate) {
188
+ return candidate.year;
189
+ }
190
+ }
191
+ export function findBestYearInIIIFResource(iiifResource) {
192
+ return findYearCandidatesInIIIFResource(iiifResource)[0];
193
+ }
194
+ export function findYearCandidatesInIIIFResource(iiifResource) {
195
+ const navDateYears = iiifResource.navDate
196
+ ? [
197
+ {
198
+ value: iiifResource.navDate.toISOString(),
199
+ year: iiifResource.navDate.getFullYear(),
200
+ source: 'navDate'
201
+ }
202
+ ]
203
+ : [];
204
+ const metadataYears = findYearsInMetadata(iiifResource.metadata);
205
+ const descriptionYears = findYearsInLanguageString(iiifResource.description, {
206
+ source: 'description'
207
+ });
208
+ const labelYears = findYearsInLanguageString(iiifResource.label, {
209
+ source: 'label'
210
+ });
211
+ const summaryYears = findYearsInLanguageString(iiifResource.summary, {
212
+ source: 'summary'
213
+ });
214
+ return [
215
+ ...navDateYears,
216
+ ...metadataYears,
217
+ ...descriptionYears,
218
+ ...labelYears,
219
+ ...summaryYears
220
+ ]
221
+ .map((year) => ({
222
+ ...year,
223
+ score: scoreYear(year)
224
+ }))
225
+ .sort((a, b) => a.score - b.score || a.year - b.year);
226
+ }
227
+ export function findYearsInMetadata(metadata) {
228
+ if (!metadata) {
229
+ return [];
230
+ }
231
+ const allYears = [];
232
+ for (const metadataItem of metadata) {
233
+ const metadataLabel = parseMetadataLabel(metadataItem.label);
234
+ const values = Object.values(metadataItem.value).flat();
235
+ for (const value of values) {
236
+ const years = findYearsInValue(value, {
237
+ source: 'metadata',
238
+ metadataLabel
239
+ });
240
+ if (years.length > 0) {
241
+ allYears.push(...years);
242
+ }
243
+ }
244
+ }
245
+ return allYears;
246
+ }
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "@allmaps/iiif-inspector",
3
+ "version": "1.0.0-beta.2",
4
+ "author": {
5
+ "name": "Bert Spaan",
6
+ "email": "hello@bertspaan.nl",
7
+ "url": "https://bertspaan.nl"
8
+ },
9
+ "description": "Inspect IIIF resources and extract useful metadata",
10
+ "type": "module",
11
+ "exports": "./dist/index.js",
12
+ "files": [
13
+ "dist"
14
+ ],
15
+ "publishConfig": {
16
+ "access": "public"
17
+ },
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "https://github.com/allmaps/allmaps.git",
21
+ "directory": "packages/iiif-inspector"
22
+ },
23
+ "homepage": "https://allmaps.org",
24
+ "keywords": [
25
+ "allmaps",
26
+ "iiif",
27
+ "inspector"
28
+ ],
29
+ "scripts": {
30
+ "watch": "tsc --watch --noCheck",
31
+ "build": "tsc --noCheck",
32
+ "types": "tsc --noEmit",
33
+ "test": "NODE_ENV=test vitest run",
34
+ "lint": "prettier --ignore-path ../../.gitignore --check src test && eslint src test --ext .js,.ts",
35
+ "documentation": "remark . --output --use remark-api",
36
+ "format": "prettier --ignore-path ../../.gitignore --write src test"
37
+ },
38
+ "license": "MIT",
39
+ "dependencies": {
40
+ "@allmaps/iiif-parser": "^1.0.0-beta.49"
41
+ },
42
+ "devDependencies": {
43
+ "@allmaps/types": "^1.0.0-beta.24",
44
+ "@typescript/native": "npm:typescript@^7.0.2",
45
+ "eslint": "^10.7.0",
46
+ "eslint-config-prettier": "^10.1.8",
47
+ "prettier": "^3.8.3",
48
+ "remark-api": "^1.1.0",
49
+ "remark-cli": "^12.0.1",
50
+ "typescript": "npm:@typescript/typescript6@^6.0.2",
51
+ "vite": "8.0.16",
52
+ "vitest": "^4.1.8"
53
+ },
54
+ "gitHead": "c1a72e7f5c7d228c6cd55e9611fe8e0dd7dd2274"
55
+ }