Skip to content

Instantly share code, notes, and snippets.

@zmnv
Last active December 29, 2018 16:32
Show Gist options
  • Save zmnv/24b27fd1156b58be8bf5265e1f38e53a to your computer and use it in GitHub Desktop.
Save zmnv/24b27fd1156b58be8bf5265e1f38e53a to your computer and use it in GitHub Desktop.
Cute Numbers with spaces. Cyrillic labels by last number.
function cuteNumber(num) {
num = ''+num;
return num.replace(/(\d)(?=(\d\d\d)+([^\d]|$))/g, '$1 ');;
}
function cuteNumberObjects(num, replacer = ['объект', 'объекта', 'объектов']) {
num = cuteNumber(num);
switch(num[num.length - 1]) {
case '1':
return `${num} ${replacer[0]}`;
case '2' || '3' || '4':
return `${num} ${replacer[1]}`;
case '0' || '5' || '6' || '7' || '8' || '9':
return `${num} ${replacer[2]}`;
}
}
console.log(cuteNumberObjects(5421, ['картинка', 'картинки', 'картинок']));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment