mirror of
https://github.com/samsonjs/strftime.git
synced 2026-04-27 14:57:37 +00:00
refactor, support exporting to global scope in ES5 strict mode
This commit is contained in:
parent
200dc2ef5e
commit
cd4d2a3ec5
1 changed files with 119 additions and 115 deletions
76
lib/index.js
76
lib/index.js
|
|
@ -7,17 +7,34 @@
|
|||
|
||||
;(function() {
|
||||
|
||||
var DefaultLocale = {
|
||||
days: [ 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' ],
|
||||
shortDays: [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ],
|
||||
//// Export the API
|
||||
var namespace;
|
||||
|
||||
months: [ 'January', 'February', 'March', 'April', 'May', 'June', 'July',
|
||||
'August', 'September', 'October', 'November', 'December' ],
|
||||
// CommonJS / Node module
|
||||
if (typeof exports !== 'undefined') {
|
||||
namespace = exports;
|
||||
}
|
||||
|
||||
shortMonths: [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
|
||||
'Sep', 'Oct', 'Nov', 'Dec' ],
|
||||
AM: 'AM',
|
||||
PM: 'PM'
|
||||
// Browsers and other environments
|
||||
else {
|
||||
// Get the global object. Works in ES3, ES5, and ES5 strict mode.
|
||||
namespace = (function(){ return this || (1,eval)('this') }());
|
||||
}
|
||||
|
||||
namespace.strftime = strftime;
|
||||
namespace.strftimeUTC = strftimeUTC;
|
||||
namespace.getLocalizedStrftime = getLocalizedStrftime;
|
||||
////
|
||||
|
||||
function words(s) { return (s || '').split(' '); }
|
||||
|
||||
var DefaultLocale =
|
||||
{ days: words('Sunday Monday Tuesday Wednesday Thursday Friday Saturday')
|
||||
, shortDays: words('Sun Mon Tue Wed Thu Fri Sat')
|
||||
, months: words('January February March April May June July August September October November December')
|
||||
, shortMonths: words('Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec')
|
||||
, AM: 'AM'
|
||||
, PM: 'PM'
|
||||
}
|
||||
|
||||
function pad(n, padding) {
|
||||
|
|
@ -32,6 +49,20 @@
|
|||
return hour;
|
||||
}
|
||||
|
||||
function strftime(fmt, d, locale) {
|
||||
return _strftime(fmt, d, locale, false);
|
||||
}
|
||||
|
||||
function strftimeUTC(fmt, d, locale) {
|
||||
return _strftime(fmt, d, locale, true);
|
||||
}
|
||||
|
||||
function getLocalizedStrftime(locale) {
|
||||
return function(fmt, d) {
|
||||
return strftime(fmt, d, locale);
|
||||
};
|
||||
}
|
||||
|
||||
// locale is an object with the same structure as DefaultLocale
|
||||
function _strftime(fmt, d, locale, _useUTC) {
|
||||
// d and locale are optional so check if d is really the locale
|
||||
|
|
@ -42,7 +73,6 @@
|
|||
d = d || new Date();
|
||||
locale = locale || DefaultLocale;
|
||||
locale.formats = locale.formats || {}
|
||||
|
||||
if (_useUTC) {
|
||||
d = new Date(d.getTime() + ((d.getTimezoneOffset() || 0) * 60000));
|
||||
}
|
||||
|
|
@ -103,30 +133,4 @@
|
|||
});
|
||||
}
|
||||
|
||||
function strftime(fmt, d, locale) {
|
||||
return _strftime(fmt, d, locale, false);
|
||||
}
|
||||
|
||||
function strftimeUTC(fmt, d, locale) {
|
||||
return _strftime(fmt, d, locale, true);
|
||||
}
|
||||
|
||||
function getLocalizedStrftime(locale) {
|
||||
return function(fmt, d) {
|
||||
return strftime(fmt, d, locale);
|
||||
};
|
||||
}
|
||||
|
||||
if (typeof exports !== 'undefined') {
|
||||
exports.strftime = strftime;
|
||||
exports.strftimeUTC = strftimeUTC;
|
||||
exports.getLocalizedStrftime = getLocalizedStrftime;
|
||||
} else {
|
||||
(function(global) {
|
||||
global.strftime = strftime;
|
||||
global.strftimeUTC = strftimeUTC;
|
||||
global.getLocalizedStrftime = getLocalizedStrftime;
|
||||
}(this));
|
||||
}
|
||||
|
||||
}());
|
||||
Loading…
Reference in a new issue