Prefix private variables with an underscore. Fixes #72

This commit is contained in:
mossroy 2014-03-21 13:46:57 +01:00
parent 2d4e949367
commit 9e9d9ea45b
5 changed files with 1639 additions and 1640 deletions

View File

@ -1,407 +1,407 @@
/** /**
* tests.js : Unit tests implemented with qunit * tests.js : Unit tests implemented with qunit
* *
* Copyright 2013-2014 Mossroy and contributors * Copyright 2013-2014 Mossroy and contributors
* License GPL v3: * License GPL v3:
* *
* This file is part of Evopedia. * This file is part of Evopedia.
* *
* Evopedia is free software: you can redistribute it and/or modify * Evopedia is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* Evopedia is distributed in the hope that it will be useful, * Evopedia is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with Evopedia (file LICENSE-GPLv3.txt). If not, see <http://www.gnu.org/licenses/> * along with Evopedia (file LICENSE-GPLv3.txt). If not, see <http://www.gnu.org/licenses/>
*/ */
define(function(require) { define(function(require) {
var $ = require('jquery'); var $ = require('jquery');
var evopediaTitle = require('title'); var evopediaTitle = require('title');
var evopediaArchive = require('archive'); var evopediaArchive = require('archive');
var geometry = require('geometry'); var geometry = require('geometry');
var util = require('util'); var util = require('util');
// Due to security restrictions in the browsers, // Due to security restrictions in the browsers,
// we can not read directly the files and run the unit tests // we can not read directly the files and run the unit tests
// The user has to select them manually, then launch the tests // The user has to select them manually, then launch the tests
$('#runTests').on('click', function(e) { $('#runTests').on('click', function(e) {
runTests(); runTests();
}); });
var runTests = function() { var runTests = function() {
module("environment"); module("environment");
test("qunit test", function() { test("qunit test", function() {
equal("test", "test", "QUnit is properly configured"); equal("test", "test", "QUnit is properly configured");
}); });
test("check archive files are selected", function() { test("check archive files are selected", function() {
var archiveFiles = document.getElementById('archiveFiles').files; var archiveFiles = document.getElementById('archiveFiles').files;
ok(archiveFiles && archiveFiles[0] && archiveFiles[0].size > 0, "First archive file set and not empty"); ok(archiveFiles && archiveFiles[0] && archiveFiles[0].size > 0, "First archive file set and not empty");
ok(archiveFiles.length >= 5, "At least 5 files are selected"); ok(archiveFiles.length >= 5, "At least 5 files are selected");
}); });
// Create a localArchive from selected files, in order to run the following tests // Create a localArchive from selected files, in order to run the following tests
var localArchive = new evopediaArchive.LocalArchive(); var localArchive = new evopediaArchive.LocalArchive();
localArchive.initializeFromArchiveFiles(document.getElementById('archiveFiles').files); localArchive.initializeFromArchiveFiles(document.getElementById('archiveFiles').files);
module("evopedia_title_search_and_read"); module("evopedia_title_search_and_read");
asyncTest("check getTitlesStartingAtOffset 0", function() { asyncTest("check getTitlesStartingAtOffset 0", function() {
expect(4); expect(4);
var callbackFunction = function(titleList) { var callbackFunction = function(titleList) {
equal(titleList.length, 4, "4 titles found, as requested"); equal(titleList.length, 4, "4 titles found, as requested");
var indexAbraham = -1; var indexAbraham = -1;
for (var i = 0; i < titleList.length; i++) { for (var i = 0; i < titleList.length; i++) {
if (titleList[i] && titleList[i].name === "Abraham") { if (titleList[i] && titleList[i]._name === "Abraham") {
indexAbraham = i; indexAbraham = i;
} }
} }
ok(indexAbraham > -1, "Title 'Abraham' found"); ok(indexAbraham > -1, "Title 'Abraham' found");
var firstTitleName = "not found"; var firstTitleName = "not found";
var secondTitleName = "not found"; var secondTitleName = "not found";
if (titleList.length >= 1 && titleList[0]) { if (titleList.length >= 1 && titleList[0]) {
firstTitleName = titleList[0].name; firstTitleName = titleList[0]._name;
} }
if (titleList.length >= 2 && titleList[1]) { if (titleList.length >= 2 && titleList[1]) {
secondTitleName = titleList[1].name; secondTitleName = titleList[1]._name;
} }
equal(firstTitleName, "Abbasid_Caliphate", "First article name is 'Abbasid_Caliphate'"); equal(firstTitleName, "Abbasid_Caliphate", "First article name is 'Abbasid_Caliphate'");
equal(secondTitleName, "Abortion", "Second article name is 'Abortion'"); equal(secondTitleName, "Abortion", "Second article name is 'Abortion'");
start(); start();
}; };
localArchive.getTitlesStartingAtOffset(0, 4, callbackFunction); localArchive.getTitlesStartingAtOffset(0, 4, callbackFunction);
}); });
asyncTest("check findTitlesWithPrefix Am", function() { asyncTest("check findTitlesWithPrefix Am", function() {
expect(4); expect(4);
var callbackFunction = function(titleList) { var callbackFunction = function(titleList) {
ok(titleList && titleList.length > 0, "At least one title is found"); ok(titleList && titleList.length > 0, "At least one title is found");
var firstTitleName = "not found"; var firstTitleName = "not found";
var secondTitleName = "not found"; var secondTitleName = "not found";
if (titleList.length >= 1 && titleList[0]) { if (titleList.length >= 1 && titleList[0]) {
firstTitleName = titleList[0].name; firstTitleName = titleList[0]._name;
} }
if (titleList.length >= 2 && titleList[1]) { if (titleList.length >= 2 && titleList[1]) {
secondTitleName = titleList[1].name; secondTitleName = titleList[1]._name;
} }
equal(firstTitleName, "Amazon_River", "First article name is 'Amazon_River'"); equal(firstTitleName, "Amazon_River", "First article name is 'Amazon_River'");
equal(secondTitleName, "American_Civil_War", "Second article name is 'American_Civil_War'"); equal(secondTitleName, "American_Civil_War", "Second article name is 'American_Civil_War'");
equal(titleList.length, 4, "4 titles should be found"); equal(titleList.length, 4, "4 titles should be found");
start(); start();
}; };
localArchive.findTitlesWithPrefix("Am", 10, callbackFunction); localArchive.findTitlesWithPrefix("Am", 10, callbackFunction);
}); });
// Create a title instance for the Article 'Abraham' // Create a title instance for the Article 'Abraham'
var titleAbraham = new evopediaTitle.Title(); var titleAbraham = new evopediaTitle.Title();
titleAbraham.archive = localArchive; titleAbraham._archive = localArchive;
titleAbraham.articleLength = 10071; titleAbraham._articleLength = 10071;
titleAbraham.blockOffset = 127640; titleAbraham._blockOffset = 127640;
titleAbraham.blockStart = 2364940; titleAbraham._blockStart = 2364940;
titleAbraham.fileNr = 0; titleAbraham._fileNr = 0;
titleAbraham.name = "Abraham"; titleAbraham._name = "Abraham";
titleAbraham.titleOffset = 57; titleAbraham._titleOffset = 57;
asyncTest("check getTitleByName with accents : Diego Velázquez", function() { asyncTest("check getTitleByName with accents : Diego Velázquez", function() {
expect(2); expect(2);
var callbackFunction = function(title) { var callbackFunction = function(title) {
ok(title !== null, "Title found"); ok(title !== null, "Title found");
equal(title.name, "Diego_Velázquez", "Name of the title is correct"); equal(title._name, "Diego_Velázquez", "Name of the title is correct");
start(); start();
}; };
localArchive.getTitleByName("Diego_Velázquez", callbackFunction); localArchive.getTitleByName("Diego_Velázquez", callbackFunction);
}); });
asyncTest("check getTitleByName with quote : Hundred Years' War", function() { asyncTest("check getTitleByName with quote : Hundred Years' War", function() {
expect(2); expect(2);
var callbackFunction = function(title) { var callbackFunction = function(title) {
ok(title !== null, "Title found"); ok(title !== null, "Title found");
equal(title.name, "Hundred_Years'_War", "Name of the title is correct"); equal(title._name, "Hundred_Years'_War", "Name of the title is correct");
start(); start();
}; };
localArchive.getTitleByName("Hundred_Years'_War", callbackFunction); localArchive.getTitleByName("Hundred_Years'_War", callbackFunction);
}); });
test("check parseTitleFromId", function() { test("check parseTitleFromId", function() {
var titleId = "small|2010-08-14|0|57|Abraham|2364940|127640|10071"; var titleId = "small|2010-08-14|0|57|Abraham|2364940|127640|10071";
var title = evopediaTitle.Title.parseTitleId(localArchive, titleId); var title = evopediaTitle.Title.parseTitleId(localArchive, titleId);
ok(title, "Title instance created"); ok(title, "Title instance created");
deepEqual(title, titleAbraham, "Parsing from titleId gives Abraham title"); deepEqual(title, titleAbraham, "Parsing from titleId gives Abraham title");
}); });
asyncTest("check readArticle", function() { asyncTest("check readArticle", function() {
expect(3); expect(3);
var callbackFunction = function(title, htmlArticle) { var callbackFunction = function(title, htmlArticle) {
ok(htmlArticle && htmlArticle.length > 0, "Article not empty"); ok(htmlArticle && htmlArticle.length > 0, "Article not empty");
// Remove new lines // Remove new lines
htmlArticle = htmlArticle.replace(/[\r\n]/g, " "); htmlArticle = htmlArticle.replace(/[\r\n]/g, " ");
ok(htmlArticle.match("^[ \t]*<h1[^>]*>Abraham</h1>"), "'Abraham' title at the beginning"); ok(htmlArticle.match("^[ \t]*<h1[^>]*>Abraham</h1>"), "'Abraham' title at the beginning");
ok(htmlArticle.match("</div>[ \t]$"), "</div> at the end"); ok(htmlArticle.match("</div>[ \t]$"), "</div> at the end");
start(); start();
}; };
localArchive.readArticle(titleAbraham, callbackFunction); localArchive.readArticle(titleAbraham, callbackFunction);
}); });
asyncTest("check getTitleByName and readArticle with escape bytes", function() { asyncTest("check getTitleByName and readArticle with escape bytes", function() {
expect(5); expect(5);
var callbackArticleRead = function(title, htmlArticle) { var callbackArticleRead = function(title, htmlArticle) {
ok(htmlArticle && htmlArticle.length > 0, "Article not empty"); ok(htmlArticle && htmlArticle.length > 0, "Article not empty");
// Remove new lines // Remove new lines
htmlArticle = htmlArticle.replace(/[\r\n]/g, " "); htmlArticle = htmlArticle.replace(/[\r\n]/g, " ");
ok(htmlArticle.match("^[ \t]*<h1[^>]*>AIDS</h1>"), "'AIDS' title at the beginning"); ok(htmlArticle.match("^[ \t]*<h1[^>]*>AIDS</h1>"), "'AIDS' title at the beginning");
ok(htmlArticle.match("</div>[ \t]$"), "</div> at the end"); ok(htmlArticle.match("</div>[ \t]$"), "</div> at the end");
start(); start();
}; };
var callbackTitleFound = function(title) { var callbackTitleFound = function(title) {
ok(title !== null, "Title found"); ok(title !== null, "Title found");
equal(title.name, "AIDS", "Name of the title is correct"); equal(title._name, "AIDS", "Name of the title is correct");
localArchive.readArticle(title, callbackArticleRead); localArchive.readArticle(title, callbackArticleRead);
}; };
localArchive.getTitleByName("AIDS", callbackTitleFound); localArchive.getTitleByName("AIDS", callbackTitleFound);
}); });
asyncTest("check getTitleByName with a title name that does not exist in the archive", function() { asyncTest("check getTitleByName with a title name that does not exist in the archive", function() {
expect(1); expect(1);
var callbackTitleFound = function(title) { var callbackTitleFound = function(title) {
ok(title === null, "No title found because it does not exist in the archive"); ok(title === null, "No title found because it does not exist in the archive");
start(); start();
}; };
localArchive.getTitleByName("abcdef", callbackTitleFound); localArchive.getTitleByName("abcdef", callbackTitleFound);
}); });
asyncTest("check loading a math image", function() { asyncTest("check loading a math image", function() {
expect(2); expect(2);
var callbackFunction = function(data) { var callbackFunction = function(data) {
ok(data && data.length > 0, "Image not empty"); ok(data && data.length > 0, "Image not empty");
// edb3069b82c68d270f6642c171cc6293.png should give a "1 1/2" formula (can be found in "Rational_number" article) // edb3069b82c68d270f6642c171cc6293.png should give a "1 1/2" formula (can be found in "Rational_number" article)
equal(data, equal(data,
"iVBORw0KGgoAAAANSUhEUgAAABUAAAApBAMAAAAogX9zAAAAMFBMVEX///8AAADm5uZAQEDMzMwWFhYiIiIwMDBQUFCenp62trZiYmIMDAwEBASKiop0dHRvDVFEAAAAb0lEQVQY02NggAAmAwY4cE2AM9VNEWwG9oFhcxgKN9HJhYyCQCBApgs5jYMVYCKrGdgOwNgGDCzSMLYwA4MYjH2cgeEawjgWCQSbQwjBdpyAYMch2f4Awd7HwAVj8n1g4Iaxl+7e3Q1jXxQUlGMAAJkfGS29Qu04AAAAAElFTkSuQmCC", "iVBORw0KGgoAAAANSUhEUgAAABUAAAApBAMAAAAogX9zAAAAMFBMVEX///8AAADm5uZAQEDMzMwWFhYiIiIwMDBQUFCenp62trZiYmIMDAwEBASKiop0dHRvDVFEAAAAb0lEQVQY02NggAAmAwY4cE2AM9VNEWwG9oFhcxgKN9HJhYyCQCBApgs5jYMVYCKrGdgOwNgGDCzSMLYwA4MYjH2cgeEawjgWCQSbQwjBdpyAYMch2f4Awd7HwAVj8n1g4Iaxl+7e3Q1jXxQUlGMAAJkfGS29Qu04AAAAAElFTkSuQmCC",
"Math image corresponds to '1 1/2' png"); "Math image corresponds to '1 1/2' png");
start(); start();
}; };
localArchive.loadMathImage("edb3069b82c68d270f6642c171cc6293", callbackFunction); localArchive.loadMathImage("edb3069b82c68d270f6642c171cc6293", callbackFunction);
}); });
module("geometry"); module("geometry");
test("check rectangle intersection", function() { test("check rectangle intersection", function() {
var rect1 = new geometry.rect(0,0,2,2); var rect1 = new geometry.rect(0,0,2,2);
var rect2 = new geometry.rect(1,1,2,2); var rect2 = new geometry.rect(1,1,2,2);
var rect3 = new geometry.rect(2,2,2,2); var rect3 = new geometry.rect(2,2,2,2);
var rect4 = new geometry.rect(1,1,1,1); var rect4 = new geometry.rect(1,1,1,1);
var rect5 = new geometry.rect(3,3,2,2); var rect5 = new geometry.rect(3,3,2,2);
var rect6 = new geometry.rect(2,0,1,10); var rect6 = new geometry.rect(2,0,1,10);
ok(rect1.intersect(rect2), "rect1 intersects rect2"); ok(rect1.intersect(rect2), "rect1 intersects rect2");
ok(rect2.intersect(rect1), "rect2 intersects rect1"); ok(rect2.intersect(rect1), "rect2 intersects rect1");
ok(rect2.intersect(rect3), "rect1 intersects rect3"); ok(rect2.intersect(rect3), "rect1 intersects rect3");
ok(!rect1.intersect(rect3), "rect1 does not intersect rect3"); ok(!rect1.intersect(rect3), "rect1 does not intersect rect3");
ok(!rect4.intersect(rect3), "rect4 does not intersect rect3"); ok(!rect4.intersect(rect3), "rect4 does not intersect rect3");
ok(rect4.intersect(rect2), "rect4 intersects rect2"); ok(rect4.intersect(rect2), "rect4 intersects rect2");
ok(!rect5.intersect(rect1), "rect5 does not intersect rect1"); ok(!rect5.intersect(rect1), "rect5 does not intersect rect1");
ok(!rect1.intersect(rect5), "rect1 does not intersect rect5"); ok(!rect1.intersect(rect5), "rect1 does not intersect rect5");
ok(rect6.intersect(rect2), "rect6 intersects rect2"); ok(rect6.intersect(rect2), "rect6 intersects rect2");
ok(rect6.intersect(rect3), "rect6 intersects rect3"); ok(rect6.intersect(rect3), "rect6 intersects rect3");
ok(!rect6.intersect(rect5), "rect6 intersects rect5"); ok(!rect6.intersect(rect5), "rect6 intersects rect5");
var rect7 = new geometry.rect(0,0,45.5,90,5); var rect7 = new geometry.rect(0,0,45.5,90,5);
var rect8 = new geometry.rect(0,40,10,10); var rect8 = new geometry.rect(0,40,10,10);
ok(rect8.intersect(rect7), "rect8 intersects rect7"); ok(rect8.intersect(rect7), "rect8 intersects rect7");
}); });
test("check rectangle contains a point", function() { test("check rectangle contains a point", function() {
var rect1 = new geometry.rect(2,3,4,5); var rect1 = new geometry.rect(2,3,4,5);
var point1 = new geometry.point(1,1); var point1 = new geometry.point(1,1);
var point2 = new geometry.point(2,3); var point2 = new geometry.point(2,3);
var point3 = new geometry.point(4,4); var point3 = new geometry.point(4,4);
var point4 = new geometry.point(7,9); var point4 = new geometry.point(7,9);
var point5 = new geometry.point(4,6); var point5 = new geometry.point(4,6);
ok(!rect1.containsPoint(point1), "rect1 does not contain point1"); ok(!rect1.containsPoint(point1), "rect1 does not contain point1");
ok(!rect1.containsPoint(point2), "rect1 does not contain point2"); ok(!rect1.containsPoint(point2), "rect1 does not contain point2");
ok(rect1.containsPoint(point3), "rect1 contains point3"); ok(rect1.containsPoint(point3), "rect1 contains point3");
ok(!rect1.containsPoint(point4), "rect1 does not contain point4"); ok(!rect1.containsPoint(point4), "rect1 does not contain point4");
ok(rect1.containsPoint(point5), "rect1 contains point5"); ok(rect1.containsPoint(point5), "rect1 contains point5");
}); });
test("check normalization of a rectangle", function() { test("check normalization of a rectangle", function() {
var rect1 = new geometry.rect(2,3,4,5); var rect1 = new geometry.rect(2,3,4,5);
var normalizedRect1 = rect1.normalized(); var normalizedRect1 = rect1.normalized();
ok(rect1.x===normalizedRect1.x ok(rect1.x===normalizedRect1.x
&& rect1.y===normalizedRect1.y && rect1.y===normalizedRect1.y
&& rect1.width===normalizedRect1.width && rect1.width===normalizedRect1.width
&& rect1.height===normalizedRect1.height, "rect1 is the same after normalization"); && rect1.height===normalizedRect1.height, "rect1 is the same after normalization");
var rect2 = new geometry.rect(6,3,-4,5); var rect2 = new geometry.rect(6,3,-4,5);
var normalizedRect2 = rect2.normalized(); var normalizedRect2 = rect2.normalized();
//alert("normalizedRect2 = " + normalizedRect2); //alert("normalizedRect2 = " + normalizedRect2);
ok(normalizedRect2.x===2 ok(normalizedRect2.x===2
&& normalizedRect2.y===3 && normalizedRect2.y===3
&& normalizedRect2.width===4 && normalizedRect2.width===4
&& normalizedRect2.height===5, "rect2 successfully normalized by switching top left and top right corners"); && normalizedRect2.height===5, "rect2 successfully normalized by switching top left and top right corners");
var rect3 = new geometry.rect(2,8,4,-5); var rect3 = new geometry.rect(2,8,4,-5);
var normalizedRect3 = rect3.normalized(); var normalizedRect3 = rect3.normalized();
ok(normalizedRect3.x===2 ok(normalizedRect3.x===2
&& normalizedRect3.y===3 && normalizedRect3.y===3
&& normalizedRect3.width===4 && normalizedRect3.width===4
&& normalizedRect3.height===5, "rect3 successfully normalized by switching top left and botton left corners"); && normalizedRect3.height===5, "rect3 successfully normalized by switching top left and botton left corners");
var rect4 = new geometry.rect(6,8,-4,-5); var rect4 = new geometry.rect(6,8,-4,-5);
var normalizedRect4 = rect4.normalized(); var normalizedRect4 = rect4.normalized();
ok(normalizedRect4.x===2 ok(normalizedRect4.x===2
&& normalizedRect4.y===3 && normalizedRect4.y===3
&& normalizedRect4.width===4 && normalizedRect4.width===4
&& normalizedRect4.height===5, "rect4 successfully normalized by switching bottom right and top left corners"); && normalizedRect4.height===5, "rect4 successfully normalized by switching bottom right and top left corners");
var rect5 = new geometry.rect(12,2,-4,-1); var rect5 = new geometry.rect(12,2,-4,-1);
var normalizedRect5 = rect5.normalized(); var normalizedRect5 = rect5.normalized();
ok(normalizedRect5.x===8 ok(normalizedRect5.x===8
&& normalizedRect5.y===1 && normalizedRect5.y===1
&& normalizedRect5.width===4 && normalizedRect5.width===4
&& normalizedRect5.height===1, "rect5 successfully normalized by switching bottom right and top left corners"); && normalizedRect5.height===1, "rect5 successfully normalized by switching bottom right and top left corners");
}); });
test("check rectangle constructor from top-left and bottom-right points", function() { test("check rectangle constructor from top-left and bottom-right points", function() {
var topLeft = new geometry.point(2,5); var topLeft = new geometry.point(2,5);
var bottomRight = new geometry.point(5,3); var bottomRight = new geometry.point(5,3);
var rect = new geometry.rect(topLeft, bottomRight); var rect = new geometry.rect(topLeft, bottomRight);
equal(rect.x, 2 , "rect.x should be 2"); equal(rect.x, 2 , "rect.x should be 2");
equal(rect.y, 3 , "rect.y should be 3"); equal(rect.y, 3 , "rect.y should be 3");
equal(rect.width, 3 , "rect.width should be 3"); equal(rect.width, 3 , "rect.width should be 3");
equal(rect.height, 2 , "rect.height should be 2"); equal(rect.height, 2 , "rect.height should be 2");
}); });
test("check rectangle contains another rectangle", function() { test("check rectangle contains another rectangle", function() {
var rect1 = new geometry.rect(2,3,4,4); var rect1 = new geometry.rect(2,3,4,4);
var rect2 = new geometry.rect(3,4,1,1); var rect2 = new geometry.rect(3,4,1,1);
var rect3 = new geometry.rect(1,1,1,1); var rect3 = new geometry.rect(1,1,1,1);
var rect4 = new geometry.rect(3,1,2,4); var rect4 = new geometry.rect(3,1,2,4);
var rect5 = new geometry.rect(3,1,6,4); var rect5 = new geometry.rect(3,1,6,4);
var rect6 = new geometry.rect(2,3,3,2); var rect6 = new geometry.rect(2,3,3,2);
var rect7 = new geometry.rect(5,6,-3,-2); // same as rect7 but not normalized var rect7 = new geometry.rect(5,6,-3,-2); // same as rect7 but not normalized
ok(rect1.contains(rect2), "rect1 should contain rect2"); ok(rect1.contains(rect2), "rect1 should contain rect2");
ok(!rect2.contains(rect1), "rect2 should not contain rect1"); ok(!rect2.contains(rect1), "rect2 should not contain rect1");
ok(!rect1.contains(rect3), "rect1 should not contain rect3"); ok(!rect1.contains(rect3), "rect1 should not contain rect3");
ok(!rect1.contains(rect4), "rect1 should not contain rect4"); ok(!rect1.contains(rect4), "rect1 should not contain rect4");
ok(!rect1.contains(rect5), "rect1 should not contain rect5"); ok(!rect1.contains(rect5), "rect1 should not contain rect5");
ok(rect1.contains(rect1), "rect1 should contain rect1"); ok(rect1.contains(rect1), "rect1 should contain rect1");
ok(rect1.contains(rect6), "rect1 should contain rect6"); ok(rect1.contains(rect6), "rect1 should contain rect6");
ok(rect1.contains(rect7), "rect1 should contain rect7"); ok(rect1.contains(rect7), "rect1 should contain rect7");
}); });
module("utils"); module("utils");
test("check reading an IEEE_754 float from 4 bytes" ,function() { test("check reading an IEEE_754 float from 4 bytes" ,function() {
var byteArray = new Uint8Array(4); var byteArray = new Uint8Array(4);
// This example is taken from https://fr.wikipedia.org/wiki/IEEE_754#Un_exemple_plus_complexe // This example is taken from https://fr.wikipedia.org/wiki/IEEE_754#Un_exemple_plus_complexe
// 1100 0010 1110 1101 0100 0000 0000 0000 // 1100 0010 1110 1101 0100 0000 0000 0000
byteArray[0] = 194; byteArray[0] = 194;
byteArray[1] = 237; byteArray[1] = 237;
byteArray[2] = 64; byteArray[2] = 64;
byteArray[3] = 0; byteArray[3] = 0;
var float = util.readFloatFrom4Bytes(byteArray, 0); var float = util.readFloatFrom4Bytes(byteArray, 0);
equal(float, -118.625, "the IEEE_754 float should be converted as -118.625"); equal(float, -118.625, "the IEEE_754 float should be converted as -118.625");
}); });
module("evopedia_articles_nearby"); module("evopedia_articles_nearby");
asyncTest("check articles found nearby France and Germany", function() { asyncTest("check articles found nearby France and Germany", function() {
expect(5); expect(5);
var callbackTitlesNearbyFound = function(titleList) { var callbackTitlesNearbyFound = function(titleList) {
ok(titleList !== null, "Some titles should be found"); ok(titleList !== null, "Some titles should be found");
equal(titleList.length, 3, "3 titles should be found"); equal(titleList.length, 3, "3 titles should be found");
var titleDanube = null; var titleDanube = null;
var titleParis = null; var titleParis = null;
var titleAlps = null; var titleAlps = null;
for (var i=0; i<titleList.length; i++) { for (var i=0; i<titleList.length; i++) {
var title = titleList[i]; var title = titleList[i];
if (title.name === "Danube") { if (title._name === "Danube") {
titleDanube = title; titleDanube = title;
} }
else if (title.name === "Paris") { else if (title._name === "Paris") {
titleParis = title; titleParis = title;
} }
else if (title.name === "Alps") { else if (title._name === "Alps") {
titleAlps = title; titleAlps = title;
} }
} }
ok(titleDanube !== null, "The title 'Danube' should be found"); ok(titleDanube !== null, "The title 'Danube' should be found");
ok(titleParis !== null, "The title 'Paris' should be found"); ok(titleParis !== null, "The title 'Paris' should be found");
ok(titleAlps !== null, "The title 'Alps' should be found"); ok(titleAlps !== null, "The title 'Alps' should be found");
start(); start();
}; };
var rectFranceGermany = new geometry.rect(40,0,10,10); var rectFranceGermany = new geometry.rect(40,0,10,10);
localArchive.getTitlesInCoords(rectFranceGermany, 10, callbackTitlesNearbyFound); localArchive.getTitlesInCoords(rectFranceGermany, 10, callbackTitlesNearbyFound);
}); });
asyncTest("check articles found nearby France and Germany, with a maximum", function() { asyncTest("check articles found nearby France and Germany, with a maximum", function() {
expect(2); expect(2);
var callbackTitlesNearbyMaximumFound = function(titleList) { var callbackTitlesNearbyMaximumFound = function(titleList) {
ok(titleList !== null, "Some titles should be found"); ok(titleList !== null, "Some titles should be found");
equal(titleList.length, 2, "2 titles should be found"); equal(titleList.length, 2, "2 titles should be found");
start(); start();
}; };
var rectFranceGermany = new geometry.rect(40,0,10,10); var rectFranceGermany = new geometry.rect(40,0,10,10);
localArchive.getTitlesInCoords(rectFranceGermany, 2, callbackTitlesNearbyMaximumFound); localArchive.getTitlesInCoords(rectFranceGermany, 2, callbackTitlesNearbyMaximumFound);
}); });
asyncTest("check articles found nearby London", function() { asyncTest("check articles found nearby London", function() {
expect(3); expect(3);
var callbackTitlesNearbyLondonFound = function(titleList) { var callbackTitlesNearbyLondonFound = function(titleList) {
ok(titleList !== null, "Some titles should be found"); ok(titleList !== null, "Some titles should be found");
equal(titleList.length, 1, "1 title should be found"); equal(titleList.length, 1, "1 title should be found");
var titleLondon = null; var titleLondon = null;
for (var i=0; i<titleList.length; i++) { for (var i=0; i<titleList.length; i++) {
var title = titleList[i]; var title = titleList[i];
if (title.name === "London") { if (title._name === "London") {
titleLondon = title; titleLondon = title;
} }
} }
ok(titleLondon !== null, "The title 'London' should be found"); ok(titleLondon !== null, "The title 'London' should be found");
start(); start();
}; };
var pointLondon = new geometry.point(51, 0); var pointLondon = new geometry.point(51, 0);
var maxDistance = 1; var maxDistance = 1;
var rectLondon = new geometry.rect( var rectLondon = new geometry.rect(
pointLondon.x - maxDistance, pointLondon.x - maxDistance,
pointLondon.y - maxDistance, pointLondon.y - maxDistance,
maxDistance * 2, maxDistance * 2,
maxDistance * 2); maxDistance * 2);
localArchive.getTitlesInCoords(rectLondon, 10, callbackTitlesNearbyLondonFound); localArchive.getTitlesInCoords(rectLondon, 10, callbackTitlesNearbyLondonFound);
}); });
asyncTest("check articles found nearby Amsterdam", function() { asyncTest("check articles found nearby Amsterdam", function() {
expect(3); expect(3);
var callbackTitlesNearbyAmsterdamFound = function(titleList) { var callbackTitlesNearbyAmsterdamFound = function(titleList) {
ok(titleList !== null, "Some titles should be found"); ok(titleList !== null, "Some titles should be found");
equal(titleList.length, 1, "1 title should be found"); equal(titleList.length, 1, "1 title should be found");
var titleAmsterdam = null; var titleAmsterdam = null;
for (var i=0; i<titleList.length; i++) { for (var i=0; i<titleList.length; i++) {
var title = titleList[i]; var title = titleList[i];
if (title.name === "Amsterdam") { if (title._name === "Amsterdam") {
titleAmsterdam = title; titleAmsterdam = title;
} }
} }
ok(titleAmsterdam !== null, "The title 'Amsterdam' should be found"); ok(titleAmsterdam !== null, "The title 'Amsterdam' should be found");
start(); start();
}; };
var pointAmsterdam = new geometry.point(55, 5); var pointAmsterdam = new geometry.point(55, 5);
var maxDistance = 5; var maxDistance = 5;
var rectAmsterdam = new geometry.rect( var rectAmsterdam = new geometry.rect(
pointAmsterdam.x - maxDistance, pointAmsterdam.x - maxDistance,
pointAmsterdam.y - maxDistance, pointAmsterdam.y - maxDistance,
maxDistance * 2, maxDistance * 2,
maxDistance * 2); maxDistance * 2);
localArchive.getTitlesInCoords(rectAmsterdam, 10, callbackTitlesNearbyAmsterdamFound); localArchive.getTitlesInCoords(rectAmsterdam, 10, callbackTitlesNearbyAmsterdamFound);
}); });
module("evopedia_random_title"); module("evopedia_random_title");
asyncTest("check that a random title is found", function() { asyncTest("check that a random title is found", function() {
expect(2); expect(2);
var callbackRandomTitleFound = function(title) { var callbackRandomTitleFound = function(title) {
ok(title !== null, "One title should be found"); ok(title !== null, "One title should be found");
ok(title.name !== null, "The random title should have a name" ); ok(title._name !== null, "The random title should have a name" );
start(); start();
}; };
localArchive.getRandomTitle(callbackRandomTitleFound); localArchive.getRandomTitle(callbackRandomTitleFound);
}); });
}; };
}); });

View File

@ -62,7 +62,7 @@ define(function(require) {
return false; return false;
}); });
$('#prefix').on('keyup', function(e) { $('#prefix').on('keyup', function(e) {
if (localArchive !== null && localArchive.titleFile !== null) { if (localArchive !== null && localArchive._titleFile !== null) {
onKeyUpPrefix(e); onKeyUpPrefix(e);
$('#geolocationProgress').hide(); $('#geolocationProgress').hide();
} }
@ -322,7 +322,7 @@ define(function(require) {
$('#searchingForTitles').show(); $('#searchingForTitles').show();
$('#configuration').hide(); $('#configuration').hide();
$('#articleContent').empty(); $('#articleContent').empty();
if (localArchive !== null && localArchive.titleFile !== null) { if (localArchive !== null && localArchive._titleFile !== null) {
localArchive.findTitlesWithPrefix(prefix.trim(), MAX_SEARCH_RESULT_SIZE, populateListOfTitles); localArchive.findTitlesWithPrefix(prefix.trim(), MAX_SEARCH_RESULT_SIZE, populateListOfTitles);
} else { } else {
$('#searchingForTitles').hide(); $('#searchingForTitles').hide();
@ -404,7 +404,7 @@ define(function(require) {
$("#titleList").empty(); $("#titleList").empty();
findTitleFromTitleIdAndLaunchArticleRead(titleId); findTitleFromTitleIdAndLaunchArticleRead(titleId);
var title = evopediaTitle.Title.parseTitleId(localArchive, titleId); var title = evopediaTitle.Title.parseTitleId(localArchive, titleId);
pushBrowserHistoryState(title.name); pushBrowserHistoryState(title._name);
$("#prefix").val(""); $("#prefix").val("");
return false; return false;
} }
@ -416,12 +416,12 @@ define(function(require) {
* @param {type} titleId * @param {type} titleId
*/ */
function findTitleFromTitleIdAndLaunchArticleRead(titleId) { function findTitleFromTitleIdAndLaunchArticleRead(titleId) {
if (localArchive.dataFiles && localArchive.dataFiles.length > 0) { if (localArchive._dataFiles && localArchive._dataFiles.length > 0) {
var title = evopediaTitle.Title.parseTitleId(localArchive, titleId); var title = evopediaTitle.Title.parseTitleId(localArchive, titleId);
$("#articleName").html(title.name); $("#articleName").html(title._name);
$("#readingArticle").show(); $("#readingArticle").show();
$("#articleContent").html(""); $("#articleContent").html("");
if (title.fileNr === 255) { if (title._fileNr === 255) {
localArchive.resolveRedirect(title, readArticle); localArchive.resolveRedirect(title, readArticle);
} }
else { else {
@ -438,7 +438,7 @@ define(function(require) {
* @param {type} title * @param {type} title
*/ */
function readArticle(title) { function readArticle(title) {
if (title.fileNr === 255) { if (title._fileNr === 255) {
localArchive.resolveRedirect(title, readArticle); localArchive.resolveRedirect(title, readArticle);
} }
else { else {
@ -571,7 +571,7 @@ define(function(require) {
$('#configuration').hide(); $('#configuration').hide();
$('#titleList').hide(); $('#titleList').hide();
$('#articleContent').empty(); $('#articleContent').empty();
if (localArchive !== null && localArchive.titleFile !== null) { if (localArchive !== null && localArchive._titleFile !== null) {
var longitude = $('#longitude').val(); var longitude = $('#longitude').val();
var latitude = $('#latitude').val(); var latitude = $('#latitude').val();
var maxDistance = $('#maxDistance').val(); var maxDistance = $('#maxDistance').val();
@ -659,7 +659,7 @@ define(function(require) {
alert("Error finding random article."); alert("Error finding random article.");
} }
else { else {
$("#articleName").html(title.name); $("#articleName").html(title._name);
$("#readingArticle").show(); $("#readingArticle").show();
$("#articleContent").html(""); $("#articleContent").html("");
readArticle(title); readArticle(title);

File diff suppressed because it is too large Load Diff

View File

@ -1,150 +1,149 @@
/** /**
* title.js : Class for the title of an article * title.js : Class for the title of an article
* *
* Copyright 2013-2014 Mossroy and contributors * Copyright 2013-2014 Mossroy and contributors
* License GPL v3: * License GPL v3:
* *
* This file is part of Evopedia. * This file is part of Evopedia.
* *
* Evopedia is free software: you can redistribute it and/or modify * Evopedia is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* Evopedia is distributed in the hope that it will be useful, * Evopedia is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with Evopedia (file LICENSE-GPLv3.txt). If not, see <http://www.gnu.org/licenses/> * along with Evopedia (file LICENSE-GPLv3.txt). If not, see <http://www.gnu.org/licenses/>
*/ */
define(function(require) { define(function(require) {
// Module dependencies // Module dependencies
var utf8 = require('utf8'); var utf8 = require('utf8');
var util = require('util'); var util = require('util');
/** /**
* Title class : defines the title of an article and some methods to manipulate it * Title class : defines the title of an article and some methods to manipulate it
*/ */
function Title() { function Title() {
this.name = null; this._name = null;
this.fileNr = null; this._fileNr = null;
this.blockStart = null; this._blockStart = null;
this.blockOffset = null; this._blockOffset = null;
this.articleLength = null; this._articleLength = null;
this.archive = null; this._archive = null;
this.titleOffset = null; this._titleOffset = null;
this.titleEntryLength = null; this._titleEntryLength = null;
} };
;
Title.prototype.getReadableName = function() {
Title.prototype.getReadableName = function() { return this._name.replace(/_/g, " ");
return this.name.replace(/_/g, " "); };
};
/**
/** * Creates a Title instance from an encoded title line from a title file
* Creates a Title instance from an encoded title line from a title file * @param {type} encodedTitle
* @param {type} encodedTitle * @param {type} archive
* @param {type} archive * @param {type} titleOffset
* @param {type} titleOffset * @returns {_L1.Title}
* @returns {_L1.Title} */
*/ Title.parseTitle = function(encodedTitle, archive, titleOffset) {
Title.parseTitle = function(encodedTitle, archive, titleOffset) { if (archive === null) {
if (archive === null) { throw new Error("Error while parsing an encoded title line un title File : archive cannot be null");
throw new Error("Error while parsing an encoded title line un title File : archive cannot be null"); }
} if (titleOffset < 0) {
if (titleOffset < 0) { throw new Error("Error while parsing an encoded title line un title File : titleOffset cannot be negative (was " + titleOffset + ")");
throw new Error("Error while parsing an encoded title line un title File : titleOffset cannot be negative (was " + titleOffset + ")"); }
} var t = new Title();
var t = new Title(); t._archive = archive;
t.archive = archive; t._titleOffset = titleOffset;
t.titleOffset = titleOffset;
if (encodedTitle === null || encodedTitle.length < 15)
if (encodedTitle === null || encodedTitle.length < 15) return null;
return null;
if (encodedTitle[encodedTitle.length - 1] == '\n') {
if (encodedTitle[encodedTitle.length - 1] == '\n') { t._titleEntryLength = encodedTitle.length;
t.titleEntryLength = encodedTitle.length; } else {
} else { t._titleEntryLength = encodedTitle.length + 1;
t.titleEntryLength = encodedTitle.length + 1; }
}
var escapedEncodedTitle = new Uint8Array(encodedTitle);
var escapedEncodedTitle = new Uint8Array(encodedTitle); var escapes = util.readIntegerFrom2Bytes(encodedTitle, 0);
var escapes = util.readIntegerFrom2Bytes(encodedTitle, 0); if ((escapes & (1 << 14)) != 0)
if ((escapes & (1 << 14)) != 0) escapes |= 10;
escapes |= 10; for (var i = 0; i < 13; i++) {
for (var i = 0; i < 13; i++) { if ((escapes & (1 << i)) != 0)
if ((escapes & (1 << i)) != 0) escapedEncodedTitle[i + 2] = 10; // Corresponds to \n
escapedEncodedTitle[i + 2] = 10; // Corresponds to \n }
}
t._fileNr = 1 * escapedEncodedTitle[2];
t.fileNr = 1 * escapedEncodedTitle[2]; t._blockStart = util.readIntegerFrom4Bytes(escapedEncodedTitle, 3);
t.blockStart = util.readIntegerFrom4Bytes(escapedEncodedTitle, 3); t._blockOffset = util.readIntegerFrom4Bytes(escapedEncodedTitle, 7);
t.blockOffset = util.readIntegerFrom4Bytes(escapedEncodedTitle, 7); t._articleLength = util.readIntegerFrom4Bytes(escapedEncodedTitle, 11);
t.articleLength = util.readIntegerFrom4Bytes(escapedEncodedTitle, 11);
t._name = Title.parseNameOnly(escapedEncodedTitle);
t.name = Title.parseNameOnly(escapedEncodedTitle);
return t;
return t; };
};
/*
/* * Retrieves the name of an article from an encoded title line
* Retrieves the name of an article from an encoded title line */
*/ Title.parseNameOnly = function(encodedTitle) {
Title.parseNameOnly = function(encodedTitle) { var len = encodedTitle.length;
var len = encodedTitle.length; if (len < 15) {
if (len < 15) { return null;
return null; }
} if (len > 15 && encodedTitle[len - 1] == '\n') {
if (len > 15 && encodedTitle[len - 1] == '\n') { len--;
len--; }
} return utf8.parse(encodedTitle.subarray(15, len));
return utf8.parse(encodedTitle.subarray(15, len)); };
};
/**
/** * Creates a title instance from a serialized id
* Creates a title instance from a serialized id * @param {type} localArchive
* @param {type} localArchive * @param {type} titleId
* @param {type} titleId * @returns {_L1.Title}
* @returns {_L1.Title} */
*/ Title.parseTitleId = function(localArchive, titleId) {
Title.parseTitleId = function(localArchive, titleId) { var title = new Title();
var title = new Title(); var idParts = titleId.split("|");
var idParts = titleId.split("|"); title._archive = localArchive;
title.archive = localArchive; title._fileNr = parseInt(idParts[2], 10);
title.fileNr = parseInt(idParts[2], 10); title._titleOffset = parseInt(idParts[3], 10);
title.titleOffset = parseInt(idParts[3], 10); title._name = idParts[4];
title.name = idParts[4]; title._blockStart = parseInt(idParts[5], 10);
title.blockStart = parseInt(idParts[5], 10); title._blockOffset = parseInt(idParts[6], 10);
title.blockOffset = parseInt(idParts[6], 10); title._articleLength = parseInt(idParts[7], 10);
title.articleLength = parseInt(idParts[7], 10); return title;
return title; };
};
/**
/** * Serialize the title with its values
* Serialize the title with its values * @returns {String}
* @returns {String} */
*/ Title.prototype.toStringId = function() {
Title.prototype.toStringId = function() { return this._archive.language + "|" + this._archive.date + "|" + this._fileNr + "|"
return this.archive.language + "|" + this.archive.date + "|" + this.fileNr + "|" + this._titleOffset + "|" + this._name + "|" + this._blockStart + "|" + this._blockOffset + "|" + this._articleLength;
+ this.titleOffset + "|" + this.name + "|" + this.blockStart + "|" + this.blockOffset + "|" + this.articleLength; };
};
/**
/** * Serialize the title in a readable way
* Serialize the title in a readable way */
*/ Title.prototype.toString = function() {
Title.prototype.toString = function() { return "title.id = " + this.toStringId() + "title.name = " + this._name + " title.fileNr = " + this._fileNr + " title.blockStart = " + this._blockStart + " title.blockOffset = " + this._blockOffset + " title.articleLength = " + this._articleLength;
return "title.id = " + this.toStringId() + "title.name = " + this.name + " title.fileNr = " + this.fileNr + " title.blockStart = " + this.blockStart + " title.blockOffset = " + this.blockOffset + " title.articleLength = " + this.articleLength; };
};
/**
/** * Functions and classes exposed by this module
* Functions and classes exposed by this module */
*/ return {
return { Title: Title
Title: Title };
}; });
});

View File

@ -1,127 +1,127 @@
/** /**
* titleIterators.js : Various classes to iterate over titles, for example as a * titleIterators.js : Various classes to iterate over titles, for example as a
* result of searching. * result of searching.
* *
* Copyright 2014 Evopedia developers * Copyright 2014 Evopedia developers
* License GPL v3: * License GPL v3:
* *
* This file is part of Evopedia. * This file is part of Evopedia.
* *
* Evopedia is free software: you can redistribute it and/or modify * Evopedia is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* Evopedia is distributed in the hope that it will be useful, * Evopedia is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with Evopedia (file LICENSE-GPLv3.txt). If not, see <http://www.gnu.org/licenses/> * along with Evopedia (file LICENSE-GPLv3.txt). If not, see <http://www.gnu.org/licenses/>
*/ */
define(['utf8', 'title', 'util', 'jquery'], function(utf8, evopediaTitle, util, jQuery) { define(['utf8', 'title', 'util', 'jquery'], function(utf8, evopediaTitle, util, jQuery) {
// Maximum length of a title // Maximum length of a title
// 300 bytes is arbitrary : we actually do not really know how long the titles will be // 300 bytes is arbitrary : we actually do not really know how long the titles will be
// But mediawiki titles seem to be limited to ~200 bytes, so 300 should be more than enough // But mediawiki titles seem to be limited to ~200 bytes, so 300 should be more than enough
var MAX_TITLE_LENGTH = 300; var MAX_TITLE_LENGTH = 300;
/** /**
* Iterates over all titles starting at the given offset. * Iterates over all titles starting at the given offset.
* The asynchronous method advance has to be called before this.title is * The asynchronous method advance has to be called before this.title is
* valid. * valid.
* @param archive * @param archive
* @param offset * @param offset
*/ */
function SequentialTitleIterator(archive, offset) { function SequentialTitleIterator(archive, offset) {
this._titleFile = archive.titleFile; this._titleFile = archive._titleFile;
this._archive = archive; this._archive = archive;
this._offset = offset; this._offset = offset;
this.title = null; this._title = null;
}; };
/** /**
* Advances to the next title (or the first), if possible. * Advances to the next title (or the first), if possible.
* @returns jQuery promise containing the next title or null if there is no * @returns jQuery promise containing the next title or null if there is no
* next title * next title
*/ */
SequentialTitleIterator.prototype.advance = function() { SequentialTitleIterator.prototype.advance = function() {
if (this._offset >= this._titleFile.size) { if (this._offset >= this._titleFile.size) {
this.title = null; this._title = null;
return jQuery.when(this.title); return jQuery.when(this._title);
} }
var that = this; var that = this;
return util.readFileSlice(this._titleFile, this._offset, return util.readFileSlice(this._titleFile, this._offset,
this._offset + MAX_TITLE_LENGTH).then(function(byteArray) { this._offset + MAX_TITLE_LENGTH).then(function(byteArray) {
var newLineIndex = 15; var newLineIndex = 15;
while (newLineIndex < byteArray.length && byteArray[newLineIndex] !== 10) { while (newLineIndex < byteArray.length && byteArray[newLineIndex] !== 10) {
newLineIndex++; newLineIndex++;
} }
var encodedTitle = byteArray.subarray(0, newLineIndex); var encodedTitle = byteArray.subarray(0, newLineIndex);
that._title = evopediaTitle.Title.parseTitle(encodedTitle, that._archive, that._offset); that._title = evopediaTitle.Title.parseTitle(encodedTitle, that._archive, that._offset);
that._offset += newLineIndex + 1; that._offset += newLineIndex + 1;
return that._title; return that._title;
}); });
}; };
/** /**
* Searches for the offset into the given title file where the first title * Searches for the offset into the given title file where the first title
* with the given prefix (or lexicographically larger) is located. * with the given prefix (or lexicographically larger) is located.
* The given function normalize is applied to every title before comparison. * The given function normalize is applied to every title before comparison.
* @param titleFile * @param titleFile
* @param prefix * @param prefix
* @param normalize function to be applied to every title before comparison * @param normalize function to be applied to every title before comparison
* @returns jQuery promise giving the offset * @returns jQuery promise giving the offset
*/ */
function findPrefixOffset(titleFile, prefix, normalize) { function findPrefixOffset(titleFile, prefix, normalize) {
prefix = normalize(prefix); prefix = normalize(prefix);
var lo = 0; var lo = 0;
var hi = titleFile.size; var hi = titleFile.size;
var iterate = function() { var iterate = function() {
if (lo >= hi - 1) { if (lo >= hi - 1) {
if (lo > 0) if (lo > 0)
lo += 2; // Let lo point to the start of an entry lo += 2; // Let lo point to the start of an entry
return jQuery.when(lo); return jQuery.when(lo);
} else { } else {
var mid = Math.floor((lo + hi) / 2); var mid = Math.floor((lo + hi) / 2);
return util.readFileSlice(titleFile, mid, mid + MAX_TITLE_LENGTH).then(function(byteArray) { return util.readFileSlice(titleFile, mid, mid + MAX_TITLE_LENGTH).then(function(byteArray) {
// Look for the index of the next NewLine // Look for the index of the next NewLine
var newLineIndex = 0; var newLineIndex = 0;
while (newLineIndex < byteArray.length && byteArray[newLineIndex] !== 10) { while (newLineIndex < byteArray.length && byteArray[newLineIndex] !== 10) {
newLineIndex++; newLineIndex++;
} }
var startIndex = 0; var startIndex = 0;
if (mid > 0) { if (mid > 0) {
startIndex = newLineIndex + 16; startIndex = newLineIndex + 16;
newLineIndex = startIndex; newLineIndex = startIndex;
// Look for the index of the next NewLine // Look for the index of the next NewLine
while (newLineIndex < byteArray.length && byteArray[newLineIndex] !== 10) { while (newLineIndex < byteArray.length && byteArray[newLineIndex] !== 10) {
newLineIndex++; newLineIndex++;
} }
} }
if (newLineIndex === startIndex) { if (newLineIndex === startIndex) {
// End of file reached // End of file reached
hi = mid; hi = mid;
} else { } else {
var normalizedTitle = normalize(utf8.parse(byteArray.subarray(startIndex, newLineIndex))); var normalizedTitle = normalize(utf8.parse(byteArray.subarray(startIndex, newLineIndex)));
if (normalizedTitle < prefix) { if (normalizedTitle < prefix) {
lo = mid + newLineIndex - 1; lo = mid + newLineIndex - 1;
} else { } else {
hi = mid; hi = mid;
} }
} }
return iterate(); return iterate();
}); });
} }
}; };
return iterate(); return iterate();
} }
/** /**
* Functions and classes exposed by this module * Functions and classes exposed by this module
*/ */
return { return {
SequentialTitleIterator : SequentialTitleIterator, SequentialTitleIterator : SequentialTitleIterator,
findPrefixOffset : findPrefixOffset, findPrefixOffset : findPrefixOffset,
MAX_TITLE_LENGTH : MAX_TITLE_LENGTH MAX_TITLE_LENGTH : MAX_TITLE_LENGTH
}; };
}); });