beautify_js

This commit is contained in:
Ilya Kantor 2015-03-09 18:48:58 +03:00
parent 0febe4f5fd
commit 5c2f32e184
208 changed files with 3891 additions and 1474 deletions

View file

@ -1,5 +1,5 @@
function getWeekDay(date) {
var days = ['вс','пн','вт','ср','чт','пт','сб'] ;
var days = ['вс', 'пн', 'вт', 'ср', 'чт', 'пт', 'сб'];
return days[ date.getDay() ];
return days[date.getDay()];
}

View file

@ -1,29 +1,29 @@
describe("getWeekDay", function() {
it("3 января 2014 - пятница", function() {
assert.equal( getWeekDay(new Date(2014, 0, 3)), 'пт');
assert.equal(getWeekDay(new Date(2014, 0, 3)), 'пт');
});
it("4 января 2014 - суббота", function() {
assert.equal( getWeekDay(new Date(2014, 0, 4)), 'сб');
assert.equal(getWeekDay(new Date(2014, 0, 4)), 'сб');
});
it("5 января 2014 - воскресенье", function() {
assert.equal( getWeekDay(new Date(2014, 0, 5)), 'вс');
assert.equal(getWeekDay(new Date(2014, 0, 5)), 'вс');
});
it("6 января 2014 - понедельник", function() {
assert.equal( getWeekDay(new Date(2014, 0, 6)), 'пн');
assert.equal(getWeekDay(new Date(2014, 0, 6)), 'пн');
});
it("7 января 2014 - вторник", function() {
assert.equal( getWeekDay(new Date(2014, 0, 7)), 'вт');
assert.equal(getWeekDay(new Date(2014, 0, 7)), 'вт');
});
it("8 января 2014 - среда", function() {
assert.equal( getWeekDay(new Date(2014, 0, 8)), 'ср');
assert.equal(getWeekDay(new Date(2014, 0, 8)), 'ср');
});
it("9 января 2014 - четверг", function() {
assert.equal( getWeekDay(new Date(2014, 0, 9)), 'чт');
assert.equal(getWeekDay(new Date(2014, 0, 9)), 'чт');
});
});

View file

@ -2,9 +2,9 @@ function getLocalDay(date) {
var day = date.getDay();
if ( day == 0 ) { // день 0 становится 7
day = 7;
if (day == 0) { // день 0 становится 7
day = 7;
}
return day;
}
}

View file

@ -1,29 +1,29 @@
describe("getLocalDay возвращает день недели", function() {
it("3 января 2014 - пятница", function() {
assert.equal( getLocalDay(new Date(2014, 0, 3)), 5);
assert.equal(getLocalDay(new Date(2014, 0, 3)), 5);
});
it("4 января 2014 - суббота", function() {
assert.equal( getLocalDay(new Date(2014, 0, 4)), 6);
assert.equal(getLocalDay(new Date(2014, 0, 4)), 6);
});
it("5 января 2014 - воскресенье", function() {
assert.equal( getLocalDay(new Date(2014, 0, 5)), 7);
assert.equal(getLocalDay(new Date(2014, 0, 5)), 7);
});
it("6 января 2014 - понедельник", function() {
assert.equal( getLocalDay(new Date(2014, 0, 6)), 1);
assert.equal(getLocalDay(new Date(2014, 0, 6)), 1);
});
it("7 января 2014 - вторник", function() {
assert.equal( getLocalDay(new Date(2014, 0, 7)), 2);
assert.equal(getLocalDay(new Date(2014, 0, 7)), 2);
});
it("8 января 2014 - среда", function() {
assert.equal( getLocalDay(new Date(2014, 0, 8)), 3);
assert.equal(getLocalDay(new Date(2014, 0, 8)), 3);
});
it("9 января 2014 - четверг", function() {
assert.equal( getLocalDay(new Date(2014, 0, 9)), 4);
assert.equal(getLocalDay(new Date(2014, 0, 9)), 4);
});
});
});

View file

@ -1,6 +1,6 @@
function getDateAgo(date, days) {
var dateCopy = new Date(date);
dateCopy.setDate( date.getDate() - days );
return dateCopy.getDate();
dateCopy.setDate(date.getDate() - days);
return dateCopy.getDate();
}

View file

@ -1,20 +1,20 @@
describe("getDateAgo", function() {
it("1 день до 02.01.2015 -> число 1", function() {
assert.equal( getDateAgo(new Date(2015, 0, 2), 1), 1 );
assert.equal(getDateAgo(new Date(2015, 0, 2), 1), 1);
});
it("2 день до 02.01.2015 -> число 31", function() {
assert.equal( getDateAgo(new Date(2015, 0, 2), 2), 31 );
assert.equal(getDateAgo(new Date(2015, 0, 2), 2), 31);
});
it("100 дней от 02.01.2015 -> число 24", function() {
assert.equal( getDateAgo(new Date(2015, 0, 2), 100), 24 );
assert.equal(getDateAgo(new Date(2015, 0, 2), 100), 24);
});
it("365 дней от 02.01.2015 -> число 2", function() {
assert.equal( getDateAgo(new Date(2015, 0, 2), 365), 2 );
assert.equal(getDateAgo(new Date(2015, 0, 2), 365), 2);
});
it("не меняет переданный объект Date", function() {
@ -23,5 +23,5 @@ describe("getDateAgo", function() {
getDateAgo(dateCopy, 100);
assert.equal(date.getTime(), dateCopy.getTime());
});
});
});

