-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate-array-photos.js
More file actions
75 lines (61 loc) · 1.95 KB
/
Copy pathcreate-array-photos.js
File metadata and controls
75 lines (61 loc) · 1.95 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
import { getRandomInteger, getRandomUniqueId, getRandomArrayElement } from './util.js';
import { getDataArrayPhotos } from './data.js';
const LIKES_RANGE = {
MIN: 15,
MAX: 200,
};
const COMMENT_SENTECES_RANGE = {
MIN: 1,
MAX: 2,
};
const AVATAR_RANGE = {
MIN: 1,
MAX: 6,
};
const PHOTO_ID_RANGE = {
MIN: 1,
MAX: 25,
};
const COMMENT_COUNT_RANGE = {
MIN: 0,
MAX: 30,
};
const PHOTO_ARRAY_SIZE = 25;
const { MESSAGES, NAMES, DESCRIPTIONS } = getDataArrayPhotos();
//Функция создания фабрики для генерации комментариев
const createCommentFactory = () => {
let commentIdCounter = 1;
//Возвращает массив случайных комментариев (1-2 комментария)
return () => {
const numSentences = getRandomInteger(
COMMENT_SENTECES_RANGE.MIN,
COMMENT_SENTECES_RANGE.MAX
);
const sentences = Array.from({length: numSentences}, () => getRandomArrayElement(MESSAGES));
//Возвращает объект - комментарий
return {
id: commentIdCounter++,
avatar: `img/avatar-${getRandomInteger(AVATAR_RANGE.MIN, AVATAR_RANGE.MAX)}.svg`,
message: sentences.join(' '),
name: getRandomArrayElement(NAMES)
};
};
};
const createComment = createCommentFactory();
const createPhotoId = getRandomUniqueId(PHOTO_ID_RANGE.MIN, PHOTO_ID_RANGE.MAX);
const createPhotoDescription = () => {
const photoId = createPhotoId();
return {
id: photoId,
url: `photos/${photoId}.jpg`,
description: getRandomArrayElement(DESCRIPTIONS),
likes: getRandomInteger(LIKES_RANGE.MIN, LIKES_RANGE.MAX),
comments: Array.from(
{ length: getRandomInteger(COMMENT_COUNT_RANGE.MIN, COMMENT_COUNT_RANGE.MAX) },
createComment
),
};
};
//Функция создания массива из 25 объектов
const photos = Array.from({ length: PHOTO_ARRAY_SIZE }, createPhotoDescription);
export { photos };