Skip to content

Commit a751276

Browse files
author
Юлия Перелыгина
committed
делит код на модули
1 parent a5ee1ec commit a751276

5 files changed

Lines changed: 94 additions & 92 deletions

File tree

index.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
<link rel="stylesheet" href="css/style.css">
99
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
1010
<title>Кекстаграм</title>
11-
<script src="js/main.js" defer></script>
1211
</head>
1312

1413
<body>
@@ -236,6 +235,9 @@ <h2 class="data-error__title">Не удалось загрузить данны
236235
</section>
237236
</template>
238237

238+
<script type="module" src="js/main.js"></script>
239+
<script type="module" src="js/functions.js"></script>
240+
239241
</body>
240242

241243
</html>

js/createArrayMiniatures.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {
2+
getRandomInteger,
3+
getRandomArrayElement,
4+
createRandomIdFromRangeGenerator,
5+
} from './util.js';
6+
import { NAMES, COMMENTS, DESCRIPTIONS } from './data.js';
7+
8+
const OBJECT_COUNT = 25;
9+
10+
const getPhotoID = createRandomIdFromRangeGenerator(1, 26);
11+
const generateCommentId = createRandomIdFromRangeGenerator(1, 26);
12+
const getNumberPhoto = createRandomIdFromRangeGenerator(1, 26);
13+
14+
const createComments = () => ({
15+
id: generateCommentId(),
16+
avatar: `img/avatar-${getRandomInteger(1, 6)}.svg`,
17+
message: getRandomArrayElement(COMMENTS),
18+
name: getRandomArrayElement(NAMES),
19+
});
20+
21+
const createPhotoDescription = () => ({
22+
id: getPhotoID(),
23+
url: `photos/${getNumberPhoto()}.jpg`,
24+
description: getRandomArrayElement(DESCRIPTIONS),
25+
likes: getRandomInteger(15, 200),
26+
comments: Array.from({ length: getRandomInteger(0, 30) }, createComments)
27+
});
28+
29+
const getArrayMiniatures = () =>
30+
Array.from({ length: OBJECT_COUNT }, createPhotoDescription);
31+
32+
export { getArrayMiniatures };

js/data.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const NAMES = ['Артём', 'Мария', 'Иван', 'Анна', 'Дмитрий', 'Елена', 'Сергей', 'Ольга'];
2+
3+
const COMMENTS = [
4+
'Всё отлично!',
5+
'В целом всё неплохо. Но не всё.',
6+
'Когда вы делаете фотографию, хорошо бы убирать палец из кадра. В конце концов это просто непрофессионально.',
7+
'Моя бабушка случайно чихнула с фотоаппаратом в руках и у неё получилась фотография лучше.',
8+
'Я поскользнулся на банановой кожуре и уронил фотоаппарат на кота и у меня получилась фотография лучше.',
9+
'Лица у людей на фотке перекошены, как будто их избивают. Как можно было поймать такой неудачный момент?!',
10+
];
11+
12+
13+
const DESCRIPTIONS = [
14+
'Отличный день!',
15+
'Прекрасный вид',
16+
'Незабываемый момент',
17+
'Лучший отпуск',
18+
'Море и солнце',
19+
'С друзьями весело',
20+
'Природа прекрасна',
21+
'Городские джунгли',
22+
];
23+
24+
25+
export { NAMES, COMMENTS, DESCRIPTIONS };

js/main.js

Lines changed: 2 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,4 @@
11
/* eslint-disable no-console */
22

