1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-06-18 03:19:55 +00:00

Less naive fix

Now we make sure we maintain the sort order of the titles array when adding a new tiddler
This commit is contained in:
jeremy@jermolene.com 2022-01-04 09:59:23 +00:00
parent 51175616d3
commit 13d1f6d6c1
2 changed files with 32 additions and 4 deletions

View File

@ -68,6 +68,26 @@ $tw.utils.isArrayEqual = function(array1,array2) {
});
};
/*
Add an entry to a sorted array if it doesn't already exist, while maintaining the sort order
*/
$tw.utils.insertSortedArray = function(array,value) {
var low = 0, high = array.length - 1, mid, cmp;
while(low <= high) {
mid = (low + high) >> 1;
cmp = array[mid];
if(value > cmp) {
low = mid + 1;
} else if(value < cmp) {
high = mid - 1;
} else {
return array;
}
}
array.splice(low,0,value);
return array;
};
/*
Push entries onto an array, removing them first if they already exist in the array
array: array to modify (assumed to be free of duplicates)
@ -1134,10 +1154,8 @@ $tw.Wiki = function(options) {
}
// Save the new tiddler
tiddlers[title] = tiddler;
// Check we've got it's title
if(tiddlerTitles && tiddlerTitles.indexOf(title) === -1) {
tiddlerTitles.push(title);
}
// Check we've got the title
tiddlerTitles = $tw.utils.insertSortedArray(tiddlerTitles || [],title);
// Record the new tiddler state
updateDescriptor["new"] = {
tiddler: tiddler,

View File

@ -169,6 +169,16 @@ describe("Utility tests", function() {
expect(cv("1.1.1","1.1.2")).toEqual(-1);
});
it("should insert strings into sorted arrays", function() {
expect($tw.utils.insertSortedArray([],"a").join(",")).toEqual("a");
expect($tw.utils.insertSortedArray(["b","c","d"],"a").join(",")).toEqual("a,b,c,d");
expect($tw.utils.insertSortedArray(["b","c","d"],"d").join(",")).toEqual("b,c,d");
expect($tw.utils.insertSortedArray(["b","c","d"],"f").join(",")).toEqual("b,c,d,f");
expect($tw.utils.insertSortedArray(["b","c","d","e"],"f").join(",")).toEqual("b,c,d,e,f");
expect($tw.utils.insertSortedArray(["b","c","g"],"f").join(",")).toEqual("b,c,f,g");
expect($tw.utils.insertSortedArray(["b","c","d"],"ccc").join(",")).toEqual("b,c,ccc,d");
});
});
})();