Skip to content

Commit

Permalink
Fixing problem with negative ordinals.
Browse files Browse the repository at this point in the history
This commit resolves #15
  • Loading branch information
marlun78 committed Aug 9, 2018
1 parent be6e25b commit 909a1bf
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
6 changes: 6 additions & 0 deletions spec/toOrdinalSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ var toOrdinal = typeof require !== 'undefined' ? require('../src/toOrdinal') : w

describe('toOrdinal', function () {
var tests = [
{ input: -121, expect: '-121st' },
{ input: -13, expect: '-13th' },
{ input: -12, expect: '-12th' },
{ input: -11, expect: '-11th' },
{ input: -3, expect: '-3rd' },
{ input: -2, expect: '-2nd' },
{ input: -1, expect: '-1st' },
{ input: 0, expect: '0th' },
{ input: 1, expect: '1st' },
Expand Down
2 changes: 1 addition & 1 deletion src/toOrdinal.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function toOrdinal(number) {
var num = parseInt(number, 10);
if (!isFinite(num)) throw new TypeError('Not a finite number: ' + number + ' (' + typeof number + ')');
var str = String(num);
var lastTwoDigits = num % 100;
var lastTwoDigits = Math.abs(num % 100);
var betweenElevenAndThirteen = lastTwoDigits >= 11 && lastTwoDigits <= 13;
var lastChar = str.charAt(str.length - 1);
return str + (betweenElevenAndThirteen ? 'th'
Expand Down

0 comments on commit 909a1bf

Please sign in to comment.