3-
const getRandomInteger = (min, max) => {
4-
const lower = Math.ceil(Math.min(min, max));
5-
const upper = Math.floor(Math.max(min, max));
6-
return Math.floor(Math.random() * (upper - lower + 1) + lower);
7-
};
8-
9-
const MESSAGES = [
10-
'Всё отлично!',
11-
'В целом всё неплохо. Но не всё.',
12-
'Когда вы делаете фотографию, хорошо бы убирать палец из кадра. В конце концов это просто непрофессионально.',
13-
'Моя бабушка случайно чихнула с фотоаппаратом в руках и у неё получилась фотография лучше.',
14-
'Я поскользнулся на банановой кожуре и уронил фотоаппарат на кота и у меня получилась фотография лучше.',
15-
'Лица у людей на фотке перекошены, как будто их избивают. Как можно было поймать такой неудачный момент?!',
16-
];
17-
18-
const NAMES = ['Артём', 'Мария', 'Иван', 'Анна', 'Дмитрий', 'Елена', 'Сергей', 'Ольга'];
19-
20-
const DESCRIPTIONS = [
21-
'Отличный день!',
22-
'Прекрасный вид',
23-
'Незабываемый момент',
24-
'Лучший отпуск',
25-
'Море и солнце',
26-
'С друзьями весело',
27-
'Природа прекрасна',
28-
'Городские джунгли',
29-
];
30-
31-
const PHOTOS_COUNT = 25;
32-
const MIN_LIKES = 15;
33-
const MAX_LIKES = 200;
34-
const MIN_COMMENTS = 0;
35-
const MAX_COMMENTS = 30;
36-
const AVATAR_COUNT = 6;
37-
38-
let commentId = 0;
39-
40-
const createMessage = () => {
41-
const sentenceCount = getRandomInteger(1, 2);
42-
const messages = [];
43-
44-
for (let i = 0; i < sentenceCount; i++) {
45-
messages.push(MESSAGES[getRandomInteger(0, MESSAGES.length - 1)]);
46-
}
47-
48-
return messages.join(' ');
49-
};
50-
51-
const createComment = () => {
52-
commentId++;
53-
return {
54-
id: commentId,
55-
avatar: `img/avatar-${getRandomInteger(1, AVATAR_COUNT)}.svg`,
56-
message: createMessage(),
57-
name: NAMES[getRandomInteger(0, NAMES.length - 1)],
58-
};
59-
};
60-
61-
const createComments = () => {
62-
const commentsCount = getRandomInteger(MIN_COMMENTS, MAX_COMMENTS);
63-
const comments = [];
64-
65-
for (let i = 0; i < commentsCount; i++) {
66-
comments.push(createComment());
67-
}
68-
69-
return comments;
70-
};
71-
72-
const createPhoto = (index) => ({
73-
id: index,
74-
url: `photos/${index}.jpg`,
75-
description: DESCRIPTIONS[getRandomInteger(0, DESCRIPTIONS.length - 1)],
76-
likes: getRandomInteger(MIN_LIKES, MAX_LIKES),
77-
comments: createComments(),
78-
});
79-
80-
const createPhotos = () => {
81-
const result = [];
82-
83-
for (let i = 1; i <= PHOTOS_COUNT; i++) {
84-
result.push(createPhoto(i));
85-
}
86-
87-
return result;
88-
};
89-
90-
const photos = createPhotos();
91-
92-
93-
console.log(photos);
3+
import { getArrayMiniatures } from './createArrayMiniatures.js';
4+
console.log(getArrayMiniatures());

js/util.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const getRandomInteger = (a, b) => {
2+
const lower = Math.ceil(Math.min(a, b));
3+
const upper = Math.floor(Math.max(a, b));
4+
const result = Math.random() * (upper - lower + 1) + lower;
5+
return Math.floor(result);
6+
};
7+
8+
const createRandomIdFromRangeGenerator = (min, max) => {
9+
const previousValues = [];
10+
11+
return function () {
12+
let currentValue = getRandomInteger(min, max);
13+
if (previousValues.length >= max - min + 1) {
14+
return null;
15+
}
16+
while (previousValues.includes(currentValue)) {
17+
currentValue = getRandomInteger(min, max);
18+
}
19+
previousValues.push(currentValue);
20+
return currentValue;
21+
};
22+
};
23+
24+
const getRandomArrayElement = (elements) =>
25+
elements[getRandomInteger(0, elements.length - 1)];
26+
27+
export {
28+
getRandomInteger,
29+
getRandomArrayElement,
30+
createRandomIdFromRangeGenerator,
31+
};
32+

0 commit comments

Comments
 (0)