Skip to content

Commit b98c4a4

Browse files
authored
NEW: Language.type (#98)
closes #98
1 parent de93d03 commit b98c4a4

File tree

3 files changed

+29
-59
lines changed

3 files changed

+29
-59
lines changed

src/core/Model.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,20 @@ class Model {
5454

5555
}
5656

57+
/**
58+
* Create a "type" property on an object that is non-writable
59+
* @param {Object} object The object to define the property on
60+
* @param {String} type The value to use for the type
61+
*/
62+
static defineTypeProp(object, type) {
63+
Object.defineProperty(object, `type`, {
64+
configurable: true,
65+
enumerable: true,
66+
value: type,
67+
writable: false,
68+
});
69+
}
70+
5771
/**
5872
* A utility function that defines a property on a class that validates its value when set.
5973
* @param {Object} object [description]
@@ -79,20 +93,6 @@ class Model {
7993

8094
}
8195

82-
/**
83-
* Create a "type" property on an object that is non-writable
84-
* @param {Object} object The object to define the property on
85-
* @param {String} type The value to use for the type
86-
*/
87-
static defineTypeProp(object, type) {
88-
Object.defineProperty(object, `type`, {
89-
configurable: true,
90-
enumerable: true,
91-
value: type,
92-
writable: false,
93-
});
94-
}
95-
9696
}
9797

9898
export default Model;

src/models/Language.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ function validateISOCode(input) {
5454
* @prop {String} glottolog - The Glottocode for this language
5555
* @prop {String} iso - The ISO 639-3 code for this language
5656
* @prop {models.MultiLangString} name - The name of this language
57+
* @prop {String} type - "Language"
5758
*/
5859
class Language extends Model {
5960

@@ -72,6 +73,7 @@ class Language extends Model {
7273
// Property Definitions
7374

7475
Model.defineModelProp(this, `name`, MultiLangString);
76+
Model.defineTypeProp(this, `Language`);
7577
Model.defineValidatedProp(this, `abbreviation`, validateAbbreviation);
7678
Model.defineValidatedProp(this, `glottolog`, validateGlottoCode);
7779
Model.defineValidatedProp(this, `iso`, validateISOCode);

src/models/Language.test.js

Lines changed: 13 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -13,81 +13,49 @@ describe(`Language`, () => {
1313
Language.name.should.equal(`Language`);
1414
});
1515

16-
it(`Abbreviation`, () => {
16+
it(`abbreviation`, () => {
1717
const lang = new Language;
1818
(() => { lang.abbreviation = undefined; }).should.not.throw();
1919
(() => { lang.abbreviation = `ctm`; }).should.not.throw();
2020
(() => { lang.abbreviation = `en!`; }).should.throw().with.property(`name`, `AbbreviationError`);
2121
(typeof lang.abbreviation).should.not.equal(`object`);
2222
});
2323

24-
it(`Custom Property`, () => {
24+
it(`custom property`, () => {
2525
const lang = new Language;
2626
(() => { lang.deleted = true; }).should.not.throw();
2727
lang.deleted.should.equal(true);
2828
});
2929

30-
it(`Glottocode`, () => {
30+
it(`glottolog`, () => {
3131
const lang = new Language;
3232
(() => { lang.glottolog = undefined; }).should.not.throw();
3333
(() => { lang.glottolog = `stan1293`; }).should.not.throw();
3434
(() => { lang.glottolog = `stan129`; }).should.throw().with.property(`name`, `GlottoCodeError`);
3535
(typeof lang.glottolog).should.not.equal(`object`);
3636
});
3737

38-
it(`ISO 639-3 code`, () => {
38+
it(`iso`, () => {
3939
const lang = new Language;
4040
(() => { lang.iso = undefined; }).should.not.throw();
4141
(() => { lang.iso = `ctm`; }).should.not.throw();
4242
(() => { lang.iso = `en`; }).should.throw().with.property(`name`, `ISOCodeError`);
4343
(typeof lang.iso).should.not.equal(`object`);
4444
});
4545

46-
describe(`name`, () => {
46+
it(`name`, () => {
4747

48-
it(`class: MultiLangString`, () => {
49-
const lang = new Language();
50-
lang.name.should.be.instanceOf(MultiLangString);
51-
});
48+
const lang = new Language({ name: { eng: `Chitimacha` } });
5249

53-
it(`enumerable`, () => {
54-
const lang = new Language({ name: 'Chitimacha' });
55-
Object.keys(lang).should.contain(`name`);
56-
});
50+
lang.name.should.be.instanceOf(MultiLangString);
51+
lang.name.get(`eng`).should.equal(`Chitimacha`);
5752

58-
it(`Success: String`, () => {
59-
60-
const name = `Chitimacha`;
61-
const lang = new Language;
62-
63-
lang.name = name;
64-
65-
lang.name.get(`eng`).should.equal(name);
66-
67-
});
68-
69-
it(`Success: Object`, () => {
70-
71-
const name = {
72-
eng: `Chitimacha`,
73-
fra: `chitimacha`,
74-
};
75-
76-
const lang = new Language;
77-
78-
lang.name = name;
79-
80-
lang.name.get(`eng`).should.equal(name.eng);
81-
lang.name.get(`fra`).should.equal(name.fra);
82-
83-
});
84-
85-
it(`Error: bad data`, () => {
86-
const lang = new Language;
87-
const setBadLang = () => { lang.name = false; };
88-
setBadLang.should.throw().with.property(`name`, `MultiLangStringDataError`);
89-
});
53+
});
9054

55+
it(`type`, () => {
56+
const lang = new Language;
57+
lang.type.should.equal(`Language`);
58+
(() => { lang.type = `language`; }).should.throw();
9159
});
9260

9361
});

0 commit comments

Comments
 (0)