Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

DynamicHelpers Ejemplo

View helpers.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
// Extraido de https://github.com/visionmedia/finance/blob/master/lib/helpers.js
 
/**
* Months.
*/
 
var months = [
'January'
, 'February'
, 'March'
, 'April'
, 'May'
, 'June'
, 'July'
, 'August'
, 'September'
, 'October'
, 'November'
, 'December'
];
 
/**
* Pad `n` with leading zero.
*/
 
function pad(n) {
return n < 10 ? '0' + n : n;
}
 
/**
* Format date.
*
* @param {String} fmt
* @param {Date} optional date
* @return {String}
*/
 
exports.date = function(fmt, date){
date = date || new Date;
return fmt
.replace(/mm/g, function(){
return pad(date.getMonth());
})
.replace(/dd/g, function(){
return pad(date.getDate());
})
.replace(/yy/g, function(){
return pad(date.getFullYear().toString().substr(2));
});
};
 
/**
* Parse date in the format of `"mm/dd/yy".
*
* @param {String} str
* @return {Date}
*/
 
exports.parseDate = function(str){
var date = new Date
, parts = str.split('/')
, month = parseInt(parts.shift(), 10)
, day = parseInt(parts.shift(), 10)
, year = parseInt(parts.shift(), 10) + 2000;
 
date.setMonth(month);
date.setDate(day);
date.setYear(year);
 
return date;
};
 
/**
* Return the month name for `n`.
*
* @param {Number} n
* @return {String}
*/
 
exports.monthName = function(n){
return months[n];
};
 
/**
* Return month no for `name`.
*
* @param {String} name
* @return {Number}
*/
 
exports.monthNumber = function(name){
name = name.toLowerCase();
for (var i = 0; i < 12; ++i) {
if (name == months[i].toLowerCase())
return i;
}
};
 
/**
* Check if `obj` has keys.
*
* @param {Object} obj
* @return {Boolean}
*/
 
exports.empty = function(obj){
return ! Object.keys(obj).length;
};
 
/**
* Return the sum of `type` in `items`.
*
* @param {Object} items
* @param {String} type
* @return {Number}
*/
 
exports.total = function(items, type){
return Object.keys(items).reduce(function(sum, key){
if (type != items[key].type) return sum;
return sum + items[key].amount;
}, 0);
};
 
/**
* Format `n` in dollars.
*
* @param {Number} n
* @return {String}
*/
 
exports.money = function(n){
return n.toFixed(2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.