Skip to content

Commit 3142656

Browse files
authored
NEW: add type properties to models (#30)
closes #30
1 parent b98c4a4 commit 3142656

17 files changed

+106
-75
lines changed

src/core/Collection.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import Model from './Model.js';
2+
13
/* eslint-disable
24
no-constructor-return,
35
*/
@@ -11,18 +13,20 @@ class Collection extends Array {
1113

1214
/**
1315
* Create a new Collection
14-
* @param {Function} Model The class to use as the model for each item in the collection
16+
* @param {Function} ItemModel The class to use as the model for each item in the collection
1517
* @param {Array} [array=[]] The array to instantiate the Collection with
1618
*/
17-
constructor(Model, array = []) {
19+
constructor(ItemModel, array = []) {
1820

1921
super();
2022

23+
Model.defineTypeProp(this, `Collection`);
24+
2125
const proxy = new Proxy(this, {
2226
set(target, prop, val, prox) {
2327

2428
if (Number.isInteger(Number(prop))) {
25-
val = new Model(val); // eslint-disable-line no-param-reassign
29+
val = new ItemModel(val); // eslint-disable-line no-param-reassign
2630
}
2731

2832
return Reflect.set(target, prop, val, prox);

src/core/Collection.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,9 @@ describe(`Collection`, () => {
3434

3535
});
3636

37+
it(`type`, () => {
38+
const collection = new Collection;
39+
collection.type.should.equal(`Collection`);
40+
});
41+
3742
});

src/core/Model.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ class Model {
6363
Object.defineProperty(object, `type`, {
6464
configurable: true,
6565
enumerable: true,
66-
value: type,
67-
writable: false,
66+
get() {
67+
return type;
68+
},
69+
set() {}, // eslint-disable-line no-empty-function
6870
});
6971
}
7072

src/core/Model.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ describe(`Model`, () => {
4848

4949
});
5050

51+
it(`defineTypeProp`, () => {
52+
53+
const obj = {};
54+
55+
Model.defineTypeProp(obj, `Test`);
56+
57+
obj.type.should.equal(`Test`);
58+
(() => { obj.type = `test`; }).should.not.throw();
59+
obj.type.should.equal(`Test`);
60+
61+
});
62+
5163
it(`defineValidatedProp`, () => {
5264

5365
const validate = val => {

src/models/Language.test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ describe(`Language`, () => {
5555
it(`type`, () => {
5656
const lang = new Language;
5757
lang.type.should.equal(`Language`);
58-
(() => { lang.type = `language`; }).should.throw();
5958
});
6059

6160
});

src/models/MultiLangString.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import isLanguageTag from '../utilities/types/isLanguageTag.js';
2+
import Model from '../core/Model.js';
23

34
/**
45
* Validates a language tag. Throws a type error if the input is not a valid IETF language tag.
@@ -76,6 +77,8 @@ class MultiLangString extends Map {
7677

7778
super(Object.entries(data));
7879

80+
Model.defineTypeProp(this, `MultiLangString`);
81+
7982
}
8083

8184
set(key, val) {

src/models/MultiLangString.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,9 @@ describe(modelName, () => {
109109

110110
});
111111

112-
it(`.type (should not exist)`, () => {
113-
should.not.exist(new MultiLangString().type);
112+
it(`.type`, () => {
113+
const mls = new MultiLangString;
114+
mls.type.should.equal(`MultiLangString`);
114115
});
115116

116117
});

src/models/Text.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Text extends Model {
2121

2222
Model.defineArrayProp(this, `utterances`, Utterance);
2323
Model.defineModelProp(this, `title`, MultiLangString);
24+
Model.defineTypeProp(this, `Text`);
2425

2526
Object.assign(this, data);
2627

src/models/Text.test.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import Text from './Text.js';
44

55
describe(`Text`, () => {
66

7+
it(`class: Text`, () => {
8+
Text.name.should.equal(`Text`);
9+
});
10+
711
it(`instantiates with no data`, () => {
812

913
const text = new Text;
@@ -13,13 +17,27 @@ describe(`Text`, () => {
1317

1418
});
1519

16-
it(`instantiates with data`, () => {
20+
it(`title`, () => {
1721

18-
const data = {
22+
const text = new Text({
1923
title: {
2024
eng: `The Little Prince`,
2125
fra: `Le Petit Prince`,
2226
},
27+
});
28+
29+
text.title.get(`fra`).should.equal(`Le Petit Prince`);
30+
31+
});
32+
33+
it(`type`, () => {
34+
const text = new Text;
35+
text.type.should.equal(`Text`);
36+
});
37+
38+
it(`utterances`, () => {
39+
40+
const text = new Text({
2341
utterances: [
2442
{
2543
transcription: {
@@ -28,14 +46,9 @@ describe(`Text`, () => {
2846
translation: `the little prince`,
2947
},
3048
],
31-
};
32-
33-
const text = new Text(data);
49+
});
3450

35-
text.title.get(`fra`).should.equal(data.title.fra);
3651
text.utterances.length.should.equal(1);
37-
text.utterances[0].transcription.get(`fra`).should.equal(`le petite prince`);
38-
text.utterances[0].translation.get(`eng`).should.equal(`the little prince`);
3952

4053
});
4154

src/models/Transcription.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import isAbbreviation from '../utilities/types/isAbbreviation.js';
2+
import Model from '../core/Model.js';
23

34
/**
45
* Validates an abbreviation. Throws a type error if the input is not a valid abbreviation.
@@ -63,6 +64,8 @@ class Transcription extends Map {
6364

6465
super(Object.entries(data));
6566

67+
Model.defineTypeProp(this, `Transcription`);
68+
6669
}
6770

6871
set(key, val) {

0 commit comments

Comments
 (0)