Last active
July 31, 2016 05:18
-
-
Save Piterden/457cb1c182b700c3323f04ee18bc1578 to your computer and use it in GitHub Desktop.
[ES6] Helper functions
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
/** | |
* Добавляет элемент в начало массива, сдвигая все индексы. | |
* Возвращает сам массив, а не новую длинну, как в unshift() | |
* @param {Mixed} val Значение нового элемента | |
* @return {Array} | |
*/ | |
Array.prototype.addBefore = function(val) { | |
this.unshift(val); | |
return this; | |
}; |
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
/** | |
* Форматирует дату в строку даты | |
* @param {Date} time Объект даты | |
* @param {String} format Маска формата | |
* @return {String} | |
*/ | |
formatDateToStr(time = new Date(), format = 'YYYY-MM-DD') { | |
let year = time.getFullYear(), | |
month = time.getMonth() + 1, | |
date = time.getDate(), | |
monthName = this.getMonthNames()[time.getMonth()], | |
map = { | |
YYYY: year, | |
YY: String(year).slice(2), | |
MMM: monthName, | |
MM: ('0' + month).slice(-2), | |
M: month, | |
DD: ('0' + date).slice(-2), | |
D: date | |
} | |
return format.replace(/Y+|M+|D+/g, (str) => { | |
return map[str] | |
}) | |
}, |
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
/** | |
* Форматирует дату в строку времени | |
* @param {Date} time Объект даты | |
* @param {String} format Маска формата | |
* @return {String} | |
*/ | |
timeStrFromDateObj(time = new Date(), format = 'hh:mm') { | |
let hour = time.getHours(), | |
minute = time.getMinutes(), | |
second = time.getSeconds(), | |
map = { | |
hh: ('0' + hour).slice(-2), | |
h: hour, | |
mm: ('0' + minute).slice(-2), | |
m: minute, | |
ss: ('0' + second).slice(-2), | |
s: second | |
} | |
return format.replace(/h+|m+|s+/g, (str) => { | |
return map[str] | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment