Skip to content

Instantly share code, notes, and snippets.

@sapegin
Created June 7, 2011 13:27
Show Gist options
  • Select an option

  • Save sapegin/1012250 to your computer and use it in GitHub Desktop.

Select an option

Save sapegin/1012250 to your computer and use it in GitHub Desktop.
JavaScript Cookbook
function formatNumber(value) {
value = value.toString()
.replace(".", ",")
.replace(" ", "")
.replace("-", "−");
var re = /(\d+)(\d{3})/;
while (re.test(value)) {
value = value.replace(re, "<i>$1</i>$2");
}
return value;
}
function getText(node) {
return node.textContent || node.innerText;
}
// ['минута', 'минуты', 'минут'][plural(minutes)]
function plural(n) {
return (n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);
}
if (!String.prototype.trim) {
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g, '');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment