Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unused removeDuplicateTitlesInDirEntryArray() #491 #544

Merged
merged 1 commit into from Jul 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 24 additions & 29 deletions tests/tests.js
@@ -1,33 +1,33 @@
/**
* tests.js : Unit tests implemented with qunit
*
*
* Copyright 2013-2014 Mossroy and contributors
* License GPL v3:
*
*
* This file is part of Kiwix.
*
*
* Kiwix is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* Kiwix is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
* You should have received a copy of the GNU General Public License
* along with Kiwix (file LICENSE-GPLv3.txt). If not, see <http://www.gnu.org/licenses/>
*/
define(['jquery', 'zimArchive', 'zimDirEntry', 'util', 'uiUtil', 'utf8'],
function($, zimArchive, zimDirEntry, util, uiUtil, utf8) {

var localZimArchive;


/**
* Make an HTTP request for a Blob and return a Promise
*
*
* @param {String} url URL to download from
* @param {String} name Name to give to the Blob instance
* @returns {Promise}
Expand Down Expand Up @@ -63,10 +63,10 @@ define(['jquery', 'zimArchive', 'zimDirEntry', 'util', 'uiUtil', 'utf8'],
xhr.send();
});
}

// Let's try to download the ZIM files
var zimArchiveFiles = new Array();

var splitBlobs = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o'].map(function(c) {
var filename = 'wikipedia_en_ray_charles_2015-06.zima' + c;
return makeBlobRequest('tests/' + filename, filename);
Expand All @@ -80,7 +80,7 @@ define(['jquery', 'zimArchive', 'zimDirEntry', 'util', 'uiUtil', 'utf8'],
runTests();
});
});

var runTests = function() {

QUnit.module("environment");
Expand All @@ -91,7 +91,7 @@ define(['jquery', 'zimArchive', 'zimDirEntry', 'util', 'uiUtil', 'utf8'],
QUnit.test("check archive files are read", function(assert) {
assert.ok(zimArchiveFiles && zimArchiveFiles[0] && zimArchiveFiles[0].size > 0, "ZIM file read and not empty");
});

QUnit.module("utils");
QUnit.test("check reading an IEEE_754 float from 4 bytes" ,function(assert) {
var byteArray = new Uint8Array(4);
Expand All @@ -114,11 +114,6 @@ define(['jquery', 'zimArchive', 'zimDirEntry', 'util', 'uiUtil', 'utf8'],
assert.equal(util.ucEveryFirstLetter(testString3), "Le Couvre-Chef Est Sur Le Porte-Manteaux", "The first letter of every word should be upper-case");
assert.equal(util.ucFirstLetter(testString4), "Épée", "The first letter should be upper-case (with accent)");
});
QUnit.test("check remove duplicates of an array of dirEntry objects", function(assert) {
var array = [{title:"a"}, {title:"b"}, {title:"c"}, {title:"a"}, {title:"c"}, {title:"d"}];
var expectedArray = [{title:"a"}, {title:"b"}, {title:"c"}, {title:"d"}];
assert.deepEqual(util.removeDuplicateTitlesInDirEntryArray(array), expectedArray, "Duplicates should be removed from the array");
});
QUnit.test("check removal of parameters in URL", function(assert) {
var testUrl1 = "A/question.html";
var testUrl2 = "A/question.html?param1=toto&param2=titi";
Expand All @@ -129,15 +124,15 @@ define(['jquery', 'zimArchive', 'zimDirEntry', 'util', 'uiUtil', 'utf8'],
assert.equal(uiUtil.removeUrlParameters(testUrl3), testUrl1);
assert.equal(uiUtil.removeUrlParameters(testUrl4), testUrl1);
});

QUnit.module("ZIM initialisation");
QUnit.test("ZIM archive is ready", function(assert) {
assert.ok(localZimArchive.isReady() === true, "ZIM archive should be set as ready");
});

QUnit.module("ZIM metadata");
QUnit.test("read ZIM language", function(assert) {
var done = assert.async();
var done = assert.async();
assert.expect(1);
var callbackFunction = function(language) {
assert.equal(language , 'eng', 'The language read inside the Metadata should be "eng" for "English"');
Expand All @@ -146,15 +141,15 @@ define(['jquery', 'zimArchive', 'zimDirEntry', 'util', 'uiUtil', 'utf8'],
localZimArchive.getMetadata("Language", callbackFunction);
});
QUnit.test("try to read a missing metadata", function(assert) {
var done = assert.async();
var done = assert.async();
assert.expect(1);
var callbackFunction = function(string) {
assert.equal(string, undefined, 'The metadata zzz should not be found inside the ZIM');
done();
};
localZimArchive.getMetadata("zzz", callbackFunction);
});

QUnit.module("zim_direntry_search_and_read");
QUnit.test("check DirEntry.fromStringId 'A Fool for You'", function(assert) {
var done = assert.async();
Expand All @@ -171,7 +166,7 @@ define(['jquery', 'zimArchive', 'zimDirEntry', 'util', 'uiUtil', 'utf8'],
localZimArchive.readUtf8File(aFoolForYouDirEntry, callbackFunction);
});
QUnit.test("check findDirEntriesWithPrefix 'A'", function(assert) {
var done = assert.async();
var done = assert.async();
assert.expect(2);
var callbackFunction = function(dirEntryList) {
assert.ok(dirEntryList && dirEntryList.length === 5, "Article list with 5 results");
Expand All @@ -182,7 +177,7 @@ define(['jquery', 'zimArchive', 'zimDirEntry', 'util', 'uiUtil', 'utf8'],
localZimArchive.findDirEntriesWithPrefix('A', 5, callbackFunction);
});
QUnit.test("check findDirEntriesWithPrefix 'a'", function(assert) {
var done = assert.async();
var done = assert.async();
assert.expect(2);
var callbackFunction = function(dirEntryList) {
assert.ok(dirEntryList && dirEntryList.length === 5, "Article list with 5 results");
Expand Down Expand Up @@ -301,7 +296,7 @@ define(['jquery', 'zimArchive', 'zimDirEntry', 'util', 'uiUtil', 'utf8'],
});
QUnit.test("Stylesheet '-/s/style.css' can be loaded", function(assert) {
var done = assert.async();

assert.expect(5);
localZimArchive.getDirEntryByTitle("-/s/style.css").then(function(dirEntry) {
assert.ok(dirEntry !== null, "DirEntry found");
Expand Down Expand Up @@ -335,7 +330,7 @@ define(['jquery', 'zimArchive', 'zimDirEntry', 'util', 'uiUtil', 'utf8'],
assert.equal(data.slice(0, beginning.length), beginning, "Content starts correctly.");
done();
});
}
}
else {
done();
}
Expand All @@ -361,15 +356,15 @@ define(['jquery', 'zimArchive', 'zimDirEntry', 'util', 'uiUtil', 'utf8'],
}
});
});

QUnit.module("zim_random_and_main_article");
QUnit.test("check that a random article is found", function(assert) {
var done = assert.async();
assert.expect(2);
var callbackRandomArticleFound = function(dirEntry) {
assert.ok(dirEntry !== null, "One DirEntry should be found");
assert.ok(dirEntry.getTitleOrUrl() !== null, "The random DirEntry should have a title" );

done();
};
localZimArchive.getRandomDirEntry(callbackRandomArticleFound);
Expand All @@ -380,7 +375,7 @@ define(['jquery', 'zimArchive', 'zimDirEntry', 'util', 'uiUtil', 'utf8'],
var callbackMainPageArticleFound = function(dirEntry) {
assert.ok(dirEntry !== null, "Main DirEntry should be found");
assert.equal(dirEntry.getTitleOrUrl(), "Summary", "The main DirEntry should be called Summary" );

done();
};
localZimArchive.getMainPageDirEntry(callbackMainPageArticleFound);
Expand Down
60 changes: 18 additions & 42 deletions www/js/lib/util.js
@@ -1,21 +1,21 @@
/**
* util.js : Utility functions
*
*
* Copyright 2013-2014 Mossroy and contributors
* License GPL v3:
*
*
* This file is part of Kiwix.
*
*
* Kiwix is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* Kiwix is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
* You should have received a copy of the GNU General Public License
* along with Kiwix (file LICENSE-GPLv3.txt). If not, see <http://www.gnu.org/licenses/>
*/
Expand All @@ -31,7 +31,7 @@ define(['q'], function(q) {
function endsWith(str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
}

/**
* Returns the same String with the first letter in upper-case
* @param {String} string
Expand All @@ -44,7 +44,7 @@ define(['q'], function(q) {
return string;
}
}

/**
* Returns the same String with the first letter in lower-case
* @param {String} string
Expand All @@ -61,7 +61,7 @@ define(['q'], function(q) {
return string;
}
}

/**
* Returns the same String with the first letter of every word in upper-case
* @param {String} string
Expand All @@ -76,36 +76,13 @@ define(['q'], function(q) {
return string;
}
}

/**
* Generates an array of DirEntry, where all duplicates (same title) have been removed
* (it also sorts them on their title)
*
* @param {Array.<DirEntry>} array of DirEntry
* @returns {Array.<DirEntry>} same array of DirEntry, without duplicates
*/
function removeDuplicateTitlesInDirEntryArray(array) {
array.sort(function(dirEntryA, dirEntryB) {
if (dirEntryA.title < dirEntryB.title) return -1;
if (dirEntryA.title > dirEntryB.title) return 1;
return 0;
});
for(var i = 1; i < array.length; ){
if(array[i-1].title === array[i].title){
array.splice(i, 1);
} else {
i++;
}
}
return array;
}


/**
* Generates an array of Strings, where all duplicates have been removed
* (without changing the order)
* It is optimized for small arrays.
* Source : http://codereview.stackexchange.com/questions/60128/removing-duplicates-from-an-array-quickly
*
*
* @param {Array} array of String
* @returns {Array} same array of Strings, without duplicates
*/
Expand All @@ -118,7 +95,7 @@ define(['q'], function(q) {
}
return unique;
}

/**
* Read an integer encoded in 4 bytes, little endian
* @param {Array} byteArray
Expand All @@ -142,7 +119,7 @@ define(['q'], function(q) {
var integer = dataView.getUint16(0, true);
return integer;
}

/**
* Read a float encoded in 2 bytes
* @param {Array} byteArray
Expand Down Expand Up @@ -247,11 +224,11 @@ define(['q'], function(q) {
return mid;
});
}

/**
* Converts a Base64 Content to a Blob
* From https://stackoverflow.com/questions/16245767/creating-a-blob-from-a-base64-string-in-javascript
*
*
* @param {String} b64Data Base64-encoded data
* @param {String} contentType
* @param {Integer} sliceSize
Expand Down Expand Up @@ -280,11 +257,11 @@ define(['q'], function(q) {
var blob = new Blob(byteArrays, {type: contentType});
return blob;
}

/**
* Converts a UInt Array to a UTF-8 encoded string
* source : http://michael-rushanan.blogspot.de/2014/03/javascript-uint8array-hacks-and-cheat.html
*
*
* @param {UIntArray} uintArray
* @returns {String}
*/
Expand All @@ -295,12 +272,12 @@ define(['q'], function(q) {
}
return s;
}

/**
* Does a "left shift" on an integer.
* It is equivalent to int << bits (which works only on 32-bit integers),
* but compatible with 64-bit integers.
*
*
* @param {Integer} int
* @param {Integer} bits
* @returns {Integer}
Expand All @@ -317,7 +294,6 @@ define(['q'], function(q) {
ucFirstLetter: ucFirstLetter,
lcFirstLetter: lcFirstLetter,
ucEveryFirstLetter: ucEveryFirstLetter,
removeDuplicateTitlesInDirEntryArray: removeDuplicateTitlesInDirEntryArray,
removeDuplicateStringsInSmallArray: removeDuplicateStringsInSmallArray,
readIntegerFrom4Bytes: readIntegerFrom4Bytes,
readIntegerFrom2Bytes : readIntegerFrom2Bytes,
Expand Down