Skip to content

Commit

Permalink
Added some more utility methods, not sure about dehash
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter West committed Jun 21, 2012
1 parent 2791722 commit 49d6188
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion util/util.js
Expand Up @@ -25,6 +25,15 @@ util.hash = function(array, fn) {
return obj;
};

// Produces an array from an object
util.dehash = function(obj, fn) {
var array = [];
for (key in obj) {
array.push(fn(obj[key], key));
};
return array;
};

// Combines two arrays into an object as key-value pairs
util.zip = function(array, keys) {
var obj = {};
Expand Down Expand Up @@ -75,4 +84,12 @@ util.regEscape = function(str) { return str.replace(/([.?*+^$[\]\\(){}|-])/g, "\
util.round = function(num, precision) {
var multiple = Math.pow(10, precision);
return Math.round(num * multiple) / multiple;
};
};

// Clear way to cast to values
util.cast = function(type, value) {
if (type == "boolean") return !!value;
if (type == "string") return value+"";
if (type == "integer") return parseInt(value);
if (type == "float") return parseFloat(value);
}

0 comments on commit 49d6188

Please sign in to comment.