From 90506c39bb39ea3cbc77cbd22dd8179e94d46b68 Mon Sep 17 00:00:00 2001 From: Matěj Cepl Date: Sun, 17 Apr 2011 08:44:49 +0200 Subject: Auch! Say NO to the copy&paste inheritance! * leadingZero made into special function exported from xmlrpc module (it is now able to accept either number of string as its parameter) * added tests for leadingZero --- lib/logger.js | 14 +++----------- lib/xmlrpc.js | 24 ++++++++++++++++++------ 2 files changed, 21 insertions(+), 17 deletions(-) (limited to 'lib') diff --git a/lib/logger.js b/lib/logger.js index 7e0c516..d4c5dc9 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -79,21 +79,13 @@ exports.generateTimeSheet = function generateTimeSheet() { }; function timeSheetRecordsPrinter(records) { - function leadingZero(n) { - // pads a single number with a leading zero. Heh. - var s = n.toString(); - if (s.length === 1) { - s = "0" + s; - } - return s; - } - var commentBugRE = new RegExp("[bB]ug\\s+([0-9]+)","g"); // sort the records into temporary array var tmpArr = []; var today = new Date(); - var todayStr = today.getFullYear()+"-"+leadingZero(today.getMonth()+1)+ - "-"+leadingZero(today.getDate()); + var todayStr = today.getFullYear() + "-" + + xrpc.leadingZero(today.getMonth()+1) + "-" + + xrpc.leadingZero(today.getDate()); var outStr = '' + "\n"+ "\n"+ diff --git a/lib/xmlrpc.js b/lib/xmlrpc.js index 6576f31..e038c4e 100644 --- a/lib/xmlrpc.js +++ b/lib/xmlrpc.js @@ -26,15 +26,27 @@ * not an instance of SAME Array. */ -var dateToISO8601 = exports.dateToISO8601 = function dateToISO8601(date) { - function leadingZero(n) { - // pads a single number with a leading zero. Heh. - if (n.length === 1) { - n = "0" + n; - } +/** + * pads a single number with a leading zero. Heh. + * + * @param n String or Number + * @return String with leading zero added if necessary + * + * If the real parameter is not numerical, it is just returned as it is. + */ +var leadingZero = exports.leadingZero = function leadingZero(n) { + if (isNaN(Number(n))) { return n; + }; + + var s = "0" + n; + if (s.length > 2) { + s = s.slice(1); } + return s; +}; +var dateToISO8601 = exports.dateToISO8601 = function dateToISO8601(date) { // wow I hate working with the Date object console.log("date = " + date); var year = date.getYear(); -- cgit