Skip to content

Commit 4008d32

Browse files
authored
NEW: add tags property to models (#160)
closes #160
1 parent ed48d2b commit 4008d32

10 files changed

Lines changed: 90 additions & 26 deletions

File tree

src/models/Language.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import isGlottoCode from '../utilities/types/isGlottoCode.js';
33
import isISOCode from '../utilities/types/isISO.js';
44
import Model from '../core/Model.js';
55
import MultiLangString from './MultiLangString.js';
6+
import Tags from './Tags.js';
67

78
/**
89
* Validates a language abbreviation. Throws a type error if the input is not a valid abbreviation.
@@ -54,6 +55,7 @@ function validateISOCode(input) {
5455
* @prop {String} glottolog - The Glottocode for this language
5556
* @prop {String} iso - The ISO 639-3 code for this language
5657
* @prop {models.MultiLangString} name - The name of this language
58+
* @prop {models.Tags} tags - The tags for this language
5759
* @prop {String} type - "Language"
5860
*/
5961
class Language extends Model {
@@ -65,18 +67,20 @@ class Language extends Model {
6567
* @param {String} [data.glottolog] The Glottolog Code for this language.
6668
* @param {String} [data.iso] The ISO 639-3 code for this language.
6769
* @param {Map|Object|String} [data.name] The name of this language. May be a string if English, an Object formatted as a [MultiLangString]{@link https://format.digitallinguistics.io/schemas/MultiLangString.html}, or a Map of language tags => transcriptions.
70+
* @param {Map|Object} [data.tags] A Map or Object of tags for this Language, formatted as a [DLx Tags object]{@link https://format.digitallinguistics.io/schemas/Tags.html}.
6871
*/
6972
constructor(data = {}) {
7073

7174
super(data);
7275

7376
// Property Definitions
7477

75-
Model.defineModelProp(this, `name`, MultiLangString);
76-
Model.defineTypeProp(this, `Language`);
7778
Model.defineValidatedProp(this, `abbreviation`, validateAbbreviation);
7879
Model.defineValidatedProp(this, `glottolog`, validateGlottoCode);
7980
Model.defineValidatedProp(this, `iso`, validateISOCode);
81+
Model.defineTypeProp(this, `Language`);
82+
Model.defineModelProp(this, `name`, MultiLangString);
83+
Model.defineModelProp(this, `tags`, Tags);
8084

8185
// Initialization
8286

src/models/Language.test.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,28 @@
55
import chai from 'chai';
66
import { Language, MultiLangString } from './index.js';
77

8-
chai.should();
8+
const should = chai.should();
99

1010
describe(`Language`, () => {
1111

1212
it(`class: Language`, () => {
1313
Language.name.should.equal(`Language`);
1414
});
1515

16+
it(`instantiates with no data`, () => {
17+
18+
const lang = new Language;
19+
20+
should.not.exist(lang.abbreviation);
21+
should.not.exist(lang.glottolog);
22+
should.not.exist(lang.iso);
23+
should.not.exist(lang.tags);
24+
25+
lang.type.should.equal(`Language`);
26+
lang.name.should.be.instanceOf(MultiLangString);
27+
28+
});
29+
1630
it(`abbreviation`, () => {
1731
const lang = new Language;
1832
(() => { lang.abbreviation = undefined; }).should.not.throw();
@@ -22,9 +36,14 @@ describe(`Language`, () => {
2236
});
2337

2438
it(`custom property`, () => {
39+
2540
const lang = new Language;
2641
(() => { lang.deleted = true; }).should.not.throw();
2742
lang.deleted.should.equal(true);
43+
44+
const l = new Language({ customProperty: true });
45+
l.customProperty.should.be.true;
46+
2847
});
2948

3049
it(`glottolog`, () => {
@@ -44,12 +63,14 @@ describe(`Language`, () => {
4463
});
4564

4665
it(`name`, () => {
47-
4866
const lang = new Language({ name: { eng: `Chitimacha` } });
49-
5067
lang.name.should.be.instanceOf(MultiLangString);
5168
lang.name.get(`eng`).should.equal(`Chitimacha`);
69+
});
5270

71+
it(`tags`, () => {
72+
const lang = new Language({ tags: { position: `final` } });
73+
lang.tags.get(`position`).should.equal(`final`);
5374
});
5475

5576
it(`type`, () => {

src/models/MultiLangString.test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import chai from 'chai';
66
import Model from '../core/Model.js';
77
import MultiLangString from './MultiLangString.js';
88

9-
const should = chai.should();
9+
chai.should();
1010

1111
const modelName = `MultiLangString`;
1212

@@ -21,6 +21,11 @@ describe(modelName, () => {
2121

2222
describe(`data`, () => {
2323

24+
it(`no data`, () => {
25+
const mls = new MultiLangString;
26+
mls.type.should.equal(`MultiLangString`);
27+
});
28+
2429
it(`String`, () => {
2530
const data = `hello`;
2631
const mls = new MultiLangString(data);

src/models/Tags.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ describe(`Tags`, () => {
1717
const tags = new Tags;
1818
tags.should.be.instanceOf(Map);
1919
tags.size.should.equal(0);
20+
tags.type.should.equal(`Tags`);
2021
});
2122

2223
it(`throws with bad data`, () => {

src/models/Text.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
11
import Model from '../core/Model.js';
22
import MultiLangString from './MultiLangString.js';
3+
import Tags from './Tags.js';
34
import Utterance from './Utterance.js';
45

56
/**
67
* A class representing a linguistic text, formatted according to the [DLx Data Format for a language]{@link https://format.digitallinguistics.io/schemas/Text.html}
78
* @memberof models
89
* @extends core.Model
10+
* @prop {models.Tags} tags - The tags for this text
911
* @prop {models.MultiLangString} title - The title of this text
1012
* @prop {core.Collection} utterances - An array of utterances in this text
1113
*/
1214
class Text extends Model {
1315

1416
/**
1517
* Create a new Text
16-
* @param {Object} [data={}] The data to use for this Text. Data should be formatted according to the [DLx Data Format's guidelines for Text data]{@link https://format.digitallinguistics.io/schemas/Text.html}.
18+
* @param {Object} [data={}] The data for this text, formatted as a [DLx Text object]{@link https://format.digitallinguistics.io/schemas/Text.html}.
19+
* @param {Map|Object} [data.tags] The tags for this text, formatted as a [DLx Tags object]{@link https://format.digitallinguistics.io/schemas/Tags.html}.
1720
*/
1821
constructor(data = {}) {
1922

2023
super(data);
2124

22-
Model.defineArrayProp(this, `utterances`, Utterance);
23-
Model.defineModelProp(this, `title`, MultiLangString);
25+
Model.defineModelProp(this, `tags`, Tags);
2426
Model.defineTypeProp(this, `Text`);
27+
Model.defineModelProp(this, `title`, MultiLangString);
28+
Model.defineArrayProp(this, `utterances`, Utterance);
2529

2630
Object.assign(this, data);
2731

src/models/Text.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import chai from 'chai';
12
import Collection from '../core/Collection.js';
23
import MultiLangString from './MultiLangString.js';
34
import Text from './Text.js';
45

6+
const should = chai.should();
7+
58
describe(`Text`, () => {
69

710
it(`class: Text`, () => {
@@ -12,11 +15,23 @@ describe(`Text`, () => {
1215

1316
const text = new Text;
1417

18+
should.not.exist(text.tags);
1519
text.title.should.be.instanceOf(MultiLangString);
20+
text.type.should.equal(`Text`);
1621
text.utterances.should.be.instanceOf(Collection);
1722

1823
});
1924

25+
it(`custom property`, () => {
26+
const text = new Text({ customProperty: true });
27+
text.customProperty.should.be.true;
28+
});
29+
30+
it(`tags`, () => {
31+
const text = new Text({ tags: { position: `final` } });
32+
text.tags.get(`position`).should.equal(`final`);
33+
});
34+
2035
it(`title`, () => {
2136

2237
const text = new Text({

src/models/Utterance.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Model from '../core/Model.js';
22
import MultiLangString from './MultiLangString.js';
3+
import Tags from './Tags.js';
34
import Transcription from './Transcription.js';
45
import Word from './Word.js';
56

@@ -22,11 +23,12 @@ class Utterance extends Model {
2223

2324
super();
2425

26+
Model.defineModelProp(this, `tags`, Tags);
2527
Model.defineModelProp(this, `transcript`, Transcription);
2628
Model.defineModelProp(this, `transcription`, Transcription);
2729
Model.defineModelProp(this, `translation`, MultiLangString);
28-
Model.defineArrayProp(this, `words`, Word);
2930
Model.defineTypeProp(this, `Utterance`);
31+
Model.defineArrayProp(this, `words`, Word);
3032

3133
Object.assign(this, data);
3234

src/models/Utterance.test.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import chai from 'chai';
2+
import Collection from '../core/Collection.js';
23
import MultiLangString from './MultiLangString.js';
34
import Transcription from './Transcription.js';
45
import Utterance from './Utterance.js';
@@ -13,6 +14,7 @@ describe(`Utterance`, () => {
1314

1415
const utterance = new Utterance;
1516

17+
should.not.exist(utterance.tags);
1618
should.not.exist(utterance.transcript);
1719

1820
utterance.transcription.should.be.instanceOf(Transcription);
@@ -21,15 +23,26 @@ describe(`Utterance`, () => {
2123
utterance.translation.should.be.instanceOf(MultiLangString);
2224
utterance.translation.size.should.equal(0);
2325

26+
utterance.type.should.equal(`Utterance`);
27+
28+
utterance.words.should.be.instanceOf(Collection);
29+
2430
});
2531

26-
it(`transcript`, () => {
32+
it(`custom property`, () => {
33+
const utterance = new Utterance({ customProperty: true });
34+
utterance.customProperty.should.be.true;
35+
});
2736

28-
const utterance = new Utterance({ transcript: testData });
37+
it(`tags`, () => {
38+
const utterance = new Utterance({ tags: { position: `final` } });
39+
utterance.tags.get(`position`).should.equal(`final`);
40+
});
2941

42+
it(`transcript`, () => {
43+
const utterance = new Utterance({ transcript: testData });
3044
utterance.transcript.should.be.instanceOf(Transcription);
3145
utterance.transcript.get(`eng`).should.equal(testData.eng);
32-
3346
});
3447

3548
it(`transcription`, () => {
@@ -39,12 +52,9 @@ describe(`Utterance`, () => {
3952
});
4053

4154
it(`translation`, () => {
42-
4355
const utterance = new Utterance({ translation: testData });
44-
4556
utterance.translation.should.be.instanceOf(MultiLangString);
4657
utterance.translation.get(`eng`).should.equal(testData.eng);
47-
4858
});
4959

5060
it(`type`, () => {

src/models/Word.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Model from '../core/Model.js';
22
import MultiLangString from './MultiLangString.js';
3+
import Tags from './Tags.js';
34
import Transcription from './Transcription.js';
45

56
/**
@@ -25,6 +26,7 @@ class Word extends Model {
2526
Model.defineModelProp(this, `analysis`, Transcription);
2627
Model.defineModelProp(this, `gloss`, MultiLangString);
2728
Model.defineModelProp(this, `literal`, MultiLangString);
29+
Model.defineModelProp(this, `tags`, Tags);
2830
Model.defineModelProp(this, `transcription`, Transcription);
2931
Model.defineModelProp(this, `translation`, MultiLangString);
3032
Model.defineTypeProp(this, `Word`);

src/models/Word.test.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,45 +13,45 @@ describe(`Word`, () => {
1313
const word = new Word;
1414
should.not.exist(word.analysis);
1515
should.not.exist(word.literal);
16+
should.not.exist(word.tags);
1617
word.transcription.should.be.instanceOf(Transcription);
1718
word.transcription.size.should.equal(0);
1819
should.not.exist(word.translation);
20+
word.type.should.equal(`Word`);
1921
});
2022

2123
it(`analysis`, () => {
22-
2324
const word = new Word({ analysis: testData });
24-
2525
word.analysis.should.be.instanceOf(Transcription);
2626
word.analysis.get(`eng`).should.equal(testData.eng);
27+
});
2728

29+
it(`custom property`, () => {
30+
const word = new Word({ customProperty: true });
31+
word.customProperty.should.be.true;
2832
});
2933

3034
it(`literal`, () => {
31-
3235
const word = new Word({ literal: testData });
33-
3436
word.literal.should.be.instanceOf(MultiLangString);
3537
word.literal.get(`eng`).should.equal(testData.eng);
38+
});
3639

40+
it(`tags`, () => {
41+
const word = new Word({ tags: { position: `final` } });
42+
word.tags.get(`position`).should.equal(`final`);
3743
});
3844

3945
it(`transcription`, () => {
40-
4146
const word = new Word({ transcription: testData });
42-
4347
word.transcription.should.be.instanceOf(Transcription);
4448
word.transcription.get(`eng`).should.equal(testData.eng);
45-
4649
});
4750

4851
it(`translation`, () => {
49-
5052
const word = new Word({ translation: testData });
51-
5253
word.translation.should.be.instanceOf(MultiLangString);
5354
word.translation.get(`eng`).should.equal(testData.eng);
54-
5555
});
5656

5757
it(`type`, () => {

0 commit comments

Comments
 (0)