1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-26 19:47:20 +00:00

Filtering optimisations

This commit is contained in:
Jermolene 2014-04-05 17:31:36 +01:00
parent b7f674c51a
commit 272a4bbe61
4 changed files with 34 additions and 30 deletions

View File

@ -73,13 +73,11 @@ Iterate through all the own properties of an object or array. Callback is invoke
$tw.utils.each = function(object,callback) {
var f;
if(object) {
if($tw.utils.isArray(object)) {
for(f=0; f<object.length; f++) {
callback(object[f],f,object);
}
if(Object.prototype.toString.call(object) == "[object Array]") {
object.forEach(callback);
} else {
for(f in object) {
if($tw.utils.hop(object,f)) {
if(Object.prototype.hasOwnProperty.call(object,f)) {
callback(object[f],f,object);
}
}
@ -840,20 +838,16 @@ $tw.Wiki = function(options) {
var t = tiddlers[title];
if(t instanceof $tw.Tiddler) {
return t;
} else if(title !== undefined && $tw.utils.hop(shadowTiddlers,title)) {
} else if(title !== undefined && Object.prototype.hasOwnProperty.call(shadowTiddlers,title)) {
return shadowTiddlers[title].tiddler;
} else {
return undefined;
}
};
// Get a hashmap of all tiddler titles
this.getAllTitles = function() {
var results = {};
for(var title in tiddlers) {
results[title] = true;
}
return results;
// Get an array of all tiddler titles
this.allTitles = function() {
return Object.keys(tiddlers);
};
// Iterate through all tiddler titles
@ -863,6 +857,11 @@ $tw.Wiki = function(options) {
}
};
// Get an array of all shadow tiddler titles
this.allShadowTitles = function() {
return Object.keys(shadowTiddlers);
};
// Iterate through all shadow tiddler titles
this.eachShadow = function(callback) {
for(var title in shadowTiddlers) {

View File

@ -16,11 +16,7 @@ Filter function for [all[shadows]]
Export our filter function
*/
exports.shadows = function(source,prefix,options) {
var results = [];
options.wiki.eachShadow(function(tiddler,title) {
results.push(title);
});
return results;
return options.wiki.allShadowTitles();
};
})();

View File

@ -16,11 +16,7 @@ Filter function for [all[tiddlers]]
Export our filter function
*/
exports.tiddlers = function(source,prefix,options) {
var results = [];
options.wiki.each(function(tiddler,title) {
results.push(title);
});
return results;
return options.wiki.allTitles();
};
})();

View File

@ -35,21 +35,34 @@ exports.count = function(object) {
/*
Push entries onto an array, removing them first if they already exist in the array
array: array to modify
array: array to modify (assumed to be free of duplicates)
value: a single value to push or an array of values to push
*/
exports.pushTop = function(array,value) {
var t,p;
if($tw.utils.isArray(value)) {
// Remove any array entries that are duplicated in the new values
if(value.length !== 0) {
if(array.length !== 0) {
if(value.length < array.length) {
for(t=0; t<value.length; t++) {
p = array.indexOf(value[t]);
if(p !== -1) {
array.splice(p,1);
}
}
} else {
for(t=array.length-1; t>=0; t--) {
p = value.indexOf(array[t]);
if(p !== -1) {
array.splice(t,1);
}
}
}
}
// Push the values on top of the main array
array.push.apply(array,value);
}
} else {
p = array.indexOf(value);
if(p !== -1) {