Skip to content

Commit

Permalink
Merge pull request #60 from IIIF-Commons/support-multiple-language-ma…
Browse files Browse the repository at this point in the history
…p-values

Add support to enable multiple metadata value retreival
  • Loading branch information
edsilv authored Apr 28, 2020
2 parents 7a24cf0 + 234eb34 commit f5f792b
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/LabelValuePair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ export class LabelValuePair {
return null;
}

public getValues(): Array<string | null> {
if (this.value) {
var locale: string = this.defaultLocale;

// if the label has a locale, prefer that to the default locale
if (this.label && this.label.length && this.label[0].locale) {
locale = this.label[0].locale;
}

return LanguageMap.getValues(this.value, locale);
}

return [];
}

public setValue(value: string): void {
if (this.value && this.value.length) {
var t: Language = this.value.filter(
Expand Down
23 changes: 23 additions & 0 deletions src/LanguageMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,27 @@ export class LanguageMap extends Array<Language> {

return null;
}

static getValues(
languageCollection: LanguageMap,
locale?: string
): Array<string | null> {
if (languageCollection.length) {
if (locale) {
return languageCollection
.filter(
t =>
t.locale === locale ||
Utils.getInexactLocale(t.locale) ===
Utils.getInexactLocale(locale)
)
.map(language => language.value);
}

// returns all of the values
return languageCollection.map(language => language.value);
}

return [];
}
}
44 changes: 44 additions & 0 deletions test/fixtures/4.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"@context": "http://iiif.io/api/presentation/2/context.json",
"@id": "http://iiif.io/api/presentation/2.1/example/fixtures/4/manifest.json",
"@type": "sc:Manifest",
"label": "Test 4 Manifest: Metadata Pairs with Multiple Values in same Language",
"metadata": [
{
"label": "date",
"value": [
"some date",
"some other date"
]
}
],
"within": "http://iiif.io/api/presentation/2.1/example/fixtures/collection.json",
"sequences": [
{
"@type": "sc:Sequence",
"label": "Test 4 Sequence 1",
"canvases": [
{
"@id": "http://iiif.io/api/presentation/2.1/example/fixtures/canvas/4/c1.json",
"@type": "sc:Canvas",
"label": "Test 4 Canvas: 1",
"height": 1800,
"width": 1200,
"images": [
{
"@type": "oa:Annotation",
"motivation": "sc:painting",
"resource": {
"@id": "http://iiif.io/api/presentation/2.1/example/fixtures/resources/page1-full.png",
"@type": "dctypes:Image",
"height": 1800,
"width": 1200
},
"on": "http://iiif.io/api/presentation/2.1/example/fixtures/canvas/4/c1.json"
}
]
}
]
}
]
}
1 change: 1 addition & 0 deletions test/fixtures/manifests.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
"4": "http://localhost:3001/4.json",
"aarau": "http://localhost:3001/aarau.json",
"annotationdimensions": "http://localhost:3001/annotation-dimensions.json",
"anzacbulletin": "http://localhost:3001/anzacbulletin.json",
Expand Down
2 changes: 2 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ importTest('horriblemurders', './tests/horriblemurders');
importTest('https', './tests/https');
importTest('illustrationsofchina', './tests/illustrationsofchina');
importTest('items-not-sequence', './tests/items-not-sequence');
importTest('LabelValuePair', './tests/LabelValuePair.test');
importTest('LanguageMap', './tests/LanguageMap.test');
importTest('logo', './tests/logo');
importTest('lunchroommanners', './tests/lunchroommanners');
importTest('manifest-with-thumbnail', './tests/manifest-with-thumbnail');
Expand Down
24 changes: 24 additions & 0 deletions test/tests/LabelValuePair.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var expect = require('chai').expect;
var should = require('chai').should();
var manifesto = require('../../dist-commonjs/');
var manifests = require('../fixtures/manifests');
var ServiceProfile = require('@iiif/vocabulary/dist-commonjs/').ServiceProfile;

var manifest;

describe('LabelValuePair', function() {
beforeEach(function(done) {
manifesto.loadManifest(manifests['4']).then(function(data) {
manifest = manifesto.parseManifest(data);
done();
});
})
describe('#getValues', function() {
it('returns an array of language values', function() {
var metadata = manifest.getMetadata().map(m => m.getValues());
expect(metadata[0]).to.eql(['some date', 'some other date']);
});
});
});


24 changes: 24 additions & 0 deletions test/tests/LanguageMap.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var expect = require('chai').expect;
var should = require('chai').should();
var manifesto = require('../../dist-commonjs/');
var manifests = require('../fixtures/manifests');
var ServiceProfile = require('@iiif/vocabulary/dist-commonjs/').ServiceProfile;

var manifest;

describe('LanguageMap', function() {
beforeEach(function(done) {
manifesto.loadManifest(manifests['4']).then(function(data) {
manifest = manifesto.parseManifest(data);
done();
});
})
describe('#getValues', function() {
it('returns an array of language values', function() {
var metadata = manifesto.LanguageMap.getValues(manifest.getMetadata())[0].map(v => v.value);
expect(metadata).to.eql(['some date', 'some other date']);
});
});
});


2 changes: 1 addition & 1 deletion test/tests/biocrats.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@ describe('#loadsBiocrats', function() {
var canvasId = range.getCanvasIds()[0];
canvasId.should.equal("http://wellcomelibrary.org/iiif/b18035978/canvas/c0");
});
});
});

0 comments on commit f5f792b

Please sign in to comment.