Skip to content

Commit 909a1bf

Browse files
committed
Fixing problem with negative ordinals.
This commit resolves #15
1 parent be6e25b commit 909a1bf

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

spec/toOrdinalSpec.js

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ var toOrdinal = typeof require !== 'undefined' ? require('../src/toOrdinal') : w
44

55
describe('toOrdinal', function () {
66
var tests = [
7+
{ input: -121, expect: '-121st' },
8+
{ input: -13, expect: '-13th' },
9+
{ input: -12, expect: '-12th' },
10+
{ input: -11, expect: '-11th' },
11+
{ input: -3, expect: '-3rd' },
12+
{ input: -2, expect: '-2nd' },
713
{ input: -1, expect: '-1st' },
814
{ input: 0, expect: '0th' },
915
{ input: 1, expect: '1st' },

src/toOrdinal.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function toOrdinal(number) {
1313
var num = parseInt(number, 10);
1414
if (!isFinite(num)) throw new TypeError('Not a finite number: ' + number + ' (' + typeof number + ')');
1515
var str = String(num);
16-
var lastTwoDigits = num % 100;
16+
var lastTwoDigits = Math.abs(num % 100);
1717
var betweenElevenAndThirteen = lastTwoDigits >= 11 && lastTwoDigits <= 13;
1818
var lastChar = str.charAt(str.length - 1);
1919
return str + (betweenElevenAndThirteen ? 'th'

0 commit comments

Comments
 (0)