Skip to content
This repository was archived by the owner on Jun 14, 2024. It is now read-only.

Commit e4ada28

Browse files
committed
Adding ith helper.
1 parent 178f6fe commit e4ada28

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

ith.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
/**
3+
* Returns ordinal number as `1st`, `2nd` etc. string.
4+
*
5+
* @param {number} value
6+
* @return {string}
7+
*/
8+
function ith(value) {
9+
const ones = value % 10
10+
const tens = Math.floor(value / 10) % 10
11+
if (tens === 1) {
12+
return `${value}th`
13+
}
14+
switch (ones) {
15+
case 1: return `${value}st`
16+
case 2: return `${value}nd`
17+
case 3: return `${value}rd`
18+
}
19+
return `${value}th`
20+
}
21+
22+
module.exports = ith

0 commit comments

Comments
 (0)