-
Notifications
You must be signed in to change notification settings - Fork 2
/
ordinal.js
40 lines (29 loc) · 1.08 KB
/
ordinal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"use strict";
//Array to hold on to ordinals
const ordinalsArray = ["th", "st", "nd", "rd"];
// Function will check if number is valid and return false if not.
const validateNumber = (num) => {
return !isNaN(parseFloat( num )) && isFinite( num );
};
const ordinal = {
ordinalSuffix (num) {
//Get reminder of number by hundred so that we can counter number between 11-19
let offset = num % 100;
// Calculate position of ordinal to be used. Logic : Array index is calculated based on defined values.
let ordinalPos = ordinalsArray[ ( offset-20 )%10 ] || ordinalsArray[ offset ] || ordinalsArray[0];
//Return suffix
return ordinalPos;
},
toOrdinal (num) {
//Check if number is valid
if( !validateNumber(num) ) {
return `${num} is not a valid number`;
}
//If number is zero no need to spend time on calculation
if(num === 0) {
return num.toString();
}
return num.toString() + this.ordinalSuffix(num);
}
};
module.exports = ordinal;