Skip to content
This repository was archived by the owner on Feb 7, 2020. It is now read-only.

Commit b390d1c

Browse files
author
Bridget Almas
committed
fix #23
1 parent 2ebf6a0 commit b390d1c

File tree

8 files changed

+90
-43
lines changed

8 files changed

+90
-43
lines changed

dist/alpheios-data-models.js

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

dist/alpheios-data-models.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/alpheios-data-models.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/feature.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ export default class Feature {
6363
normalized = [[data, this.defaultSortOrder]]
6464
} else if (!Array.isArray(data[0])) {
6565
// Multiple values without any sort order, default sort order will be used
66-
normalized = data.map((v, i) => [v, i + 1])
66+
// we reverse because sortOrder is numeric descending (i.e. 2 is before 1)
67+
normalized = data.map((v, i) => [v, data.length - i])
6768
} else {
6869
// Value has all the data, including a sort order
6970
normalized = data
@@ -154,7 +155,18 @@ export default class Feature {
154155
* Sort order is deterministic.
155156
*/
156157
sort () {
157-
this._data.sort((a, b) => a.sortOrder !== b.sortOrder ? a.sortOrder - b.sortOrder : a.value.localeCompare(b.value))
158+
this._data.sort((a, b) => a.sortOrder !== b.sortOrder ? b.sortOrder - a.sortOrder : a.value.localeCompare(b.value))
159+
}
160+
161+
/**
162+
* Compares a feature's values to another feature's values for sorting
163+
* @param {Feature} otherFeature the feature to compare this feature's values to
164+
* @return {integer} >=1 if this feature should be sorted first, 0 if they are equal and -1 if this feature should be sorted second
165+
*/
166+
compareTo (otherFeature) {
167+
// the data values are sorted upon construction and insertion so we only should need to look at the first values
168+
// feature sortOrders are descending (i.e. 5 sorts higher than 1)
169+
return otherFeature._data[0].sortOrder - this._data[0].sortOrder
158170
}
159171

160172
get items () {

src/lexeme.js

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,22 +85,15 @@ class Lexeme {
8585
static getSortByTwoLemmaFeatures (primary, secondary) {
8686
return (a, b) => {
8787
if (a.lemma.features[primary] && b.lemma.features[primary]) {
88-
if (a.lemma.features[primary].sortOrder < b.lemma.features[primary].sortOrder) {
89-
return 1
90-
} else if (a.lemma.features[primary].sortOrder > b.lemma.features[primary].sortOrder) {
91-
return -1
88+
let primarySort = a.lemma.features[primary].compareTo(b.lemma.features[primary])
89+
if (primarySort !== 0) {
90+
return primarySort
9291
} else if (a.lemma.features[secondary] && b.lemma.features[secondary]) {
93-
if (a.lemma.features[secondary].sortOrder < b.lemma.features[secondary].sortOrder) {
94-
return 1
95-
} else if (a.lemma.features[secondary].sortOrder > b.lemma.features[secondary].sortOrder) {
96-
return -1
97-
} else if (a.lemma.features[secondary] && !b.lemma.features[secondary]) {
98-
return -1
99-
} else if (!a.lemma.features[secondary] && b.lemma.features[secondary]) {
100-
return 1
101-
} else {
102-
return 0
103-
}
92+
return a.lemma.features[secondary].compareTo(b.lemma.features[secondary])
93+
} else if (a.lemma.features[secondary] && !b.lemma.features[secondary]) {
94+
return -1
95+
} else if (!a.lemma.features[secondary] && b.lemma.features[secondary]) {
96+
return 1
10497
}
10598
} else if (a.lemma.features[primary] && !b.lemma.features[primary]) {
10699
return -1

tests/feature.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,31 @@ describe('Feature object', () => {
3939
let b = new Feature(Feature.types.note, 'valuea', Constants.LANG_LATIN)
4040
expect(a.isEqual(b)).toBeFalsy()
4141
})
42+
43+
it('expects to Feature a to be sorted after Feature b', () => {
44+
let a = new Feature(Feature.types.frequency, [['lower', 1]], Constants.LANG_GREEK)
45+
let b = new Feature(Feature.types.frequency, [['higher', 2]], Constants.LANG_GREEK)
46+
expect(a.compareTo(b)).toBeGreaterThan(0)
47+
expect([a, b].sort((a, b) => a.compareTo(b))).toEqual([b, a])
48+
})
49+
50+
it('expects to Feature a to be sorted before Feature b', () => {
51+
let a = new Feature(Feature.types.frequency, [['lower', 3]], Constants.LANG_GREEK)
52+
let b = new Feature(Feature.types.frequency, [['higher', 1]], Constants.LANG_GREEK)
53+
expect(a.compareTo(b)).toBeLessThan(0)
54+
expect([a, b].sort((a, b) => a.compareTo(b))).toEqual([a, b])
55+
})
56+
57+
it('expects to Feature a to be sorted the same as Feature b', () => {
58+
let a = new Feature(Feature.types.frequency, [['same', 1]], Constants.LANG_GREEK)
59+
let b = new Feature(Feature.types.frequency, [['same', 1]], Constants.LANG_GREEK)
60+
expect(a.compareTo(b)).toEqual(0)
61+
expect([a, b].sort((a, b) => a.compareTo(b))).toEqual([a, b])
62+
})
63+
it('expects to multi-valued Features to sort correctly', () => {
64+
let a = new Feature(Feature.types.frequency, [['lower', 1], ['highest', 3]], Constants.LANG_GREEK)
65+
let b = new Feature(Feature.types.frequency, [['higher', 2]], Constants.LANG_GREEK)
66+
expect(a.compareTo(b)).toBeLessThan(0)
67+
expect([a, b].sort((a, b) => a.compareTo(b))).toEqual([a, b])
68+
})
4269
})

tests/feature_type.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ describe('FeatureType', () => {
8383
test('orderedFeatures() should return type features in an indexed order.', () => {
8484
expect(featureType.orderedFeatures).toMatchObject([
8585
{'languageID': Constants.LANG_LATIN, 'type': 'declension', '_data': [{'sortOrder': 1, 'value': 'first'}]},
86-
{'languageID': Constants.LANG_LATIN, 'type': 'declension', '_data': [{'sortOrder': 1, 'value': 'second'}, {'sortOrder': 2, 'value': 'third'}]},
86+
{'languageID': Constants.LANG_LATIN, 'type': 'declension', '_data': [{'sortOrder': 2, 'value': 'second'}, {'sortOrder': 1, 'value': 'third'}]},
8787
{'languageID': Constants.LANG_LATIN, 'type': 'declension', '_data': [{'sortOrder': 1, 'value': 'fourth'}]}
8888
])
8989
})

tests/lexeme.test.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,31 +51,41 @@ describe('Lexeme object', () => {
5151
let mockLexemeOne = {
5252
lemma: {
5353
features: {
54-
freq: new Feature(Feature.types.frequency, [''], Constants.LANG_GREEK, 5),
55-
pofs: new Feature(Feature.types.part, [''], Constants.LANG_GREEK, 5)
54+
freq: new Feature(Feature.types.frequency, [['most frequent', 5]], Constants.LANG_GREEK, 1),
55+
pofs: new Feature(Feature.types.part, ['least important pofs', 1], Constants.LANG_GREEK, 1)
5656
}
5757
}
5858
}
5959
let mockLexemeTwo = {
6060
lemma: {
6161
features: {
62-
freq: new Feature(Feature.types.frequency, [''], Constants.LANG_GREEK, 5),
63-
pofs: new Feature(Feature.types.part, [''], Constants.LANG_GREEK, 5)
62+
freq: new Feature(Feature.types.frequency, [['less frequent', 3]], Constants.LANG_GREEK, 1),
63+
pofs: new Feature(Feature.types.part, [['more important pofs', 6]], Constants.LANG_GREEK, 1)
6464
}
6565
}
6666
}
6767
let mockLexemeThree = {
6868
lemma: {
6969
features: {
70-
pofs: new Feature(Feature.types.part, [''], Constants.LANG_GREEK, 7)
70+
pofs: new Feature(Feature.types.part, [['most important pofs', 7]], Constants.LANG_GREEK, 1)
71+
}
72+
}
73+
}
74+
let mockLexemeFour = {
75+
lemma: {
76+
features: {
77+
freq: new Feature(Feature.types.frequency, [['most frequent', 5]], Constants.LANG_GREEK, 1),
78+
pofs: new Feature(Feature.types.part, [['more important pofs', 6]], Constants.LANG_GREEK, 1)
7179
}
7280
}
7381
}
7482
let lexemes = [ mockLexemeOne, mockLexemeTwo ]
7583
let sortFunc = Lexeme.getSortByTwoLemmaFeatures('freq', 'pofs')
76-
expect(lexemes.sort(sortFunc)).toEqual([mockLexemeTwo, mockLexemeOne])
84+
expect(lexemes.sort(sortFunc)).toEqual([mockLexemeOne, mockLexemeTwo])
7785
lexemes = [ mockLexemeTwo, mockLexemeThree ]
7886
expect(lexemes.sort(sortFunc)).toEqual([mockLexemeTwo, mockLexemeThree])
87+
lexemes = [ mockLexemeOne, mockLexemeFour ]
88+
expect(lexemes.sort(sortFunc)).toEqual([mockLexemeFour, mockLexemeOne])
7989
})
8090

8191
test('isPopulated', () => {

0 commit comments

Comments
 (0)