From 1ce7cd68f911e7a8d581feec1814d38d94ddbd7a Mon Sep 17 00:00:00 2001 From: Dirk Zittersteyn Date: Wed, 6 Jul 2016 16:14:32 -0700 Subject: [PATCH 1/4] Have ordinal return 0th instead of 0 --- __tests__/humanize.spec.js | 4 ++-- src/humanize.js | 4 ---- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/__tests__/humanize.spec.js b/__tests__/humanize.spec.js index b49aee9..f5593f2 100644 --- a/__tests__/humanize.spec.js +++ b/__tests__/humanize.spec.js @@ -45,8 +45,8 @@ describe('compactInteger tests', () => { describe('Ordinal value of numbers Test Suite', () => { describe('Ordinal value for numbers ending in zero', () => { - it('should return 0 if the number is 0 (cos 0th doesnt read very well)', () => { - expect(Humanize.ordinal(0)).toEqual(0); + it('should return 0th if the number is 0', () => { + expect(Humanize.ordinal(0)).toEqual('0th'); }); it('should return the number with suffix th', () => { diff --git a/src/humanize.js b/src/humanize.js index afe4354..2438fcc 100644 --- a/src/humanize.js +++ b/src/humanize.js @@ -220,10 +220,6 @@ ordinal(value) { const number = parseInt(value, 10); - if (number === 0) { - return value; - } - const specialCase = number % 100; if ([11, 12, 13].indexOf(specialCase) >= 0) { return `${ number }th`; From 223fcd281c0ad2fa68f743a2becbcf123ed3467d Mon Sep 17 00:00:00 2001 From: Dirk Zittersteyn Date: Thu, 7 Jul 2016 12:58:16 -0700 Subject: [PATCH 2/4] add zeroth option to ordinal --- __tests__/humanize.spec.js | 8 ++++++-- src/humanize.js | 7 ++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/__tests__/humanize.spec.js b/__tests__/humanize.spec.js index f5593f2..13fd043 100644 --- a/__tests__/humanize.spec.js +++ b/__tests__/humanize.spec.js @@ -45,8 +45,12 @@ describe('compactInteger tests', () => { describe('Ordinal value of numbers Test Suite', () => { describe('Ordinal value for numbers ending in zero', () => { - it('should return 0th if the number is 0', () => { - expect(Humanize.ordinal(0)).toEqual('0th'); + it('should return 0 if the number is 0', () => { + expect(Humanize.ordinal(0)).toEqual(0); + }); + + it('should return 0th if the number is 0, and the zeroth option is passed', () => { + expect(Humanize.ordinal(0, {zeroth: true})).toEqual('0th'); }); it('should return the number with suffix th', () => { diff --git a/src/humanize.js b/src/humanize.js index 2438fcc..0bc91cf 100644 --- a/src/humanize.js +++ b/src/humanize.js @@ -217,9 +217,14 @@ }, // Converts an integer to its ordinal as a string. - ordinal(value) { + ordinal(value, options = {}) { + const { zeroth = false } = options; const number = parseInt(value, 10); + if (number === 0 && !zeroth) { + return value; + } + const specialCase = number % 100; if ([11, 12, 13].indexOf(specialCase) >= 0) { return `${ number }th`; From 49be39456ae06b1f4d067d4178670362851435a0 Mon Sep 17 00:00:00 2001 From: Dirk Zittersteyn Date: Thu, 7 Jul 2016 13:20:24 -0700 Subject: [PATCH 3/4] add spec for shortcutting behavior --- __tests__/humanize.spec.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/__tests__/humanize.spec.js b/__tests__/humanize.spec.js index 13fd043..38b3720 100644 --- a/__tests__/humanize.spec.js +++ b/__tests__/humanize.spec.js @@ -49,6 +49,10 @@ describe('Ordinal value of numbers Test Suite', () => { expect(Humanize.ordinal(0)).toEqual(0); }); + it('should return the input if the number is 0', () => { + expect(Humanize.ordinal('0.')).toEqual('0.'); + }); + it('should return 0th if the number is 0, and the zeroth option is passed', () => { expect(Humanize.ordinal(0, {zeroth: true})).toEqual('0th'); }); From fd4b699b2ac83eb6def9579ff199341feb916e59 Mon Sep 17 00:00:00 2001 From: Dirk Zittersteyn Date: Thu, 7 Jul 2016 13:20:31 -0700 Subject: [PATCH 4/4] Add zeroth option to readme --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index 2eb3ecb..c2077bc 100644 --- a/README.md +++ b/README.md @@ -114,6 +114,17 @@ Humanize.ordinal(22) // "22nd" ``` +By default returns the input if it evaluates to 0, this can be overridden by passing the option `zeroth`: + +```javascript +Humanize.ordinal(0) +// 0 +Humanize.ordinal('0.') +// "0." +Humanize.ordinal(0, {zeroth: true}) +// "0th" +``` + ##### times Interprets numbers as occurences. Also accepts an optional array/map of overrides.