diff --git a/4chan_x.user.js b/4chan_x.user.js index 3bfededcb..7396a95c2 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -1950,26 +1950,10 @@ foo: function() { var code; code = conf['time'].replace(/%([A-Za-z])/g, function(s, c) { - switch (c) { - case 'a': - case 'A': - case 'b': - case 'B': - case 'd': - case 'e': - case 'H': - case 'I': - case 'k': - case 'l': - case 'm': - case 'M': - case 'p': - case 'P': - case 'y': - return "' + Time." + c + "() + '"; - break; - default: - return s; + if (c in Time.formatters) { + return "' + Time.formatters." + c + "() + '"; + } else { + return s; } }); return Time.funk = Function('Time', "return '" + code + "'"); @@ -1983,58 +1967,60 @@ return n; } }, - a: function() { - return this.day[this.date.getDay()].slice(0, 3); - }, - A: function() { - return this.day[this.date.getDay()]; - }, - b: function() { - return this.month[this.date.getMonth()].slice(0, 3); - }, - B: function() { - return this.month[this.date.getMonth()]; - }, - d: function() { - return this.zeroPad(this.date.getDate()); - }, - e: function() { - return this.date.getDate(); - }, - H: function() { - return this.zeroPad(this.date.getHours()); - }, - I: function() { - return this.zeroPad(this.date.getHours() % 12 || 12); - }, - k: function() { - return this.date.getHours(); - }, - l: function() { - return this.date.getHours() % 12 || 12; - }, - m: function() { - return this.zeroPad(this.date.getMonth() + 1); - }, - M: function() { - return this.zeroPad(this.date.getMinutes()); - }, - p: function() { - if (this.date.getHours() < 12) { - return 'AM'; - } else { - return 'PM'; + formatters: { + a: function() { + return Time.day[Time.date.getDay()].slice(0, 3); + }, + A: function() { + return Time.day[Time.date.getDay()]; + }, + b: function() { + return Time.month[Time.date.getMonth()].slice(0, 3); + }, + B: function() { + return Time.month[Time.date.getMonth()]; + }, + d: function() { + return Time.zeroPad(Time.date.getDate()); + }, + e: function() { + return Time.date.getDate(); + }, + H: function() { + return Time.zeroPad(Time.date.getHours()); + }, + I: function() { + return Time.zeroPad(Time.date.getHours() % 12 || 12); + }, + k: function() { + return Time.date.getHours(); + }, + l: function() { + return Time.date.getHours() % 12 || 12; + }, + m: function() { + return Time.zeroPad(Time.date.getMonth() + 1); + }, + M: function() { + return Time.zeroPad(Time.date.getMinutes()); + }, + p: function() { + if (Time.date.getHours() < 12) { + return 'AM'; + } else { + return 'PM'; + } + }, + P: function() { + if (Time.date.getHours() < 12) { + return 'am'; + } else { + return 'pm'; + } + }, + y: function() { + return Time.date.getFullYear() - 2000; } - }, - P: function() { - if (this.date.getHours() < 12) { - return 'am'; - } else { - return 'pm'; - } - }, - y: function() { - return this.date.getFullYear() - 2000; } }; titlePost = { diff --git a/script.coffee b/script.coffee index f70d510c1..555c0dfce 100644 --- a/script.coffee +++ b/script.coffee @@ -1557,9 +1557,10 @@ Time = $.replace s, time foo: -> code = conf['time'].replace /%([A-Za-z])/g, (s, c) -> - switch c - when 'a', 'A', 'b', 'B', 'd', 'e', 'H', 'I', 'k', 'l', 'm', 'M', 'p', 'P', 'y' then "' + Time.#{c}() + '" - else s + if c of Time.formatters + "' + Time.formatters.#{c}() + '" + else + s Time.funk = Function 'Time', "return '#{code}'" day: [ 'Sunday' @@ -1585,21 +1586,22 @@ Time = 'December' ] zeroPad: (n) -> if n < 10 then '0' + n else n - a: -> @day[@date.getDay()][...3] - A: -> @day[@date.getDay()] - b: -> @month[@date.getMonth()][...3] - B: -> @month[@date.getMonth()] - d: -> @zeroPad @date.getDate() - e: -> @date.getDate() - H: -> @zeroPad @date.getHours() - I: -> @zeroPad @date.getHours() % 12 or 12 - k: -> @date.getHours() - l: -> @date.getHours() % 12 or 12 - m: -> @zeroPad @date.getMonth() + 1 - M: -> @zeroPad @date.getMinutes() - p: -> if @date.getHours() < 12 then 'AM' else 'PM' - P: -> if @date.getHours() < 12 then 'am' else 'pm' - y: -> @date.getFullYear() - 2000 + formatters: + a: -> Time.day[Time.date.getDay()][...3] + A: -> Time.day[Time.date.getDay()] + b: -> Time.month[Time.date.getMonth()][...3] + B: -> Time.month[Time.date.getMonth()] + d: -> Time.zeroPad Time.date.getDate() + e: -> Time.date.getDate() + H: -> Time.zeroPad Time.date.getHours() + I: -> Time.zeroPad Time.date.getHours() % 12 or 12 + k: -> Time.date.getHours() + l: -> Time.date.getHours() % 12 or 12 + m: -> Time.zeroPad Time.date.getMonth() + 1 + M: -> Time.zeroPad Time.date.getMinutes() + p: -> if Time.date.getHours() < 12 then 'AM' else 'PM' + P: -> if Time.date.getHours() < 12 then 'am' else 'pm' + y: -> Time.date.getFullYear() - 2000 titlePost = init: ->