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
83
84
85
86
87
88
89
90
91
92
|
/*
*/
var moment = require('moment');
var Parser = require('../parser').Parser;
var ParsedResult = require('../../result').ParsedResult;
var PATTERN = /(\W|^)hace\s*([0-9]+|medi[oa]|una?)\s*(minutos?|horas?|semanas?|d[ií]as?|mes(es)?|años?)(?=(?:\W|$))/i;
exports.Parser = function ESTimeAgoFormatParser(){
Parser.apply(this, arguments);
this.pattern = function() {
return PATTERN;
}
this.extract = function(text, ref, match, opt){
if (match.index > 0 && text[match.index-1].match(/\w/)) return null;
var text = match[0];
text = match[0].substr(match[1].length, match[0].length - match[1].length);
index = match.index + match[1].length;
var result = new ParsedResult({
index: index,
text: text,
ref: ref,
});
var num = parseInt(match[2]);
if (isNaN(num)) {
if (match[2].match(/medi/)) {
num = 0.5;
} else {
num = 1;
}
}
var date = moment(ref);
if (match[3].match(/hora/) || match[3].match(/minuto/)) {
if (match[3].match(/hora/)) {
date.add(-num, 'hour');
} else if (match[3].match(/minuto/)) {
date.add(-num, 'minute');
}
result.start.imply('day', date.date());
result.start.imply('month', date.month() + 1);
result.start.imply('year', date.year());
result.start.assign('hour', date.hour());
result.start.assign('minute', date.minute());
result.tags['ESTimeAgoFormatParser'] = true;
return result;
}
if (match[3].match(/semana/)) {
date.add(-num, 'week');
result.start.imply('day', date.date());
result.start.imply('month', date.month() + 1);
result.start.imply('year', date.year());
result.start.imply('weekday', date.day());
return result;
}
if (match[3].match(/d[ií]a/)) {
date.add(-num, 'd');
}
if (match[3].match(/mes/)) {
date.add(-num, 'month');
}
if (match[3].match(/año/)) {
date.add(-num, 'year');
}
result.start.assign('day', date.date());
result.start.assign('month', date.month() + 1);
result.start.assign('year', date.year());
return result;
};
}
|