From 081c96503091e21f16f074b7f3a6cfb3bf8bc169 Mon Sep 17 00:00:00 2001 From: DualFroz Date: Fri, 2 Oct 2026 15:56:00 +0200 Subject: [PATCH] fix(whereIts): return titlecased timezone names --- src/timezone/index.js | 19 +------------------ src/timezone/titleCase.js | 18 ++++++++++++++++++ src/whereIts.js | 3 ++- tests/timezones/timezone-name.test.js | 9 +++++++++ 4 files changed, 30 insertions(+), 19 deletions(-) create mode 100644 src/timezone/titleCase.js diff --git a/src/timezone/index.js b/src/timezone/index.js index 206a91a..942f5ea 100644 --- a/src/timezone/index.js +++ b/src/timezone/index.js @@ -2,6 +2,7 @@ import findTz from './find.js' import inSummerTime from './summerTime.js' import getDstShift from './dstShift.js' +import titleCase from './titleCase.js' const parseDst = dst => { if (!dst) { @@ -10,24 +11,6 @@ const parseDst = dst => { return dst.split('->') } -//iana codes are case-sensitive, technically -const titleCase = str => { - str = str[0].toUpperCase() + str.substr(1) - str = str.replace(/[/_-]([a-z])/gi, s => { - return s.toUpperCase() - }) - str = str.replace(/_(of|es)_/i, (s) => s.toLowerCase()) - str = str.replace(/\/gmt/i, '/GMT') - str = str.replace(/\/utc/i, '/UTC') - str = str.replace(/\/Dumontdurville$/i, '/DumontDUrville') - str = str.replace(/\/Mcmurdo$/i, '/McMurdo') - str = str.replace(/\/Port-au-prince$/i, '/Port-au-Prince') - if (str === 'Utc') { - str = 'UTC' - } - return str -} - //get metadata about this timezone const timezone = s => { const zones = s.timezones diff --git a/src/timezone/titleCase.js b/src/timezone/titleCase.js new file mode 100644 index 0000000..63175b6 --- /dev/null +++ b/src/timezone/titleCase.js @@ -0,0 +1,18 @@ +//iana codes are case-sensitive, technically +const titleCase = str => { + str = str[0].toUpperCase() + str.substr(1) + str = str.replace(/[/_-]([a-z])/gi, s => { + return s.toUpperCase() + }) + str = str.replace(/_(of|es)_/i, (s) => s.toLowerCase()) + str = str.replace(/\/gmt/i, '/GMT') + str = str.replace(/\/utc/i, '/UTC') + str = str.replace(/\/Dumontdurville$/i, '/DumontDUrville') + str = str.replace(/\/Mcmurdo$/i, '/McMurdo') + str = str.replace(/\/Port-au-prince$/i, '/Port-au-Prince') + if (str === 'Utc') { + str = 'UTC' + } + return str +} +export default titleCase diff --git a/src/whereIts.js b/src/whereIts.js index 0b186ab..72fe667 100644 --- a/src/whereIts.js +++ b/src/whereIts.js @@ -1,4 +1,5 @@ import Spacetime from './spacetime.js' +import titleCase from './timezone/titleCase.js' // const timezones = require('../data'); const whereIts = (a, b) => { @@ -33,6 +34,6 @@ const whereIts = (a, b) => { } return false }) - return tzs + return tzs.map(titleCase) } export default whereIts diff --git a/tests/timezones/timezone-name.test.js b/tests/timezones/timezone-name.test.js index b0f5a08..779d520 100644 --- a/tests/timezones/timezone-name.test.js +++ b/tests/timezones/timezone-name.test.js @@ -23,3 +23,12 @@ test('titlecase', (t) => { }) t.end() }) + +test('whereIts returns titlecased names', (t) => { + const tzs = spacetime.whereIts('12:00am', '11:59pm') + t.ok(tzs.includes('Antarctica/Mawson'), 'Antarctica/Mawson') + t.ok(tzs.includes('America/Port-au-Prince'), 'America/Port-au-Prince') + const wrong = tzs.filter(tz => tz !== spacetime.now(tz).timezone().name) + t.deepEqual(wrong, [], 'same name as timezone()') + t.end() +})