From f7e89bd75847ef8dfcbebf4effbfc5fbb38201be Mon Sep 17 00:00:00 2001 From: Sami Samhuri Date: Mon, 13 Jun 2011 23:05:25 -0700 Subject: [PATCH] test locales, s/getLocalizedStrftime/localizedStrftime/ --- Readme.md | 4 ++- lib/index.js | 10 +++++--- test/test.js | 72 +++++++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 70 insertions(+), 16 deletions(-) diff --git a/Readme.md b/Readme.md index 62ed136..40ea846 100644 --- a/Readme.md +++ b/Readme.md @@ -37,11 +37,13 @@ And if you don't want to pass a localization object every time you can get a loc var strftime = require('strftime') var it_IT = { /* same as above */ } - var strftime_IT = strftime.getLocalizedStrftime(it_IT) + var strftime_IT = strftime.localizedStrftime(it_IT) console.log(strftime_IT('%B %d, %y %H:%M:%S')) // aprile 28, 2011 18:21:08 For details just see `man 3 strftime` as the format specifiers are identical. +**NOTE:** `getLocalizedStrftime` is deprecated, use `localizedStrftime` instead. `getLocalizedStrftime` will be removed in 0.5 or 0.6. + License ======= diff --git a/lib/index.js b/lib/index.js index 6318590..d120778 100644 --- a/lib/index.js +++ b/lib/index.js @@ -23,7 +23,11 @@ namespace.strftime = strftime; namespace.strftimeUTC = strftimeUTC; - namespace.getLocalizedStrftime = getLocalizedStrftime; + namespace.localizedStrftime = localizedStrftime; + namespace.getLocalizedStrftime = function(locale) { + console.log('[strftime] DEPRECATION NOTICE: getLocalizedStrftime is deprecated, use localizedStrftime instead'); + return (namespace.getLocalizedStrftime = localizedStrftime)(locale); + }; //// function words(s) { return (s || '').split(' '); } @@ -45,7 +49,7 @@ return _strftime(fmt, d, locale, true); } - function getLocalizedStrftime(locale) { + function localizedStrftime(locale) { return function(fmt, d) { return strftime(fmt, d, locale); }; @@ -60,7 +64,7 @@ } d = d || new Date(); locale = locale || DefaultLocale; - locale.formats = locale.formats || {} + locale.formats = locale.formats || {}; var msDelta = 0; if (_useUTC) { msDelta = (d.getTimezoneOffset() || 0) * 60000; diff --git a/test/test.js b/test/test.js index beb5164..8d66cea 100644 --- a/test/test.js +++ b/test/test.js @@ -11,19 +11,10 @@ var assert = require('assert') // Tue, 07 Jun 2011 18:51:45 GMT , Time = new Date(1307472705067) -/// check exports - assert.fn = function(value, msg) { assert.equal('function', typeof value, msg) } -assert.fn(lib.strftime) -assert.fn(lib.strftimeUTC) -assert.fn(lib.getLocalizedStrftime) - - -/// check formats - assert.format = function(format, expected, expectedUTC) { function _assertFmt(expected, name) { name = name || 'strftime' @@ -37,6 +28,15 @@ assert.format = function(format, expected, expectedUTC) { _assertFmt(expectedUTC || expected, 'strftimeUTC') } +/// check exports +assert.fn(lib.strftime) +assert.fn(lib.strftimeUTC) +assert.fn(lib.getLocalizedStrftime) + +/// time zones +testTimezone('P[DS]T') + +/// check all formats in GMT, most coverage assert.format('%A', 'Tuesday') assert.format('%a', 'Tue') assert.format('%B', 'June') @@ -71,9 +71,57 @@ assert.format('%%', '%') // any other char ok('GMT') -// PST/PDT -testTimezone('P[DS]T') -// test other time zones you care about here + +/// locales + +var it_IT = +{ days: words('domenica lunedi martedi mercoledi giovedi venerdi sabato') +, shortDays: words('dom lun mar mer gio ven sab') +, months: words('gennaio febbraio marzo aprile maggio giugno luglio agosto settembre ottobre novembre dicembre') +, shortMonths: words('gen feb mar apr mag giu lug ago set ott nov dic') +, AM: 'it$AM' +, PM: 'it$PM' +, formats: { + D: 'it$%m/%d/%y' + , F: 'it$%Y-%m-%d' + , R: 'it$%H:%M' + , r: 'it$%I:%M:%S %p' + , T: 'it$%H:%M:%S' + , v: 'it$%e-%b-%Y' + } +} + +assert.format_it = function(format, expected, expectedUTC) { + function _assertFmt(expected, name) { + name = name || 'strftime' + var actual = lib[name](format, Time, it_IT) + assert.equal(expected, actual, + name + '("' + format + '", Time) is ' + JSON.stringify(actual) + + ', expected ' + JSON.stringify(expected)) + } + + if (expected) _assertFmt(expected) + _assertFmt(expectedUTC || expected, 'strftimeUTC') +} + +assert.format_it('%A', 'martedi') +assert.format_it('%a', 'mar') +assert.format_it('%B', 'giugno') +assert.format_it('%b', 'giu') +assert.format_it('%D', 'it$06/07/11') +assert.format_it('%F', 'it$2011-06-07') +assert.format_it('%p', null, 'it$PM') +assert.format_it('%R', null, 'it$18:51') +assert.format_it('%r', null, 'it$06:51:45 it$PM') +assert.format_it('%T', null, 'it$18:51:45') +assert.format_it('%v', 'it$7-giu-2011') + +ok('Locales') + + +/// helpers + +function words(s) { return (s || '').split(' '); } function ok(s) { console.log('[ \033[32mOK\033[0m ] ' + s) }