1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-30 05:19:57 +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) { $tw.utils.each = function(object,callback) {
var f; var f;
if(object) { if(object) {
if($tw.utils.isArray(object)) { if(Object.prototype.toString.call(object) == "[object Array]") {
for(f=0; f<object.length; f++) { object.forEach(callback);
callback(object[f],f,object);
}
} else { } else {
for(f in object) { for(f in object) {
if($tw.utils.hop(object,f)) { if(Object.prototype.hasOwnProperty.call(object,f)) {
callback(object[f],f,object); callback(object[f],f,object);
} }
} }
@ -840,20 +838,16 @@ $tw.Wiki = function(options) {
var t = tiddlers[title]; var t = tiddlers[title];
if(t instanceof $tw.Tiddler) { if(t instanceof $tw.Tiddler) {
return t; 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; return shadowTiddlers[title].tiddler;
} else { } else {
return undefined; return undefined;
} }
}; };
// Get a hashmap of all tiddler titles // Get an array of all tiddler titles
this.getAllTitles = function() { this.allTitles = function() {
var results = {}; return Object.keys(tiddlers);
for(var title in tiddlers) {
results[title] = true;
}
return results;
}; };
// Iterate through all tiddler titles // 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 // Iterate through all shadow tiddler titles
this.eachShadow = function(callback) { this.eachShadow = function(callback) {
for(var title in shadowTiddlers) { for(var title in shadowTiddlers) {

View File

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

View File

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

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 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 value: a single value to push or an array of values to push
*/ */
exports.pushTop = function(array,value) { exports.pushTop = function(array,value) {
var t,p; var t,p;
if($tw.utils.isArray(value)) { if($tw.utils.isArray(value)) {
// Remove any array entries that are duplicated in the new values // 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++) { for(t=0; t<value.length; t++) {
p = array.indexOf(value[t]); p = array.indexOf(value[t]);
if(p !== -1) { if(p !== -1) {
array.splice(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 // Push the values on top of the main array
array.push.apply(array,value); array.push.apply(array,value);
}
} else { } else {
p = array.indexOf(value); p = array.indexOf(value);
if(p !== -1) { if(p !== -1) {