Created
April 5, 2012 20:00
-
-
Save kara-ryli/2313656 to your computer and use it in GitHub Desktop.
Converts an integer into a its proper ordinal ("1st", "2nd", "3rd", "4th")
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
/*global YUI*/ | |
/** | |
* Converts an integer into a its proper ordinal ("1st", "2nd", "3rd", "4th") | |
* @module number-to-ord | |
*/ | |
YUI.add("number-to-ord", function (Y) { | |
"use strict"; | |
/** | |
* Converts an integer into a its proper ordinal ("1st", "2nd", "3rd", "4th") | |
* | |
* @method toOrd | |
* @param {Number|String} value The integer to convert | |
* @return {String} The ordinal value of the number. | |
* @static | |
* @namespace Lang | |
*/ | |
Y.Lang.toOrd = function (value) { | |
var s = String(value), | |
len = s.length, | |
end = s.substr(len - 1, 1), | |
teen = len > 1 && s.substr(len - 2, 1) === "1", | |
ord = "th"; | |
if (end === "1" && !teen) { | |
ord = "st"; | |
} else if (end === "2" && !teen) { | |
ord = "nd"; | |
} else if (end === "3" && !teen) { | |
ord = "rd"; | |
} | |
return ord; | |
}; | |
}, "3.4.1"); |
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
/*global YUI*/ | |
YUI({ logInclude: { TestRunner: true } }).use("number-to-ord", "console", "test", function (Y) { | |
"use strict"; | |
var yConsole = new Y.Console({ newestOnTop: false }); | |
Y.Test.Runner.add(new Y.Test.Case({ | |
name: "Y.Lang.toOrd Test", | |
"should convert numbers to ordinals": function () { | |
var i = 4; | |
Y.Assert.areEqual("st", Y.Lang.toOrd(1)); | |
Y.Assert.areEqual("nd", Y.Lang.toOrd(2)); | |
Y.Assert.areEqual("rd", Y.Lang.toOrd(3)); | |
do { | |
Y.Assert.areEqual("th", Y.Lang.toOrd(i)); | |
} while (++i < 10); | |
}, | |
"should handle teens correctly": function () { | |
var i = 11; | |
do { | |
Y.Assert.areEqual("th", Y.Lang.toOrd(i)); | |
} while (++i < 14); | |
}, | |
"should handle long numbers correctly": function () { | |
var start = 11013254340, | |
expected = ["th", "st", "nd", "rd", "th"], | |
i = 0, | |
l = expected.length; | |
for (i, l; i < l; i += 1) { | |
Y.Assert.areEqual(expected[i], Y.Lang.toOrd(start + i)); | |
} | |
} | |
})); | |
yConsole.render(); | |
Y.Test.Runner.run(); | |
}); |
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>Simple Number to Ordinal Converter</title> | |
</head> | |
<body class="yui3-skin-sam"> | |
<script src="http://yui.yahooapis.com/3.4.1/build/yui/yui-min.js"></script> | |
<script src="number-to-ord.js"></script> | |
<script src="number-to-ord-test.js"></script> | |
</body> | |
</html> |
Heyho,
i was looking for a small and easy way to get the ordinal of a number, i stumbled across yours and this seems a way to long for web front ends.
This method does exactly the same and is 300 bytes shorter:
function(v) { return ["th","st","nd","rd"][Math.abs(~[1,2,3].indexOf(+(+v).toFixed().substr(-1)))]; }
Greetings :)
but it's wrong for teen numbers
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Heyho,
i was looking for a small and easy way to get the ordinal of a number, i stumbled across yours and this seems a way to long for web front ends.
This method does exactly the same and is 300 bytes shorter:
function(v) { return ["th","st","nd","rd"][Math.abs(~[1,2,3].indexOf(+(+v).toFixed().substr(-1)))]; }
Greetings :)