-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwsevent.js
132 lines (115 loc) · 3.8 KB
/
wsevent.js
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"use strict";
const YAML = require('yaml');
const moment = require('moment');
const DATE_FORMAT = 'DDMMYYYYHHmm';
/**
* Класс, описывающий событие
*/
class WSEvent {
/**
* Конструктор WSEvent
* @constructor
* @param {string} name название события
* @param {string} city город события
* @param {string} link url на сайт о событии
* @param {date} start начало события
* @param {date} finish окончание события
* @param {boolean} isOnline онлайн ли событие
*/
constructor(name, city, link, start, finish, isOnline) {
this._name = name;
this._city = city;
this._link = link;
this._start = start;
this._finish = finish;
this._isOnline = isOnline;
if (this._finish <= this._start) {
throw new Error('Finish <= start');
}
}
/**
* Название события
* @returns {string} название события
*/
get name() {
return this._name;
}
/**
* Город события
* @returns {string} город события
*/
get city() {
return this._city;
}
/**
* Url на сайт о событии
* @returns {string} url на сайт о событии
*/
get link() {
return this._link;
}
/**
* Начало события
* @returns {date} начало события
*/
get start() {
return this._start;
}
/**
* Окончание события
* @returns {date} окончание события
*/
get finish() {
return this._finish;
}
/**
* Онлайн ли событие
* @returns {boolean} онлайн ли событие
*/
get isOnline() {
return this._isOnline;
}
/**
* Создание WSEvent из yaml-строки
* @static
* @param {string} yaml yaml строка с событием
* @returns {WSEvent} новое событие
*/
static fromYaml(yaml) {
const yamlData = YAML.parse(yaml);
const dateSplit = yamlData.date.split('-');
const timeSplit = (yamlData.time || '00:00 23:59').split(/[ -]/);
const timeFirst = timeSplit[0].split(':');
if (timeFirst.length == 2) {
if (timeFirst[0].length < 2) {
timeFirst[0] = `0${timeFirst[0]}`;
} else if (timeFirst[0].length > 2) {
timeFirst[0] = '00';
}
if (timeFirst[1].length < 2) {
timeFirst[1] = `0${timeFirst[0]}`;
} else if (timeFirst[1].length > 2) {
timeFirst[1] = '00';
}
timeSplit[0] = `${timeFirst[0]}${timeFirst[1]}`;
}
const timeSecond = timeSplit[0].split(':');
if (timeSecond.length == 2) {
if (timeSecond[0].length < 2) {
timeSecond[0] = `0${timeSecond[0]}`;
} else if (timeSecond[0].length > 2) {
timeSecond[0] = '23';
}
if (timeSecond[1].length < 2) {
timeSecond[1] = `0${timeSecond[0]}`;
} else if (timeSecond[1].length > 2) {
timeSecond[1] = '59';
}
timeSplit[1] = `${timeSecond[0]}${timeSecond[1]}`;
}
const start = moment.utc(`${dateSplit[0]} ${timeSplit[0] || '0000'}`.replace(/\D/g, ''), DATE_FORMAT).toDate();
const finish = moment.utc(`${dateSplit[1] || dateSplit[0]} ${timeSplit[1] || '2359'}`.replace(/\D/g, ''), DATE_FORMAT).utc().toDate();
return new WSEvent(yamlData.name, yamlData.city, yamlData.link, start, finish, yamlData.online || false);
}
}
module.exports = WSEvent;