Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Utterance#150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Merged
Utterance #150
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f532bc8
fix module loading
dwhieb eaa8746
use Chai instead of Expect.js
dwhieb 452b624
update tests to use Chai syntax
dwhieb bb5caf9
use "should" assertion style
dwhieb ee3846d
tests passing
dwhieb 2b72144
write skeleton tests
dwhieb b52fc14
write skeleton tests for Transcription object
dwhieb 4c7380a
Transcription tests passing
dwhieb 5776a86
all tests passing
dwhieb 3206a2c
update docs
dwhieb d24de12
update Node version for GitHub Actions testing workflow
dwhieb fd3c665
Merge branch 'master' into issue-145
dwhieb c10d1bc
add Transcription and Utterance references to src/models/index.js
dwhieb 834682b
add Transcription and Utterance to models module tests
dwhieb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /** | ||
| * @module models.Transcription | ||
| */ | ||
| import isAbbreviation from '../utilities/types/isAbbreviation.js'; | ||
| /** | ||
| * Validates an abbreviation. Throws a type error if the input is not a valid abbreviation. | ||
| * @param {Any} input The input to validate | ||
| */ | ||
| function validateAbbreviation(input) { | ||
| if (!isAbbreviation(input)) { | ||
| const e = new TypeError(`Each orthography key must be a valid abbreviation.`); | ||
| e.name = `TranscriptionOrthoError`; | ||
| throw e; | ||
| } | ||
| } | ||
| /** | ||
| * Validates a String for Transcription values. Throws a type error if the input is not a String. | ||
| * @param {Any} input The input to validate | ||
| */ | ||
| function validateString(input) { | ||
| if (typeof input !== `string`) { | ||
| const e = new TypeError(`Each piece of data in a Transcription must be a String of text in a particular orthography.`); | ||
| e.name = `TranscriptionStringError`; | ||
| throw e; | ||
| } | ||
| } | ||
| /** | ||
| * A class representing a Transcription, as a JavaScript Map Object. See the [DLx Data Format]{@link https://format.digitallinguistics.io/schemas/Transcription.html} for information about formatting Transcription objects. | ||
| * @memberof models | ||
| * @extends Map | ||
| * | ||
| * @example | ||
| * const transcription = new Transcription({ | ||
| * latin: `hello`, | ||
| * IPA: `hɛˈloʊ`, | ||
| * }); | ||
| * | ||
| * console.log(transcription.get(`ipa`)); // hɛˈloʊ | ||
| */ | ||
| class Transcription extends Map { | ||
| /** | ||
| * Create a new Transcription | ||
| * @param {Map|Object} [data={}] The data to use for this Transcription, as either a Map or an Object. | ||
| */ | ||
| constructor(data = {}) { | ||
| if (typeof data !== `object`) { | ||
| const e = new TypeError(`The data passed to the Transcription class must be a Map or Object.`); | ||
| e.name = `TranscriptionDataError`; | ||
| throw e; | ||
| } | ||
| // eslint-disable-next-line no-param-reassign | ||
| data = data instanceof Map ? Object.fromEntries(data) : data; | ||
| Object.keys(data).forEach(validateAbbreviation); | ||
| Object.values(data).forEach(validateString); | ||
| super(Object.entries(data)); | ||
| } | ||
| set(key, val) { | ||
| validateAbbreviation(key); | ||
| validateString(val); | ||
| return super.set(key, val); | ||
| } | ||
| toJSON() { | ||
| return Object.fromEntries(this); | ||
| } | ||
| } | ||
| export default Transcription; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| /** | ||
| * @module models.MultiLangString | ||
| */ | ||
| import Transcription from './Transcription.js'; | ||
| describe(`Transcription`, () => { | ||
| const testData = { | ||
| IPA: `hɛˈloʊ`, | ||
| Latin: `hello`, | ||
| }; | ||
| it(`is an empty Map when no data is provided`, () => { | ||
| const txn = new Transcription; | ||
| txn.should.be.instanceOf(Map); | ||
| txn.size.should.equal(0); | ||
| }); | ||
| it(`maps orthographies to transcriptions`, () => { | ||
| const txn = new Transcription(testData); | ||
| txn.get(`IPA`).should.equal(`hɛˈloʊ`); | ||
| txn.get(`Latin`).should.equal(`hello`); | ||
| }); | ||
| it(`only allows abbreviations as keys`, () => { | ||
| () => new Transcription({ 'bad key': 'hello' }) | ||
| .should.throw() | ||
| .with.property(`name`, `TranscriptionOrthoError`); | ||
| }); | ||
| it(`only allows strings as values`, () => { | ||
| () => new Transcription({ eng: 0 }) | ||
| .should.throw() | ||
| .with.property(`name`, `TranscriptionStringError`); | ||
| }); | ||
| it(`validates new keys`, () => { | ||
| const txn = new Transcription; | ||
| () => txn.set(`bad key`, `hello`) | ||
| .should.throw() | ||
| .with.property(`name`, `TranscriptionOrthoError`); | ||
| }); | ||
| it(`validates new values`, () => { | ||
| const txn = new Transcription; | ||
| () => txn.set(`eng`, undefined) | ||
| .should.throw() | ||
| .with.property(`name`, `TranscriptionStringError`); | ||
| }); | ||
| it(`stringifies as an Object`, () => { | ||
| const txn = new Transcription(testData); | ||
| const pojo = JSON.parse(JSON.stringify(txn)); | ||
| pojo.IPA.should.equal(testData.IPA); | ||
| pojo.Latin.should.equal(testData.Latin); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import Model from '../core/Model.js'; | ||
| import MultiLangString from './MultiLangString.js'; | ||
| import Transcription from './Transcription.js'; | ||
| /** | ||
| * A class representing an utterance. | ||
| * @memberof models | ||
| * @extends core.Model | ||
| */ | ||
| class Utterance extends Model { | ||
| /** | ||
| * The transcript of this utterance, as a Transcription object | ||
| * @type {Transcription} | ||
| */ | ||
| #transcript; | ||
| /** | ||
| * The transcription of this utterance, as a Transcription object | ||
| * @type {Transcription} | ||
| */ | ||
| #transcription; | ||
| /** | ||
| * The translation of this utterance, as a MultiLangString object | ||
| * @type {MultiLangString} | ||
| */ | ||
| #translation; | ||
| /** | ||
| * Create a new Utterance | ||
| * @param {Object} [data={}] The data to use for this Utterance | ||
| */ | ||
| constructor(data = {}) { | ||
| super(); | ||
| Model.defineModelProp(this, `transcript`, Transcription); | ||
| Model.defineModelProp(this, `transcription`, Transcription); | ||
| Model.defineModelProp(this, `translation`, MultiLangString); | ||
| Object.assign(this, data); | ||
| // Required properties | ||
| // TODO: replace with: this.transcription ??= new Transcription; | ||
| this.transcription = this.transcription ?? new Transcription; | ||
| this.translation = this.translation ?? new MultiLangString; | ||
| } | ||
| } | ||
| export default Utterance; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import chai from 'chai'; | ||
| import MultiLangString from './MultiLangString.js'; | ||
| import Transcription from './Transcription.js'; | ||
| import Utterance from './Utterance.js'; | ||
| const should = chai.should(); | ||
| describe(`Utterance`, () => { | ||
| const testData = { eng: 'Hello world!' }; | ||
| it(`instantiates without data`, () => { | ||
| (() => new Utterance).should.not.throw(); | ||
| }); | ||
| it(`transcript is a Transcription object`, () => { | ||
| const utterance = new Utterance({ transcript: testData }); | ||
| utterance.transcript.should.be.instanceOf(Transcription); | ||
| utterance.transcript.get(`eng`).should.equal(testData.eng); | ||
| }); | ||
| it(`transcript is undefined if absent`, () => { | ||
| const utterance = new Utterance; | ||
| should.not.exist(utterance.transcript); | ||
| }); | ||
| it(`transcription is a Transcription object`, () => { | ||
| const utterance = new Utterance({ transcription: testData }); | ||
| utterance.transcription.should.be.instanceOf(Transcription); | ||
| utterance.transcription.get(`eng`).should.equal(testData.eng); | ||
| }); | ||
| it(`transcription is an empty Transcription (Map) object if absent`, () => { | ||
| const utterance = new Utterance; | ||
| utterance.transcription.should.be.instanceOf(Transcription); | ||
| utterance.transcription.size.should.equal(0); | ||
| }); | ||
| it(`translation is a MultiLangString object`, () => { | ||
| const utterance = new Utterance({ translation: testData }); | ||
| utterance.translation.should.be.instanceOf(MultiLangString); | ||
| utterance.translation.get(`eng`).should.equal(testData.eng); | ||
| }); | ||
| it(`translation is an empty MultiLangString if absent`, () => { | ||
| const utterance = new Utterance; | ||
| utterance.translation.should.be.instanceOf(MultiLangString); | ||
| utterance.translation.size.should.equal(0); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.