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. diff --git a/__tests__/humanize.spec.js b/__tests__/humanize.spec.js index b49aee9..38b3720 100644 --- a/__tests__/humanize.spec.js +++ b/__tests__/humanize.spec.js @@ -45,10 +45,18 @@ 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)', () => { + it('should return 0 if the number is 0', () => { 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'); + }); + it('should return the number with suffix th', () => { expect(Humanize.ordinal(10)).toEqual('10th'); }); diff --git a/src/humanize.js b/src/humanize.js index afe4354..0bc91cf 100644 --- a/src/humanize.js +++ b/src/humanize.js @@ -217,10 +217,11 @@ }, // 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) { + if (number === 0 && !zeroth) { return value; }