Skip to content

Commit

Permalink
specs and README upd
Browse files Browse the repository at this point in the history
  • Loading branch information
hhzl committed Nov 30, 2016
1 parent 3c7249e commit e370223
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 52 deletions.
44 changes: 24 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,19 @@ https://github.com/e1r0nd/LearnWords
````
git clone https://github.com/hhzl/LearnWords2.git
cd LearnWords2
git status
npm install
npm install -g grunt-cli
sudo npm install -g grunt-cli
git checkout JasmineInBrowser
npm install
grunt
````

This will be simplified and merged later.
Note that maybe you have to do

sudo npm install -g grunt-cli


## Intended usage

````JavaScript
"use strict";
var LW = function(){

var db = new LWdb('learnWords');
Expand All @@ -41,31 +39,37 @@ var LW = function(){
- LWdb is the data access layer.
- LWBoxOfQuestions contains the application logic for a Leitner box

The construction above is an IIFE.
This is a function without a name which is only run once, just after the definition.
The return value of the function is of interest. It is assigned to the global LW object.

So the access to everything goes through ``LW.subobject`` or ``LW.method()``.

TODO: add examples

## Intended usage

````JavaScript
var LW = function(){

var db = new LWdb('learnWords');
## Specification and tests with Jasmine

db.loadWords(....)
### In the browser

var box = new LWBoxOfQuestions(db);
- on the command line do

return box
}();
````
npm test

- LWdb is the data access layer.
- LWBoxOfQuestions contains the application logic for a Leitner box
- then open ``SpecRunner.html``


### On the command line with node

clear
node scripts/test.js


## Status

* Jasmine specs have started, see branch [JasmineInBrowser](https://github.com/hhzl/LearnWords2/tree/JasmineInBrowser), run ``SpecRunner.html``
* Work towards [release 0.1](https://github.com/hhzl/LearnWords2/milestone/1) in progress. The release will be about the LWdb specification set up and a MVP set of specs implemented.
* The project is not ready for use.
* Jasmine specs are set up.
* Work towards [release 0.1](https://github.com/hhzl/LearnWords2/milestone/1) in progress.
* The LWdb specification set is mostly set up and a MVP set of specs implemented.
* The project will soon be ready for use.

43 changes: 36 additions & 7 deletions spec/learnwords2/LWBoxOfQuestionsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ describe("BoxOfQuestions", function() {
LW = function(){

var db = new LWdb('learnWords');
// FIXME
// db.loadWords(....)

db.loadWords(wordlist);

var box = new BoxOfQuestions(db);

Expand All @@ -31,14 +31,15 @@ describe("BoxOfQuestions", function() {

expect(LW).toBeObject();

expect(LW.db.name).toBeString("learnWords");
expect(LW.db.numberOfWords()).toBeNumber(0);
expect(LW.db.dbName).toBeString("learnWords");
expect(LW.db.numberOfWords()).toBeNumber(10);

});



it("should be able to load questions", function() {

xit("should be able to load additional questions", function() {

var previousNumberOfWords = LW.db.numberOfWords();

Expand All @@ -50,10 +51,21 @@ describe("BoxOfQuestions", function() {
});


it("should be able to have the number of steps set", function() {

expect(LW.noOfSteps).toBe(3); // the default

LW.setNumberOfSteps(7);

expect(LW.noOfSteps).toBe(7);

});



xit("should be able to process configuration information", function() {
xit("should be able to process configuration information", function(aConfigObj) {
// the configuration
var aConfigObj = {"algorithm": "Leiter", "boxes": 5};
// aConfigObj = {"algorithm": "Leitner", "noOfSteps": 5};
LW.config(aConfigObj);

// expect code here
Expand All @@ -62,6 +74,23 @@ describe("BoxOfQuestions", function() {



xit("should be able to choose a next question", function() {
var q ;

q = LW.currQuestion();

expect(q).not.toBe(null);
expect(q).not.toBe(undefined);
expect(q).toBeObject();

expect(q).toHaveString("date");

// add expect code here
// date should be >= today

});



xit("should be able to choose a next question, give options for answers and process the answer", function() {
var q, a, opt, aChoice;
Expand Down
19 changes: 13 additions & 6 deletions spec/learnwords2/LWdbSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ describe("Database LWdb", function() {

expect(this.db).toBeObject();

expect(this.db).toHaveString("name");
expect(this.db.name).toBeString("LearnWords");
expect(this.db).toHaveString("dbName");
expect(this.db.dbName).toBeString("LearnWords");

expect(this.db).toHaveArray("_keysOfAllWords");



});

Expand Down Expand Up @@ -196,12 +200,15 @@ describe("Database LWdb", function() {
});


xit("should be able to maintain an index", function() {
it("should be able to maintain an index", function() {
this.db.removeWords();
expect(this.db.numberOfWords).toBe(0);
expect(this.db.numberOfWords()).toBe(0);

this.db.importFrom(wordlist);
expect(this.db.numberOfWords).toBe(10);
this.db.importFrom(this.wordList);
expect(this.db.numberOfWords()).toBe(10);

expect(this.db.keysOfAllWords()).toBeArray();

});


Expand Down
21 changes: 20 additions & 1 deletion src/BoxOfQuestions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var LWdb = require('./LWdb');
function BoxOfQuestions(db) {
this.name = db.name;
this.db = db;
this.noOfSteps = 3;
}


Expand All @@ -13,7 +14,25 @@ BoxOfQuestions.prototype.config = function(config){
};


BoxOfQuestions.prototype.config = function(config){
BoxOfQuestions.prototype.setNumberOfSteps = function(aNumber){

// the following is fine if the number of steps is increased.
this.noOfSteps = aNumber;

// decide what happens when the number of steps is decreased.

// FIXME
// is this necessary here?
// the step information actually is only needed for calculating the new delay
// and that information is accessible through the index.
// trigger more action?.
// this.recalculateIndexes();

};



BoxOfQuestions.prototype.recalculateIndexes = function(config){
throw new Error("not yet implemented");
};

Expand Down
47 changes: 29 additions & 18 deletions src/LWdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ var LocalStorage = require('node-localstorage').LocalStorage;
var localStorage = new LocalStorage('./scratch');


function LWdb(name) {
this.name = name;
function LWdb(dbName) {
this.dbName = dbName;
this._keysOfAllWords = [];
this.recalculateIndex = true;
}


Expand Down Expand Up @@ -35,7 +37,7 @@ LWdb.prototype.isOK = function() {

LWdb.prototype.numberOfWords = function() {

var key = this.name+'-numberOfWords';
var key = this.dbName+'-numberOfWords';
var r = 0;

if (this.isOK) {
Expand All @@ -54,7 +56,7 @@ LWdb.prototype.numberOfWords = function() {


LWdb.prototype.setNumberOfWords = function(n) {
var key = this.name+'-numberOfWords';
var key = this.dbName+'-numberOfWords';
localStorage.setItem(key,n);
};

Expand All @@ -73,7 +75,7 @@ LWdb.prototype.incNumberOfWords = function() {


LWdb.prototype.wdKeyFor = function(anInteger) {
return this.name+'-wd-'+anInteger;
return this.dbName+'-wd-'+anInteger;
};


Expand Down Expand Up @@ -160,6 +162,7 @@ LWdb.prototype.importFrom = function(theWords) {
key = this.put(aWord);
}

this.invalidateIndex();

}

Expand All @@ -169,20 +172,28 @@ LWdb.prototype.loadWords = function(theWords) {



LWdb.prototype.invalidateIndex = function() {
this.recalculateIndex = true;
}




LWdb.prototype.keysOfAllWords = function() {
var keys = [];
var keyRegex = new RegExp("^"+this.name+"\\-wd\\-\\d+$");
for (var i = 0; i < localStorage.length; i++){
var key = localStorage.key(i);
// check it starts with <name>-wd-
if(keyRegex.test(key)){
keys.push(key);
}
}
return keys;
if (this.recalculateIndex) {
// calculate index
this._keysOfAllWords = [];
var keyRegex = new RegExp("^"+this.dbName+"\\-wd\\-\\d+$");
for (var i = 0; i < localStorage.length; i++){
var key = localStorage.key(i);
// check it starts with <name>-wd-
if(keyRegex.test(key)){
this._keysOfAllWords.push(key);
}
}
};
this.recalculateIndex = false;
return this._keysOfAllWords;
};


Expand All @@ -205,7 +216,7 @@ LWdb.prototype.allWords = function() {

LWdb.prototype.getSettings = function() {

var key = this.name + '-settings';
var key = this.dbName + '-settings';

var value = localStorage.getItem(key);

Expand All @@ -223,7 +234,7 @@ LWdb.prototype.getSettings = function() {

LWdb.prototype.storeSettings = function(anObject) {

var key = this.name + '-settings';
var key = this.dbName + '-settings';
return localStorage.setItem(key,JSON.stringify(anObject));
};

Expand All @@ -245,7 +256,7 @@ LWdb.prototype.removeWords = function() {

LWdb.prototype.destroy = function(anObject) {

var aKeyPrefix = this.name;
var aKeyPrefix = this.dbName;
this.removeObjects(aKeyPrefix);
};

Expand Down

0 comments on commit e370223

Please sign in to comment.