Набор полезных функций для Node JS и JavaScript

// Генерация случайного набора символов
StringRandom = function(len, base){
	if(len == undefined){
		len = 10;
	}
	if(base == undefined){
		base = 'abcdefghjkmnpqrstwxyz123456789';
	}
	var result = '';
	var max_position = base.length - 1;
	for(var i = 0; i < len; i++){
		var position = Math.floor(Math.random() * max_position);
		result += base.substring(position, position + 1);
	}
	return result;
}

// Случайное целое число от и до
GetRandomInt = function(min, max){
	return Math.floor(Math.random() * (max - min + 1)) + min;
}

// Склонение числа (число, рубль, рубля, рублей)
// Зависимости: IntVal();
DeclinationOfNumber = function(n, f1, f2, f5) {
	n = Math.abs(IntVal(n)) % 100;
	if(n > 10 && n < 20){
		return f5;
	}
	n = n % 10;
	if(n > 1 && n < 5){
		return f2;
	}
	if(n == 1){
		return f1;
	}
	return f5;
}

// Текст в транслит
GetTranslit = function(nameVal){
	enToRu = {
		"а":"a","б":"b","в":"v","г":"g","д":"d","е":"e","ё":"jo","ж":"zh","з":"z","и":"i","й":"j","к":"k","л":"l","м":"m","н":"n","о":"o","п":"p","р":"r","с":"s","т":"t","у":"u","ф":"f","х":"h","ц":"c","ч":"ch","ш":"sh","щ":"sch","ъ":"","ы":"y","ь":"","э":"e","ю":"ju","я":"ja"," ":" ","і":"i","ї":"i","є":"e","А":"A","Б":"B","В":"V","Г":"G","Д":"D","Е":"E","Ё":"Jo","Ж":"Zh","З":"Z","И":"I","Й":"J","К":"K","Л":"L","М":"M","Н":"N","О":"O","П":"P","Р":"R","С":"S","Т":"T","У":"U","Ф":"F","Х":"H","Ц":"C","Ч":"Ch","Ш":"Sh","Щ":"Sch","Ъ":"","Ы":"Y","Ь":"","Э":"E","Ю":"Ju","Я":"Ja","І":"I","Ї":"I","Є":"E","№":"n"
	},
	nameVal = nameVal.trim().split("");
	var string = new String(),
		len = nameVal.length;
	for(var i = 0; len > i; i++){
		if(enToRu[nameVal[i]] != undefined){
			string += enToRu[nameVal[i]];
		}else if(nameVal[i].match(/^[0-9a-zA-Z\-\ ]$/)){
			string += nameVal[i];
		}
	}
	string = string.trim();
	
	return string;
}

// Преобразовать значение в целое число
IntVal = function(mixedVar, base){
	var tmp;
	if(typeof(mixedVar) == 'string'){
		tmp = parseInt(mixedVar);
		if(isNaN(tmp)){
			return 0;
		}else{
			return tmp.toString(base || 10);
		}
	}else if(typeof(mixedVar) == 'number'){
		return Math.floor(mixedVar);
	}else{
		return 0;
	}
}

 

Функции для работы с куки

function CookieGet(name) {
	let matches = document.cookie.match(new RegExp("(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"));
	return matches ? decodeURIComponent(matches[1]) : undefined;
}

function CookieSet(name, value, options = {}) {
	options = {
		path: '/',
		// при необходимости можно добавить другие значения по умолчанию
		// expires - дата до которой действует куки (На один день: new Date(Date.now() + 86400e3).toUTCString())
		// max-age - сколько секунд будет действовать куки (На один час: 3600)
	};

	if (options.expires instanceof Date) {
		options.expires = options.expires.toUTCString();
	}

	let updatedCookie = encodeURIComponent(name) + "=" + encodeURIComponent(value);

	for (let optionKey in options) {
		updatedCookie += "; " + optionKey;
		let optionValue = options[optionKey];
		if (optionValue !== true) {
			updatedCookie += "=" + optionValue;
		}
	}

	document.cookie = updatedCookie;
}

function CookieDelete(name) {
	CookieSet(name, "", {
		'max-age': -1
	});
}