View file

@ -1,4 +1,4 @@
function getLastDayOfMonth(year, month) {
var date = new Date(year, month+1, 0);
var date = new Date(year, month + 1, 0);
return date.getDate();
}
}

View file

@ -1,13 +1,13 @@
describe("getLastDayOfMonth", function() {
it("последний день 01.01.2012 - 31", function() {
assert.equal( getLastDayOfMonth(2012, 0), 31);
assert.equal(getLastDayOfMonth(2012, 0), 31);
});
it("последний день 01.02.2012 - 29 (високосный год)", function() {
assert.equal( getLastDayOfMonth(2012, 1), 29);
assert.equal(getLastDayOfMonth(2012, 1), 29);
});
it("последний день 01.02.2013 - 28", function() {
assert.equal( getLastDayOfMonth(2013, 1), 28);
assert.equal(getLastDayOfMonth(2013, 1), 28);
});
});

View file

@ -1,13 +1,13 @@
function formatDate(date) {
var dd = date.getDate();
if ( dd < 10 ) dd = '0' + dd;
if (dd < 10) dd = '0' + dd;
var mm = date.getMonth()+1;
if ( mm < 10 ) mm = '0' + mm;
var mm = date.getMonth() + 1;
if (mm < 10) mm = '0' + mm;
var yy = date.getFullYear() % 100;
if ( yy < 10 ) yy = '0' + yy;
if (yy < 10) yy = '0' + yy;
return dd+'.'+mm+'.'+yy;
}
return dd + '.' + mm + '.' + yy;
}

View file

@ -1,13 +1,13 @@
describe("formatDate", function() {
it("правильно форматирует дату 30.01.14", function() {
assert.equal( formatDate(new Date(2014, 0, 30)), '30.01.14');
assert.equal(formatDate(new Date(2014, 0, 30)), '30.01.14');
});
it("правильно форматирует дату 01.01.01", function() {
assert.equal( formatDate(new Date(2001, 0, 1)), '01.01.01');
assert.equal(formatDate(new Date(2001, 0, 1)), '01.01.01');
});
it("правильно форматирует дату 01.01.00", function() {
assert.equal( formatDate(new Date(2000, 0, 1)), '01.01.00');
assert.equal(formatDate(new Date(2000, 0, 1)), '01.01.00');
});
});
});

View file

@ -1,34 +1,34 @@
function formatDate(date) {
var diff = new Date() - date; // разница в миллисекундах
if (diff < 1000) { // прошло менее 1 секунды
return 'только что';
}
var sec = Math.floor( diff / 1000 ); // округлить diff до секунд
var sec = Math.floor(diff / 1000); // округлить diff до секунд
if (sec < 60) {
return sec + ' сек. назад';
}
var min = Math.floor( diff / 60000 ); // округлить diff до минут
var min = Math.floor(diff / 60000); // округлить diff до минут
if (min < 60) {
return min + ' мин. назад';
return min + ' мин. назад';
}
// форматировать дату, с учетом того, что месяцы начинаются с 0
var d = date;
d = [
'0'+d.getDate(),
'0'+(d.getMonth()+1),
''+d.getFullYear(),
'0'+d.getHours(),
'0'+d.getMinutes()
'0' + d.getDate(),
'0' + (d.getMonth() + 1),
'' + d.getFullYear(),
'0' + d.getHours(),
'0' + d.getMinutes()
];
for(var i=0; i<d.length; i++) {
d[i] = d[i].slice(-2);
for (var i = 0; i < d.length; i++) {
d[i] = d[i].slice(-2);
}
return d.slice(0,3).join('.')+' '+d.slice(3).join(':');
}
return d.slice(0, 3).join('.') + ' ' + d.slice(3).join(':');
}

View file

@ -1,18 +1,18 @@
describe("formatDate", function() {
it("выводит дату 1мс назад как \"только что\"", function() {
assert.equal( formatDate( new Date(new Date - 1) ), 'только что' );
assert.equal(formatDate(new Date(new Date - 1)), 'только что');
});
it('выводит дату "30 сек назад"', function() {
assert.equal( formatDate( new Date( new Date - 30*1000) ), "30 сек. назад" );
assert.equal(formatDate(new Date(new Date - 30 * 1000)), "30 сек. назад");
});
it('выводит дату "5 мин назад"', function() {
assert.equal( formatDate( new Date( new Date- 5*60*1000) ), "5 мин. назад");
assert.equal(formatDate(new Date(new Date - 5 * 60 * 1000)), "5 мин. назад");
});
it("выводит старую дату в формате дд.мм.гг чч:мм", function() {
assert.equal( formatDate( new Date(2014, 2, 1, 11, 22, 33) ), "01.03.14 11:22" );
assert.equal(formatDate(new Date(2014, 2, 1, 11, 22, 33)), "01.03.14 11:22");
});
});