-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsIntro.js
More file actions
378 lines (353 loc) · 9.19 KB
/
Copy pathjsIntro.js
File metadata and controls
378 lines (353 loc) · 9.19 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
const myModule = (function () {
let messages = [{
id: '1',
text: 'Привет!',
createdAt: new Date('2020-10-12T23:00:00'),
author: 'Александр Купченя',
isPersonal: true,
to: 'Дарья Шурова'
},
{
id: '2',
text: 'Как Дела?',
createdAt: new Date('2020-10-12T23:01:00'),
author: 'Александр Купченя',
isPersonal: true,
to: 'Дарья Шурова'
},
{
id: '3',
text: 'привет)',
createdAt: new Date('2020-10-12T23:00:00'),
author: 'Дарья Шурова',
isPersonal: true,
to: 'Купченя Александр'
},
{
id: '4',
text: 'пока не родила))',
createdAt: new Date('2020-10-12T23:01:10'),
author: 'Дарья Шурова',
isPersonal: true,
to: 'Александр Купченя'
},
{
id: '5',
text: 'Декан на лекции!',
createdAt: new Date('2020-10-12T23:00:00'),
author: 'Владислав Мацкевич',
isPersonal: false,
to: false
},
{
id: '6',
text: 'Декан пришел, палундра( Бегом на пару',
createdAt: new Date('2020-10-12T12:30:02'),
author: 'Александр Купченя',
isPersonal: true,
to: 'Дарья Шурова'
},
{
id: '7',
text: '5 минут!',
createdAt: new Date('2020-10-12T12:35:00'),
author: 'Дарья Шурова',
isPersonal: false,
to: 'Александр Купченя'
},
{
id: '8',
text: 'А я за шавой!',
createdAt: new Date('2020-10-12T13:00:00'),
author: 'Антон Лях',
isPersonal: false,
to: false
},
{
id: '9',
text: 'Приятного аппетита)',
createdAt: new Date('2020-10-12T13:05:00'),
author: 'Дарья Шурова',
isPersonal: false,
to: false
},
{
id: '10',
text: 'Спасибо!',
createdAt: new Date('2020-10-12T13:11:00'),
author: 'Антон Лях',
isPersonal: false,
to: false
},
{
id: '11',
text: 'Aliquam blandit enim ut nibh imperdiet, in dignissim tortor lobortis!',
createdAt: new Date('2020-10-12T14:11:00'),
author: 'Jonh Snow',
isPersonal: true,
to: 'Daenerys Targaryen'
},
{
id: '12',
text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque rhoncus urna blandit est eleifend, non egestas magna dictum.',
createdAt: new Date('2020-10-12T15:11:00'),
author: 'Daenerys Targaryen',
isPersonal: true,
to: 'Jonh Snow'
},
{
id: '13',
text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque rhoncus urna blandit est eleifend, non egestas magna dictum.',
createdAt: new Date('2020-10-12T15:12:00'),
author: 'Daenerys Targaryen',
isPersonal: true,
to: 'Jonh Snow'
},
{
id: '14',
text: 'Aliquam blandit enim ut nibh imperdiet, in dignissim tortor lobortis!',
createdAt: new Date('2020-10-12T10:11:00'),
author: 'Jonh Snow',
isPersonal: true,
to: 'Daenerys Targaryen'
},
{
id: '15',
text: 'Aliquam blandit enim ut nibh imperdiet, in dignissim tortor lobortis! Yohoo',
createdAt: new Date('2020-10-12T09:11:00'),
author: 'Jonh Snow',
isPersonal: false,
to: false
},
{
id: '16',
text: 'Aliquam blandit enim ut nibh imperdiet, in dignissim tortor lobortis!',
createdAt: new Date('2020-10-12T08:11:00'),
author: 'Jonh Snow',
isPersonal: true,
to: 'Daenerys Targaryen'
},
{
id: '17',
text: 'Aliquam blandit enim ut nibh imperdiet, in dignissim tortor lobortis!',
createdAt: new Date('2020-10-12T19:11:00'),
author: 'Jonh Snow',
isPersonal: false,
to: false
},
{
id: '18',
text: 'Aliquam blandit enim ut nibh imperdiet, in dignissim tortor lobortis!',
createdAt: new Date('2020-10-12T07:11:00'),
author: 'Jonh Snow',
isPersonal: true,
to: 'Daenerys Targaryen'
},
{
id: '20',
text: 'Aliquam blandit enim ut nibh imperdiet, in dignissim tortor lobortis!',
createdAt: new Date('2020-10-12T19:12:00'),
author: 'Jonh Snow',
isPersonal: false,
to: false
},
{
id: '19',
text: 'Aliquam blandit enim ut nibh imperdiet, in dignissim tortor lobortis!',
createdAt: new Date('2020-10-12T19:11:00'),
author: 'Jonh Snow',
isPersonal: false,
to: false
}
];
const my = 'Александр Купченя';
function validateMessage(msg) {
let flag = true;
if (msg.hasOwnProperty('text')) {
if (typeof msg.text !== 'string' || (msg.text.length == 0 || msg.text.length > 200)) {
flag = false;
console.log('text is Invalid');
}
} else {
flag = false;
console.log('Object does not have all reguaired fields');
}
if (msg.hasOwnProperty('isPersonal')) {
if (typeof msg.isPersonal !== 'boolean') {
flag = false;
console.log('Invalid isPersonal property!');
}
if (msg.isPersonal && !(msg.hasOwnProperty('to') && typeof msg.to === 'string' && msg.to.length > 0)) {
flag = false;
} else if (!msg.isPersonal) {
msg.to = msg.isPersonal;
}
}
return flag;
}
function addMessage(msg) {
if (validateMessage(msg)) {
msg.id = `${+new Date()}`;
msg.createdAt = new Date();
msg.author = my;
messages.push(msg);
console.log('message add succsesfull');
return true;
}
console.log('Message do not add. Message structure is invalid!');
return false;
}
function removeMessage(id) {
messages.splice(
messages.findIndex(item => item.id === id), 1
);
return messages;
}
function editMessage(id, msg) {
let editedItem = messages.find(item => {
return item.id === id;
});
if (validateMessage(msg)) {
for (let key in msg) {
editedItem[key] = msg[key];
}
return editedItem;
} return false;
}
function getMessage(id) {
return messages.find(item => {
return item.id === id;
}); // .text??
}
function getMessages(skip = 0, top = 10, filterConfig = {}) {
let messagesFiltered = messages.slice().sort((a, b) => {
return a.createdAt - b.createdAt;
});
for (let key in filterConfig) {
if (key === 'author') {
messagesFiltered = messagesFiltered.filter((item) => {
return item.author.toLowerCase().includes(filterConfig[key].toLowerCase());
});
}
if (key === 'text') {
messagesFiltered = messagesFiltered.filter((item) => {
return item.text.toLowerCase().includes(filterConfig[key].toLowerCase());
});
}
if (key === 'dateFrom') {
messagesFiltered = messagesFiltered.filter((item) => {
return item.createdAt > filterConfig[key];
});
}
if (key === 'dateTo') {
messagesFiltered = messagesFiltered.filter((item) => {
return item.createdAt < filterConfig[key];
});
}
}
return messagesFiltered.slice(skip, skip + top);
}
return {
addMessage,
removeMessage,
editMessage,
validateMessage,
getMessage,
getMessages
};
}());
// console.log(myModule.getMessages(0,10)); //done
// console.log(myModule.getMessages(10,10)); //done
// console.log(myModule.getMessages({
// author: 'snow',
// dateTo: new Date('2020-10-12T15:11:00'),
// }, 0, 10, )); //done
// console.log(myModule.getMessages({
// author: 'snow',
// dateTo: new Date('2020-10-12T15:11:00'),
// text: 'yohoo'
// }, 0, 10, )); //done
// console.log(myModule.getMessage('5')); //done
// console.log(myModule.removeMessage('3')); //done
// console.log(myModule.validateMessage({
// text: 'f',
// isPersonal: true,
// to: '3',
// })); //done
console.log(myModule.addMessage(
{
text: 'Работает!',
isPersonal: true,
to: 'Дарья Шурова'
}
)); // done
// console.log(myModule.addMessage(
// {
// text: 'hy',
// isPersonal: true,
// to: 'Дарья Шурова',
// }
// )); //done
console.log(myModule.editMessage('20', {
text: '',
isPersonal: true,
to: 'my'
})); // done
// console.log(myModule.editMessage('20', {
// text: 'e',
// isPersonal: true,
// })); //done
// myModule.addMessage({
// text:'may be are you ready'
// });//done
// myModule.getMessages(0,10,{
// author:'купченя'
// });//done
function add(a, b) {
if (b) return a + b;
return (b2) => {
return a + b2;
};
}
function sub(a, b) {
if (b) return a - b;
return (b2) => {
return b2 - a;
};
}
function mul(a, b) {
if (b) return a * b;
return (b2) => {
return a * b2;
};
}
function div(a, b) {
if (b) return a / b;
return (b2) => {
return a / b2;
};
}
let a = add(1, 2); // 3
console.log(a);
let b = mul(a, 10); // 30
console.log(b);
let sub1 = sub(1); // sub1 отнимает от любого числа единицу
let c = sub1(b); // 29
console.log(c);
let d = mul(sub(a, 2))(c); // 58
console.log(d);
// function pipe(...args){
// return (f)=>{
// return div(());
// }
// }
// let d = add(2);
// console.log(d(add(1, 2)));
// console.log('d',d);
// let a = add(1, 2);
// let b = mul(a, 10);
// let sub1 = sub(1);
// let c = sub1(b);
// console.log(a);
// console.log(b);
// console.log(c);