Dia 63/2019 - AVA

Desafio #92daysofcode que fiz no final de 2019. Nesse artigo temos o dia 63, mostrando o uso do AVA.

Código

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
class DateFromNow {
constructor() {
this.second = 1000;
this.minute = 60 * this.second;
this.hour = 60 * this.minute;
this.day = 24 * this.hour;
this.week = 7 * this.day;
this.month = 31 * this.day;
this.year = 365 * this.day;
this.now = (new Date()).getTime();
};

countDays(date) {
return Math.floor(date / this.day);
}

messageFormat(date, numberDays){
let message;

switch(true) {
case (numberDays === 0):
switch(true) {
case (date < this.minute):
message = "Agora mesmo";
break;
case (date < (this.minute*2)):
message = "1 minuto atrás";
break;
case (date < this.hour):
message = Math.floor( date / this.minute ) + " minutos atrás";
break;
case (date < (this.hour*2)):
message = "1 hora atrás";
break;
case (date < this.day):
message = Math.floor( date / this.hour ) + " horas atrás";
break;
}
break;
case (numberDays === 1):
message = "Ontem";
break;
case (numberDays < 7):
message = numberDays + " dias atrás";
break;
case (numberDays < 31):
message = Math.ceil( numberDays / this.week ) + " semanas atrás";
break;
}

return message;
}

format(now, time){
let date = new Date(time || "");
let difference = ((new Date(now)).getTime() - date.getTime());
let differenceInDays = this.countDays(difference);

if ( isNaN(differenceInDays) || differenceInDays < 0 || differenceInDays >= 31 ) {
return;
}

return this.messageFormat(difference, differenceInDays);
}

// update(now) {
// var links = document.getElementsByTagName("a");
// for ( var i = 0; i < links.length; i++ ) {
// if ( links[i].title ) {
// var date = prettyDate.format(now, links[i].title);
// if ( date ) {
// links[i].innerHTML = date;
// }
// }
// }
// }
}

module.exports = DateFromNow;

// const p1 = new DateFromNow();
// console.log(p1.format( "2008/02/09 24:29:00", "2008/01/28 22:24:30"));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// AVA is a test runner for Node.js with a concise API, detailed error output, embrace of new language features and process isolation that let you write tests more effectively.
// https://github.com/avajs
import test from 'ava';
import DateFromNow from './date-from-now';

test.before(t => {
t.context.now = "2019/12/02 22:25:00";
t.context.date = "2019/12/01 22:25:00";
t.context.getDifference = (now, date) => ((new Date(now)).getTime() - new Date(date).getTime());
});

test.beforeEach(t => {
t.context.dateFromNow = new DateFromNow();
});

test("dateFromNow.countDays", t => {
const {dateFromNow, getDifference, now, date} = t.context;

const difference = getDifference(now, date);
t.is(dateFromNow.countDays(difference), 1);
});

test("dateFromNow.messageFormat", t => {
const {dateFromNow, getDifference, now, date} = t.context;

const difference = getDifference(now, date);
t.is(dateFromNow.messageFormat(difference, dateFromNow.countDays(difference)), "Ontem");
});

test("dateFromNow.format", t => {
const {dateFromNow, now} = t.context;

t.is(dateFromNow.format(now, "2019/12/02 22:24:30"), "Agora mesmo");
t.is(dateFromNow.format(now, "2019/12/02 22:23:30"), "1 minuto atrás");
t.is(dateFromNow.format(now, "2019/12/02 21:23:30"), "1 hora atrás");
t.is(dateFromNow.format(now, "2019/12/01 22:23:30"), "Ontem");
t.is(dateFromNow.format(now, "2019/11/30 22:23:30"), "2 dias atrás");
t.is(dateFromNow.format(now, "2018/12/01 22:23:30"), undefined);
});

Conclusão

A postagem original pode ser vista no meu Instagram e o código está acessível no meu Github.

Relacionados

Ao fechar este aviso ou continuar navegando no site Nerd Calistênico, você aceita o uso de cookies.

Este site usa cookies para assegurar a melhor experiência para os nossos usuários. Consulte nossa política de privacidade.

Uma nova versão está disponível. Clique aqui para atualizar.