Created
September 26, 2021 22:04
-
-
Save nicksheffield/81cff2e6d57409a5ac185aed077e1f2b to your computer and use it in GitHub Desktop.
get positional string from number, ie: first, second, third, thirteenth, twentieth, twenty-first etc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const prefix = { | |
1: 'First', | |
2: 'Second', | |
3: 'Third', | |
4: 'Fourth', | |
5: 'Fifth', | |
6: 'Sixth', | |
7: 'Seventh', | |
8: 'Eighth', | |
9: 'Ninth', | |
10: 'Tenth', | |
11: 'Eleventh', | |
12: 'Twelth', | |
13: 'Thirteenth', | |
14: 'Fourteenth', | |
15: 'Fifteenth', | |
16: 'Sixteenth', | |
17: 'Seventeenth', | |
18: 'Eighteenth', | |
19: 'Nineteenth', | |
20: { | |
lone: 'Twentieth', | |
prefix: 'Twenty' | |
}, | |
30: { | |
lone: 'Thirtieth', | |
prefix: 'Thirty' | |
}, | |
40: { | |
lone: 'Fourtieth', | |
prefix: 'Fourty' | |
}, | |
} | |
const wordify = (n: number) => { | |
if (prefix[n] !== undefined) { | |
if (typeof prefix[n] === 'string') return prefix[n] | |
return prefix[n].lone | |
} | |
const step = (n: number, pref: number) => { | |
if (n > 10) { | |
return step(n - 10, pref + 10) | |
} | |
return prefix[pref].prefix + '-' + (prefix[n].toLowerCase()) | |
} | |
if (n >= 21) { | |
return step(n - 20, 20) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment