From 0add1d0fcf476a365b61f524f336d7634c3ec17a Mon Sep 17 00:00:00 2001 From: Tarjei Huse Date: Mon, 7 Oct 2019 15:55:07 +0200 Subject: [PATCH 1/2] formatTimeItem does not work on single digits This is with the formatTimeItem implementation above: ``` ft = value => `00${value || ''}`.substr(-2, 2) > ft(0) '00' > ft(1) '01' > ft(18) '18' > ft(60) '60' > ft(59) '59' > ``` This is with the old one: ``` ft = value => `${value || ''}00`.substr(0, 2) [Function: ft] > ft(59) '59' > ft(1) '10' > ft(18) '18' > ft(8) '80' ``` ``` --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 002c227..c91644f 100644 --- a/src/index.js +++ b/src/index.js @@ -11,7 +11,7 @@ export function isNumber(value) { } export function formatTimeItem(value) { - return `${value || ''}00`.substr(0, 2); + return `00${value || ''}`.substr(2, -2); } export function validateTimeAndCursor( From 8da6c54fae2ccfd29a59143f6e45c70af8dca030 Mon Sep 17 00:00:00 2001 From: Tarjei Huse Date: Mon, 7 Oct 2019 17:36:37 +0200 Subject: [PATCH 2/2] Update tests and fix bug --- src/index.js | 14 +++++++++++++- tests/unit.test.js | 12 +++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/index.js b/src/index.js index c91644f..18c5107 100644 --- a/src/index.js +++ b/src/index.js @@ -11,7 +11,19 @@ export function isNumber(value) { } export function formatTimeItem(value) { - return `00${value || ''}`.substr(2, -2); + var v = parseInt(value, 10); + if (isNaN(v)) { + return '00'; + } + + if (v < 10) { + return `0${v}`; + } + if (v > 99) { + return `${v}`.substr(0, 2); + } + + return `${v}`; } export function validateTimeAndCursor( diff --git a/tests/unit.test.js b/tests/unit.test.js index 29fc4de..a3d68bb 100644 --- a/tests/unit.test.js +++ b/tests/unit.test.js @@ -21,10 +21,12 @@ describe('#formatTimeItem()', () => { }); test('should return formated value', () => { - expect(formatTimeItem('1')).toBe('10'); + expect(formatTimeItem('1')).toBe('01'); + expect(formatTimeItem('8')).toBe('08'); + expect(formatTimeItem('10')).toBe('10'); expect(formatTimeItem('11')).toBe('11'); expect(formatTimeItem('111')).toBe('11'); - expect(formatTimeItem(2)).toBe('20'); + expect(formatTimeItem(2)).toBe('02'); }); }); @@ -65,7 +67,7 @@ describe('#validateTimeAndCursor()', () => { expect(validateTimeAndCursor(false, '12:00', DF)[0]).toEqual('12:00'); expect(validateTimeAndCursor(false, '23:00', DF)[0]).toEqual('23:00'); expect(validateTimeAndCursor(false, '24:00', DF)[0]).toEqual('23:00'); - expect(validateTimeAndCursor(false, '1:00', DF)[0]).toEqual('10:00'); + expect(validateTimeAndCursor(false, '1:00', DF)[0]).toEqual('01:00'); expect(validateTimeAndCursor(false, '24:00', '21:00')[0]).toEqual('21:00'); }); @@ -74,7 +76,7 @@ describe('#validateTimeAndCursor()', () => { expect(validateTimeAndCursor(false, '12:30', DF)[0]).toEqual('12:30'); expect(validateTimeAndCursor(false, '12:59', DF)[0]).toEqual('12:59'); expect(validateTimeAndCursor(false, '12:60', DF)[0]).toEqual('12:00'); - expect(validateTimeAndCursor(false, '12:1', DF)[0]).toEqual('12:10'); + expect(validateTimeAndCursor(false, '12:1', DF)[0]).toEqual('12:01'); }); test('should validate seconds', () => { @@ -82,6 +84,6 @@ describe('#validateTimeAndCursor()', () => { expect(validateTimeAndCursor(true, '12:00:30', DF)[0]).toEqual('12:00:30'); expect(validateTimeAndCursor(true, '12:00:59', DF)[0]).toEqual('12:00:59'); expect(validateTimeAndCursor(true, '12:00:60', DF)[0]).toEqual('12:00:00'); - expect(validateTimeAndCursor(true, '12:00:1', DF)[0]).toEqual('12:00:10'); + expect(validateTimeAndCursor(true, '12:00:1', DF)[0]).toEqual('12:00:01'); }); });