From eeb093a598b217d58c3cc5b56df75dc7763edafe Mon Sep 17 00:00:00 2001 From: "Daniel W. Hieber" Date: Thu, 27 Aug 2020 18:04:34 -0500 Subject: [PATCH] add tags property --- src/models/Language.js | 8 ++++++-- src/models/Language.test.js | 27 ++++++++++++++++++++++++--- src/models/MultiLangString.test.js | 7 ++++++- src/models/Tags.test.js | 1 + src/models/Text.js | 10 +++++++--- src/models/Text.test.js | 15 +++++++++++++++ src/models/Utterance.js | 4 +++- src/models/Utterance.test.js | 22 ++++++++++++++++------ src/models/Word.js | 2 ++ src/models/Word.test.js | 20 ++++++++++---------- 10 files changed, 90 insertions(+), 26 deletions(-) diff --git a/src/models/Language.js b/src/models/Language.js index 87e1614..dab8928 100644 --- a/src/models/Language.js +++ b/src/models/Language.js @@ -3,6 +3,7 @@ import isGlottoCode from '../utilities/types/isGlottoCode.js'; import isISOCode from '../utilities/types/isISO.js'; import Model from '../core/Model.js'; import MultiLangString from './MultiLangString.js'; +import Tags from './Tags.js'; /** * Validates a language abbreviation. Throws a type error if the input is not a valid abbreviation. @@ -54,6 +55,7 @@ function validateISOCode(input) { * @prop {String} glottolog - The Glottocode for this language * @prop {String} iso - The ISO 639-3 code for this language * @prop {models.MultiLangString} name - The name of this language + * @prop {models.Tags} tags - The tags for this language * @prop {String} type - "Language" */ class Language extends Model { @@ -65,6 +67,7 @@ class Language extends Model { * @param {String} [data.glottolog] The Glottolog Code for this language. * @param {String} [data.iso] The ISO 639-3 code for this language. * @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. + * @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}. */ constructor(data = {}) { @@ -72,11 +75,12 @@ class Language extends Model { // Property Definitions - Model.defineModelProp(this, `name`, MultiLangString); - Model.defineTypeProp(this, `Language`); Model.defineValidatedProp(this, `abbreviation`, validateAbbreviation); Model.defineValidatedProp(this, `glottolog`, validateGlottoCode); Model.defineValidatedProp(this, `iso`, validateISOCode); + Model.defineTypeProp(this, `Language`); + Model.defineModelProp(this, `name`, MultiLangString); + Model.defineModelProp(this, `tags`, Tags); // Initialization diff --git a/src/models/Language.test.js b/src/models/Language.test.js index 86e7aad..ef726d9 100644 --- a/src/models/Language.test.js +++ b/src/models/Language.test.js @@ -5,7 +5,7 @@ import chai from 'chai'; import { Language, MultiLangString } from './index.js'; -chai.should(); +const should = chai.should(); describe(`Language`, () => { @@ -13,6 +13,20 @@ describe(`Language`, () => { Language.name.should.equal(`Language`); }); + it(`instantiates with no data`, () => { + + const lang = new Language; + + should.not.exist(lang.abbreviation); + should.not.exist(lang.glottolog); + should.not.exist(lang.iso); + should.not.exist(lang.tags); + + lang.type.should.equal(`Language`); + lang.name.should.be.instanceOf(MultiLangString); + + }); + it(`abbreviation`, () => { const lang = new Language; (() => { lang.abbreviation = undefined; }).should.not.throw(); @@ -22,9 +36,14 @@ describe(`Language`, () => { }); it(`custom property`, () => { + const lang = new Language; (() => { lang.deleted = true; }).should.not.throw(); lang.deleted.should.equal(true); + + const l = new Language({ customProperty: true }); + l.customProperty.should.be.true; + }); it(`glottolog`, () => { @@ -44,12 +63,14 @@ describe(`Language`, () => { }); it(`name`, () => { - const lang = new Language({ name: { eng: `Chitimacha` } }); - lang.name.should.be.instanceOf(MultiLangString); lang.name.get(`eng`).should.equal(`Chitimacha`); + }); + it(`tags`, () => { + const lang = new Language({ tags: { position: `final` } }); + lang.tags.get(`position`).should.equal(`final`); }); it(`type`, () => { diff --git a/src/models/MultiLangString.test.js b/src/models/MultiLangString.test.js index d206c8f..5965e79 100644 --- a/src/models/MultiLangString.test.js +++ b/src/models/MultiLangString.test.js @@ -6,7 +6,7 @@ import chai from 'chai'; import Model from '../core/Model.js'; import MultiLangString from './MultiLangString.js'; -const should = chai.should(); +chai.should(); const modelName = `MultiLangString`; @@ -21,6 +21,11 @@ describe(modelName, () => { describe(`data`, () => { + it(`no data`, () => { + const mls = new MultiLangString; + mls.type.should.equal(`MultiLangString`); + }); + it(`String`, () => { const data = `hello`; const mls = new MultiLangString(data); diff --git a/src/models/Tags.test.js b/src/models/Tags.test.js index 18b4057..2db364a 100644 --- a/src/models/Tags.test.js +++ b/src/models/Tags.test.js @@ -17,6 +17,7 @@ describe(`Tags`, () => { const tags = new Tags; tags.should.be.instanceOf(Map); tags.size.should.equal(0); + tags.type.should.equal(`Tags`); }); it(`throws with bad data`, () => { diff --git a/src/models/Text.js b/src/models/Text.js index 1005f64..f9b5028 100644 --- a/src/models/Text.js +++ b/src/models/Text.js @@ -1,11 +1,13 @@ import Model from '../core/Model.js'; import MultiLangString from './MultiLangString.js'; +import Tags from './Tags.js'; import Utterance from './Utterance.js'; /** * A class representing a linguistic text, formatted according to the [DLx Data Format for a language]{@link https://format.digitallinguistics.io/schemas/Text.html} * @memberof models * @extends core.Model + * @prop {models.Tags} tags - The tags for this text * @prop {models.MultiLangString} title - The title of this text * @prop {core.Collection} utterances - An array of utterances in this text */ @@ -13,15 +15,17 @@ class Text extends Model { /** * Create a new Text - * @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}. + * @param {Object} [data={}] The data for this text, formatted as a [DLx Text object]{@link https://format.digitallinguistics.io/schemas/Text.html}. + * @param {Map|Object} [data.tags] The tags for this text, formatted as a [DLx Tags object]{@link https://format.digitallinguistics.io/schemas/Tags.html}. */ constructor(data = {}) { super(data); - Model.defineArrayProp(this, `utterances`, Utterance); - Model.defineModelProp(this, `title`, MultiLangString); + Model.defineModelProp(this, `tags`, Tags); Model.defineTypeProp(this, `Text`); + Model.defineModelProp(this, `title`, MultiLangString); + Model.defineArrayProp(this, `utterances`, Utterance); Object.assign(this, data); diff --git a/src/models/Text.test.js b/src/models/Text.test.js index a8cca0c..99ec110 100644 --- a/src/models/Text.test.js +++ b/src/models/Text.test.js @@ -1,7 +1,10 @@ +import chai from 'chai'; import Collection from '../core/Collection.js'; import MultiLangString from './MultiLangString.js'; import Text from './Text.js'; +const should = chai.should(); + describe(`Text`, () => { it(`class: Text`, () => { @@ -12,11 +15,23 @@ describe(`Text`, () => { const text = new Text; + should.not.exist(text.tags); text.title.should.be.instanceOf(MultiLangString); + text.type.should.equal(`Text`); text.utterances.should.be.instanceOf(Collection); }); + it(`custom property`, () => { + const text = new Text({ customProperty: true }); + text.customProperty.should.be.true; + }); + + it(`tags`, () => { + const text = new Text({ tags: { position: `final` } }); + text.tags.get(`position`).should.equal(`final`); + }); + it(`title`, () => { const text = new Text({ diff --git a/src/models/Utterance.js b/src/models/Utterance.js index a093888..ffe65ef 100644 --- a/src/models/Utterance.js +++ b/src/models/Utterance.js @@ -1,5 +1,6 @@ import Model from '../core/Model.js'; import MultiLangString from './MultiLangString.js'; +import Tags from './Tags.js'; import Transcription from './Transcription.js'; import Word from './Word.js'; @@ -22,11 +23,12 @@ class Utterance extends Model { super(); + Model.defineModelProp(this, `tags`, Tags); Model.defineModelProp(this, `transcript`, Transcription); Model.defineModelProp(this, `transcription`, Transcription); Model.defineModelProp(this, `translation`, MultiLangString); - Model.defineArrayProp(this, `words`, Word); Model.defineTypeProp(this, `Utterance`); + Model.defineArrayProp(this, `words`, Word); Object.assign(this, data); diff --git a/src/models/Utterance.test.js b/src/models/Utterance.test.js index 9b1564b..9471e74 100644 --- a/src/models/Utterance.test.js +++ b/src/models/Utterance.test.js @@ -1,4 +1,5 @@ import chai from 'chai'; +import Collection from '../core/Collection.js'; import MultiLangString from './MultiLangString.js'; import Transcription from './Transcription.js'; import Utterance from './Utterance.js'; @@ -13,6 +14,7 @@ describe(`Utterance`, () => { const utterance = new Utterance; + should.not.exist(utterance.tags); should.not.exist(utterance.transcript); utterance.transcription.should.be.instanceOf(Transcription); @@ -21,15 +23,26 @@ describe(`Utterance`, () => { utterance.translation.should.be.instanceOf(MultiLangString); utterance.translation.size.should.equal(0); + utterance.type.should.equal(`Utterance`); + + utterance.words.should.be.instanceOf(Collection); + }); - it(`transcript`, () => { + it(`custom property`, () => { + const utterance = new Utterance({ customProperty: true }); + utterance.customProperty.should.be.true; + }); - const utterance = new Utterance({ transcript: testData }); + it(`tags`, () => { + const utterance = new Utterance({ tags: { position: `final` } }); + utterance.tags.get(`position`).should.equal(`final`); + }); + it(`transcript`, () => { + const utterance = new Utterance({ transcript: testData }); utterance.transcript.should.be.instanceOf(Transcription); utterance.transcript.get(`eng`).should.equal(testData.eng); - }); it(`transcription`, () => { @@ -39,12 +52,9 @@ describe(`Utterance`, () => { }); it(`translation`, () => { - const utterance = new Utterance({ translation: testData }); - utterance.translation.should.be.instanceOf(MultiLangString); utterance.translation.get(`eng`).should.equal(testData.eng); - }); it(`type`, () => { diff --git a/src/models/Word.js b/src/models/Word.js index 5805747..58b8c37 100644 --- a/src/models/Word.js +++ b/src/models/Word.js @@ -1,5 +1,6 @@ import Model from '../core/Model.js'; import MultiLangString from './MultiLangString.js'; +import Tags from './Tags.js'; import Transcription from './Transcription.js'; /** @@ -25,6 +26,7 @@ class Word extends Model { Model.defineModelProp(this, `analysis`, Transcription); Model.defineModelProp(this, `gloss`, MultiLangString); Model.defineModelProp(this, `literal`, MultiLangString); + Model.defineModelProp(this, `tags`, Tags); Model.defineModelProp(this, `transcription`, Transcription); Model.defineModelProp(this, `translation`, MultiLangString); Model.defineTypeProp(this, `Word`); diff --git a/src/models/Word.test.js b/src/models/Word.test.js index b0a2c70..43ad814 100644 --- a/src/models/Word.test.js +++ b/src/models/Word.test.js @@ -13,45 +13,45 @@ describe(`Word`, () => { const word = new Word; should.not.exist(word.analysis); should.not.exist(word.literal); + should.not.exist(word.tags); word.transcription.should.be.instanceOf(Transcription); word.transcription.size.should.equal(0); should.not.exist(word.translation); + word.type.should.equal(`Word`); }); it(`analysis`, () => { - const word = new Word({ analysis: testData }); - word.analysis.should.be.instanceOf(Transcription); word.analysis.get(`eng`).should.equal(testData.eng); + }); + it(`custom property`, () => { + const word = new Word({ customProperty: true }); + word.customProperty.should.be.true; }); it(`literal`, () => { - const word = new Word({ literal: testData }); - word.literal.should.be.instanceOf(MultiLangString); word.literal.get(`eng`).should.equal(testData.eng); + }); + it(`tags`, () => { + const word = new Word({ tags: { position: `final` } }); + word.tags.get(`position`).should.equal(`final`); }); it(`transcription`, () => { - const word = new Word({ transcription: testData }); - word.transcription.should.be.instanceOf(Transcription); word.transcription.get(`eng`).should.equal(testData.eng); - }); it(`translation`, () => { - const word = new Word({ translation: testData }); - word.translation.should.be.instanceOf(MultiLangString); word.translation.get(`eng`).should.equal(testData.eng); - }); it(`type`, () => {