Last active
August 29, 2015 14:15
-
-
Save pmn4/1c977e32b39f5bba7432 to your computer and use it in GitHub Desktop.
AngularJS Ordinal Filter
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
| // convert numbers to ordinal versions of themselves | |
| // {{ 1 | ordinal }} : "1st" | |
| // {{ 2 | ordinal }} : "2nd" | |
| // {{ 3 | ordinal }} : "3rd" | |
| // etc. | |
| angular | |
| .module("app", []) | |
| .filter("ordinal", function () { | |
| var suffix = ["th", "st", "nd", "rd"]; | |
| return function (cardinal) { | |
| var v = cardinal % 100; | |
| // suffix[(v - 20) % 10] | |
| // the teens are weird, so we ignore everything less than 20 | |
| // for numbers >= 20, we look at the one's digit | |
| // suffix[v] | |
| // this is just 0 - 3. numbers > 3 will be null | |
| // suffix[0] | |
| // 3 < n < 20 | |
| return cardinal + (suffix[(v - 20) % 10] || suffix[v] || suffix[0]); | |
| }; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment