1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-06-14 01:26:48 +00:00

Improve $tw.each() and $tw.modules.applyMethods()

This commit is contained in:
Jeremy Ruston 2012-12-13 21:31:19 +00:00
parent d0ebb159df
commit 7c712c4e03

View File

@ -119,19 +119,6 @@ $tw.utils.hop = function(object,property) {
return Object.prototype.hasOwnProperty.call(object,property);
};
/*
Iterate through all the own properties of an object. Callback is invoked with (element,title,object)
*/
$tw.utils.each = function(object,callback) {
if(object) {
for(var f in object) {
if($tw.utils.hop(object,f)) {
callback(object[f],f,object);
}
}
}
}
/*
Determine if a value is an array
*/
@ -139,6 +126,26 @@ $tw.utils.isArray = function(value) {
return Object.prototype.toString.call(value) == "[object Array]";
};
/*
Iterate through all the own properties of an object or array. Callback is invoked with (element,title,object)
*/
$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);
}
} else {
for(f in object) {
if($tw.utils.hop(object,f)) {
callback(object[f],f,object);
}
}
}
}
};
/*
Convert "&amp;" to &, "&lt;" to <, "&gt;" to > and "&quot;" to "
*/
@ -426,11 +433,15 @@ $tw.modules.getModulesByTypeAsHashmap = function(moduleType,nameField) {
Apply the exports of the modules of a particular type to a target object
*/
$tw.modules.applyMethods = function(moduleType,targetObject) {
if(!targetObject) {
targetObject = {};
}
$tw.modules.forEachModuleOfType(moduleType,function(title,module) {
$tw.utils.each(module,function(element,title,object) {
targetObject[title] = module[title];
});
});
return targetObject;
};
/////////////////////////// Barebones tiddler object