Skip to content

Commit 72851b0

Browse files
committed
fix #115; default setting for step value
1 parent e1b9fee commit 72851b0

File tree

4 files changed

+149
-47
lines changed

4 files changed

+149
-47
lines changed

spec/learnwords2/LWBoxOfQuestionsSpec.js

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ describe("BoxOfQuestions construction", function() {
3131
expect(LW.db.dbName).toBe("learnWords");
3232
expect(LW.db.numberOfWords()).toBe(12);
3333

34-
expect((LW.wordsToRepeat()).length).toBe(12);
35-
3634

3735
});
3836

@@ -65,7 +63,6 @@ describe("BoxOfQuestions construction", function() {
6563
expect(LW.db.dbName).toBe("learnWords");
6664
expect(LW.db.numberOfWords()).toBe(12);
6765

68-
expect((LW.wordsToRepeat()).length).toBe(12);
6966

7067
});
7168

@@ -85,10 +82,41 @@ describe("BoxOfQuestions", function() {
8582

8683
LW = BoxOfQuestions(LWdb('learnWords'));
8784
LW.importFrom(wordlist);
88-
85+
86+
87+
// setup a particular set of step value
88+
89+
var allWords = LW.db.allWords();
90+
// FIXME why do I need to indicate 0 as a string value?
91+
allWords[0].step = 0; // has not been answered yet
92+
allWords[1].step = 0;
93+
allWords[2].step = 0;
94+
allWords[3].step = 0;
95+
96+
allWords[4].step = 1; // word has been answered once
97+
allWords[5].step = 1;
98+
allWords[6].step = 2; // word has been answered two times
99+
allWords[7].step = 2;
100+
101+
LW.db.putWord(allWords[0]);
102+
LW.db.putWord(allWords[1]);
103+
LW.db.putWord(allWords[2]);
104+
LW.db.putWord(allWords[3]);
105+
LW.db.putWord(allWords[4]);
106+
LW.db.putWord(allWords[5]);
107+
LW.db.putWord(allWords[6]);
108+
LW.db.putWord(allWords[7]);
109+
89110
});
90111

91112

113+
it("should indicate the correct library version", function() {
114+
115+
expect(LW).toHaveString("version");
116+
expect(LW.version).toBe('0.2.1');
117+
118+
});
119+
92120

93121

