Created
October 24, 2011 10:39
-
-
Save lsauer/1308753 to your computer and use it in GitHub Desktop.
Simple english language ordinal for an integer e.g. 1st, 2nd, 12th, 25th - 'map-reduce logic'
This file contains 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
// author: lo sauer 2011 - lsauer.com; see also 'furls' concise version: https://gist.github.com/986113 | |
// description: solution with a 'map-reduce' mindset | |
// note: JS converts string-numbers on the fly e.g. "7"&3 >3; this comes in handy | |
// A similar and more concise solutions could be implemented: | |
// a=(a+'').substr(-2); | |
// included in https://github.com/lsauer/is-library | |
function (a){a=(a|0+'').split('').slice(-2).reduce(function(a,b){return 1==a?0:(b>3 ? 0 : b); });return ['th','st','nd','rd'][a]; }; | |
// Annotated & expanded | |
// Note: for 'teens case' (e.g. 12th) we cannot apply a straightforwards bit cutoff-range such as &range|start as the ORed 'start' mask would be 0 or 1 | |
var strord = function (a,i18n){ | |
//summary: cast to string, map (e.g. atomize) and reduce | |
a=(a|0+'') //cast to string | |
.split('') //atomize | |
.slice(-2) //get the last two elements | |
.reduce( //reduce arithmetically to a single value | |
function(a,b){ | |
if(1==a) return 0; //teens case -> return 0 (#el containing 'th') | |
else return b>3 ? 0 : b; //if b>3 return 0 -> 'th' | |
} | |
); | |
var oi18n = {'en': ['th','st','nd','rd']} | |
i18n=i18n||'en'; | |
return oi18n[i18n][a]; //map number to a string | |
}; | |
ord(112) | |
>"th" | |
ord(3022) | |
>"nd" | |
//Here is a regex based, albeit slower version by tsaniel (github) | |
function(a){return[,"st","nd","rd"][/1?.$/.exec(a)]||"th"} |
This file contains 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
Dual licensed: BSD or Your Driver's License | |
1.<INSERT YOUR DRIVER'S LICENSE + MUGSHOT HERE> | |
2.<BSD LICENSE> | |
Copyright (c) 2011 lo sauer. | |
All rights reserved. | |
Redistribution and use in source and binary forms are permitted | |
provided that the above copyright notice and this paragraph are | |
duplicated in all such forms and that any documentation, | |
advertising materials, and other materials related to such | |
distribution and use acknowledge that the software was developed | |
by the <organization>. The name of the | |
University may not be used to endorse or promote products derived | |
from this software without specific prior written permission. | |
THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
This file contains 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
function (a){a=(a|0+'').split('').slice(-2).reduce(function(a,b){return 1==a?0:(b>3 ? 0 : b);});return ['th','st','nd','rd'][a];}; |
This file contains 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
{ | |
"name": "strordinal", | |
"description": "Return the ordinal suffix for a number. In case of a float the number is floored.", | |
"keywords": ["ordinal","i18n","suffix","conversion","th","st","nd","rd" ] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment