Skip to content

Commit 5fb92a3

Browse files
authored
NEW: Language.name (#59)
closes #59
1 parent e1f0c3e commit 5fb92a3

15 files changed

Lines changed: 255 additions & 22 deletions

File tree

.eslintrc.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# ESLint v6.7.2
2+
parser: babel-eslint
23
parserOptions:
34
ecmaVersion: 11
45
sourceType: module

package-lock.json

Lines changed: 151 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"access": "public"
3636
},
3737
"devDependencies": {
38+
"babel-eslint": "^10.1.0",
3839
"eslint": "^6.8.0",
3940
"esm": "^3.2.25",
4041
"gh-pages": "^2.2.0",

src/core/Model.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
/**
2-
* A base Model class
2+
* A base Model class. Most other models are instances of this base Model.
33
* @memberof core
44
*/
55
class Model {
66

77
/**
88
* Create a new Model
9-
* @param {Object} [data={}] The data to populate this model with
109
*/
11-
constructor(data = {}) {
12-
Object.assign(this, data);
13-
}
10+
constructor() {} // eslint-disable-line
1411

1512
}
1613

src/core/Model.test.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,4 @@ describe(`Model`, () => {
66
expect(Model.name).toBe(`Model`);
77
});
88

9-
it(`copies data to the instance / allows custom properties`, () => {
10-
const hello = `jambo`;
11-
const model = new Model({ hello });
12-
expect(model.hello).toBe(hello);
13-
});
14-
159
});

src/core/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/**
2+
* Base classes and other code reused across this library. Most users will not need to use this module.
23
* @namespace core
34
*/
45

src/core/index.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
const { core: { Model } } = require(`../../test/index.js`);
1+
const { core } = require(`../../test/index.js`);
2+
3+
const { Model } = core;
24

35
/**
46
* Check that the core module has the expected exports

src/models/Language.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,37 @@
1-
import Model from '../core/Model.js';
1+
import Model from '../core/Model.js';
2+
import MultiLangString from './MultiLangString.js';
23

34
/**
4-
* A class representing a language
5+
* A class representing a language, formatted according to the [DLx Data Format for a language]{@link https://format.digitallinguistics.io/schemas/Language.html}
56
* @memberof models
67
* @extends Model
78
*/
89
class Language extends Model {
910

11+
#name;
12+
1013
/**
1114
* Create a new Language
12-
* @param {Object} [data={}] The data to use for this Language object
15+
* @param {Object} [data={}] The data to use for this Language object. Data should be formatted according to the [DLx Data Format's guidelines for Language data]{@link https://format.digitallinguistics.io/schemas/Language.html}.
1316
*/
1417
constructor(data = {}) {
15-
super(data);
18+
19+
super();
20+
21+
this.name = new MultiLangString(data.name);
22+
23+
}
24+
25+
/**
26+
* The name of this language, as a [MultiLangString]{@link models.MultiLangString}
27+
* @type {Map}
28+
*/
29+
get name() {
30+
return this.#name;
31+
}
32+
33+
set name(val) {
34+
this.#name = new MultiLangString(val);
1635
}
1736

1837
}

src/models/Language.test.js

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,60 @@
1-
const { models: { Language } } = require(`../../test`);
1+
/* eslint-disable
2+
max-nested-callbacks,
3+
*/
4+
5+
const { models } = require(`../../test`);
6+
7+
const {
8+
MultiLangString,
9+
Language,
10+
} = models;
211

312
describe(`Language`, () => {
413

5-
it(`is a Language class`, () => {
14+
it(`class: Language`, () => {
615
expect(Language.name).toBe(`Language`);
716
});
817

18+
describe(`Language.prototype.name`, () => {
19+
20+
it(`class: MultiLangString`, () => {
21+
const lang = new Language();
22+
expect(lang.name).toBeInstanceOf(MultiLangString);
23+
});
24+
25+
it(`Success: String`, () => {
26+
27+
const name = `Chitimacha`;
28+
const lang = new Language;
29+
30+
lang.name = name;
31+
32+
expect(lang.name.get(`eng`)).toBe(name);
33+
34+
});
35+
36+
it(`Success: Object`, () => {
37+
38+
const name = {
39+
eng: `Chitimacha`,
40+
fra: `chitimacha`,
41+
};
42+
43+
const lang = new Language;
44+
45+
lang.name = name;
46+
47+
expect(lang.name.get(`eng`)).toBe(name.eng);
48+
expect(lang.name.get(`fra`)).toBe(name.fra);
49+
50+
});
51+
52+
it(`Error: bad data`, () => {
53+
const lang = new Language;
54+
const setBadLang = () => { lang.name = false; };
55+
expect(setBadLang).toThrowMatching(e => e.name === `MultiLangStringDataError`);
56+
});
57+
58+
});
59+
960
});

src/models/MultiLangString.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ function validateString(input) {
5555
}
5656

5757
/**
58-
* A class representing a Multi-Language Text / String
58+
* A class representing a Multi-Language Text / String, as a JavaScript Map Object. See the [DLx Data Format]{@link https://format.digitallinguistics.io/schemas/MultiLangString.html} for information about formatting Multi-Language Strings.
5959
* @memberof models
6060
* @extends Map
6161
*

0 commit comments

Comments
 (0)