94122
it("should support the Box API 2", function() {
@@ -113,25 +141,28 @@ describe("BoxOfQuestions", function() {
113141

114142

115143

116-
xit("should indicate the correct library version", function() {
117144

118-
expect(LW).toHaveMethod("version");
119-
expect(LW.version).toBe('0.2.1');
120-
121-
});
145+
it("should be able to import questions", function() {
122146

147+
expect(LW).not.toBe(null);
123148

124149

150+
expect(LW.db).toHaveMethod("loadWords");
125151

152+
expect(LW).toHaveMethod("importFrom");
126153

127-
it("should be able to import questions", function() {
154+
expect(LW.db.numberOfWords()).toBe(12);
128155

129-
expect(LW).not.toBe(null);
156+
var allWords = LW.db.allWords();
157+
var aWord = allWords[0];
158+
expect(aWord.step).toBe(0);
130159

131160

132-
expect(LW.db).toHaveMethod("loadWords");
161+
aWord = allWords[4];
162+
expect(aWord.step).toBe(1);
133163

134-
expect(LW).toHaveMethod("importFrom");
164+
aWord = allWords[8];
165+
expect(aWord.step).toBe(-1);
135166

136167
});
137168

@@ -141,7 +172,7 @@ describe("BoxOfQuestions", function() {
141172

142173

143174

144-
xit("should have a helper function to get random integers", function(){
175+
it("should have a helper function to get random integers", function(){
145176

146177
expect(LW).not.toBe(null);
147178

@@ -169,39 +200,37 @@ describe("BoxOfQuestions", function() {
169200

170201
it("should be able to indicate which words are to be repeated", function() {
171202

172-
173-
// LW.wordsToRepeat(); issue #63
174-
175203
expect(LW).not.toBe(null);
176204
expect(LW).toHaveMethod("wordsToRepeat");
177205

178-
var r0 = LW.wordsToRepeat();
179206

207+
var r0 = LW.wordsToRepeat();
208+
180209
expect(r0.length).toBeNumber();
181-
expect(r0.length).toBe(12);
210+
expect(r0.length).toBe(8);
182211

183212
LW.question();
184213
LW.moveQuestionForward();
185214

186215
var r1 = LW.wordsToRepeat();
187216
expect(r1.length).toBeNumber();
188-
expect(r1.length).toBe(11);
217+
expect(r1.length).toBe(7);
189218

190219

191220
LW.question();
192221
LW.moveQuestionForward();
193222

194223
var r2 = LW.wordsToRepeat();
195224
expect(r2.length).toBeNumber();
196-
expect(r2.length).toBe(10);
225+
expect(r2.length).toBe(6);
197226

198227

199228
LW.question();
200229
LW.moveQuestionForward();
201230

202231
var r2 = LW.wordsToRepeat();
203232
expect(r2.length).toBeNumber();
204-
expect(r2.length).toBe(9);
233+
expect(r2.length).toBe(5);
205234

206235

207236
});
@@ -264,7 +293,7 @@ describe("BoxOfQuestions", function() {
264293
noOfQuestions = noOfQuestions +1};
265294
} while (q);
266295

267-
expect(noOfQuestions).toBe(12);
296+
expect(noOfQuestions).toBe(8);
268297
});
269298

270299

spec/learnwords2/LWdbSpec.js

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,74 @@ describe("Database LWdb", function() {
106106
});
107107

108108

109+
it("should be able to store a modified word", function() {
110+
expect(lwdb.numberOfWords()).toBe(0);
111+
var i = parseInt(Math.floor(Math.random()*this.wordList.length));
112+
var aWord = this.wordList[i];
113+
var id = (i+1);
114+
aWord._id = id;
115+
lwdb.putWord(aWord);
116+
expect(lwdb.numberOfWords()).toBe(1);
117+
118+
var wordFromDB = lwdb.getWord(id);
119+
wordFromDB.step = 5;
120+
lwdb.putWord(wordFromDB);
121+
122+
// read it back and check if the value has been stored
123+
expect((lwdb.getWord(id)).step).toBe(5);
124+
125+
});
126+
127+
128+
129+
it("should be able to store a step value of 0", function() {
130+
131+
// set up db entry
132+
expect(lwdb.numberOfWords()).toBe(0);
133+
var aWord = this.wordList[3];
134+
aWord._id = 4; // ids start with 1
135+
136+
lwdb.putWord(aWord);
137+
expect(lwdb.numberOfWords()).toBe(1);
138+
139+
140+
141+
// Test part 1 with value 99
142+
// get db entry
143+
var wordFromDB = lwdb.getWord(4);
144+
145+
// change step value to 99
146+
wordFromDB.step = 99;
147+
lwdb.putWord(wordFromDB);
148+
149+
// read it back and check if the value has been stored
150+
var word = lwdb.getWord(4);
151+
152+
expect(word.step).toBe(99);
153+
154+
155+
// Test part 2 with value 0
156+
// get db entry
157+
var wordFromDB = lwdb.getWord(4);
158+
159+
// change step value to 0
160+
wordFromDB.step = 0;
161+
162+
lwdb.putWord(wordFromDB);
163+
164+
// read it back and check if the value has been stored
165+
var wordR = lwdb.getWord(4);
166+
167+
expect(wordR.step).toBe(0);
168+
169+
// Note: The problem was that
170+
171+
// aWord.step may mean different things when aWord.step == 0
172+
173+
});
174+
175+
176+
109177
it("should be able to get a word", function() {
110178

111179
var newWord = {
@@ -130,7 +198,7 @@ describe("Database LWdb", function() {
130198
expect(r.translate).toBe("die Melone");
131199

132200
expect(r).toHaveNumber("step");
133-
expect(r.step).toBe(0);
201+
expect(r.step).toBe(-1);
134202

135203
expect(r).toHaveNumber("date");
136204
expect(r.date).toBe(0);
@@ -336,6 +404,15 @@ describe("Database LWdb", function() {
336404
});
337405

338406

407+
it("should be able to answer a default intial step value", function() {
408+
var settings = lwdb.getSettings();
409+
expect(settings).not.toBe(null);
410+
expect(settings).toBeObject();
411+
expect(settings.defaultInitialStepValue).toBe(-1);
412+
413+
});
414+
415+
339416
});
340417

341418
});

src/BoxOfQuestions.js

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ function BoxOfQuestions(db) {
6767

6868
return {
6969

70-
version: '0.2.0',
70+
version: '0.2.1',
7171

7272
db : db,
7373

@@ -281,30 +281,17 @@ function BoxOfQuestions(db) {
281281

282282
wordsToRepeat : function(){
283283

284-
var lowestStep;
284+
var lowestStep = 0; // all words with step value 0 and above are considered.
285285
var todayNow = new Date().valueOf();
286286

287-
var s = this.db.getSettings();
288287

289-
if (s.offerLearnMode) {
290-
lowestStep = 1
291-
// 1 is lowest step for repeat mode
292-
}
293-
else { // treat all the steps the same way.
294-
// we only have a repeat mode
295-
// thus the lowest step value is 0
296-
297-
lowestStep = 0;
298-
}
299-
300-
301-
function isToBeRepeated(aWord) {
288+
function isToBeRepeated(aWord) {
302289
return (aWord.step >= lowestStep) && (todayNow >= aWord.date);
303290
}
304-
305291

306292

307-
if (_question == null || _wordsToRepeat == null) {
293+
294+
if (_question == null || _wordsToRepeat == null ) {
308295
// _question == null means that either a question has never
309296
// been asked before or that a question has been asked and
310297
// processed but no new question yet has been picked.

src/LWdb.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ var LWdb = function(name) {
4040

4141
var recalculateIndex = true;
4242

43+
var _defaultInitialStepValue = -1;
4344

4445

4546

@@ -203,8 +204,9 @@ var LWdb = function(name) {
203204
if(!aWord._id){
204205
throw "_id is required in a word";
205206
}
206-
if(!aWord.step){
207-
aWord.step = 0;
207+
208+
if(!aWord.hasOwnProperty("step")){
209+
aWord.step = _defaultInitialStepValue;
208210
}
209211
if(!aWord.date){
210212
aWord.date = 0;
@@ -216,6 +218,7 @@ var LWdb = function(name) {
216218
var value = localStorage.getItem(storageKey);
217219

218220
// save the word
221+
219222
localStorage.setItem(storageKey, JSON.stringify(aWord));
220223

221224
// if the word has not existed before increment the number of words
@@ -236,8 +239,8 @@ var LWdb = function(name) {
236239
var storageKey = _wdKeyFor(anInteger);
237240
try{
238241
var aWord = JSON.parse(localStorage.getItem(storageKey));
239-
if(!aWord.step){
240-
aWord.step = 0;
242+
if(!aWord.hasOwnProperty("step")){
243+
aWord.step = _defaultInitialStepValue;
241244
}
242245
if(!aWord.date){
243246
aWord.date = 0;
@@ -328,12 +331,18 @@ var LWdb = function(name) {
328331
value = { "delay": 8640000,
329332
"numberOfOptions": 4,
330333
"factorForDelayValue": [1,1,3,7,45,90,360,1000],
331-
"offerLearnMode": false
334+
"offerLearnMode": false,
335+
"defaultInitialStepValue" : _defaultInitialStepValue
332336
};
333337
// One day = 24h * 60m * 60s * 100μs
334338
// the delay has been shortened to 1 day/100 for test purposes.
335339
// this is used to calculate the new date after a
336340
// word has been answered correctly.
341+
342+
// "defaultInitialStepValue : -1 means that words means
343+
// that words are available to be picked and sent to
344+
// learn/repeat mode.
345+
337346
this.putSettings(value);
338347
return value
339348
} else {

0 commit comments

Comments
 (0)