-
Notifications
You must be signed in to change notification settings - Fork 1
Модуляция #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Модуляция #4
Changes from 8 commits
76b764f
9cca2df
8e78448
38270e6
eb5aac8
eaf382b
41a2150
4e4ddb6
7f343c2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /* eslint-disable no-console */ | ||
| import { MESSAGES_ARRAY, DESCRIPTION_ARRAY, NAMES } from './data.js'; | ||
| import { getRandomArrayElement, createRandomRangeInteger, createIdGenerator } from './util.js'; | ||
| const generateCommentId = createIdGenerator(1, 100000); | ||
| const generatePhotoId = createIdGenerator(1, 25); | ||
| const generatePhotoNumber = createIdGenerator(1, 25); | ||
|
|
||
| const createMessage = () => { | ||
| const first = getRandomArrayElement(MESSAGES_ARRAY); | ||
| if (createRandomRangeInteger(0, 1) === 0) { | ||
| return first; | ||
| } | ||
| const second = getRandomArrayElement(MESSAGES_ARRAY); | ||
| return `${first } ${ second}`; | ||
| }; | ||
|
|
||
| const createComment = () => ({ | ||
| id: generateCommentId(), //случайное число, не должны повторяться | ||
| avatar: `img/avatar-${ createRandomRangeInteger(1,6) }.svg`,//число от 1 до 6 выбирается рандомно | ||
| message: createMessage(), | ||
| name: getRandomArrayElement(NAMES), //массив имён, выбирается рандомно | ||
| }); | ||
|
|
||
| const createCommentsArray = function () { | ||
| const commentsArray = []; | ||
| const count = createRandomRangeInteger (0, 30); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Магические числа 0 и 30 - нужно вынести в перечисления |
||
| for (let i = 0; i <= count - 1; i++) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| commentsArray[i] = createComment(); | ||
| } | ||
| return commentsArray; | ||
| }; | ||
|
|
||
| const createDescriptionPhoto = function () { | ||
| return { | ||
| id: generatePhotoId(),//уникальное число от 1 до 25 - целое??? | ||
| url: `photos/${ generatePhotoNumber() }.jpg`, //адрес картинки photos/{{i}}.jpg, i - случаное и не должно повторяться | ||
| description: getRandomArrayElement(DESCRIPTION_ARRAY), //????? | ||
| likes: createRandomRangeInteger(15, 200), //число лайков, поставленных фотографии, от 15 до 200 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Магические числа 15, 200 |
||
| comments: createCommentsArray()//массив объектов комментарий, отсавленных к конкретной фотографии, случайное количество от 0 до 30 | ||
|
|
||
| }; | ||
| }; | ||
|
|
||
| export const createDescriptionsPhotoArray = function () { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Экспорт лучше вынести в самый низ |
||
| return Array.from ({length: 25}, createDescriptionPhoto); | ||
| }; | ||
|
|
||
|
|
||
| console.log ('Массив из сгенерированный 25-ти объектов:', JSON.stringify(createDescriptionsPhotoArray(), null, 2)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Это лучше вызывать в main.js, так как именно в main.js мы будем женить эти данные с представлением |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| /* eslint-disable no-console */ | ||
| import { createDescriptionsPhotoArray } from './generate-objects.js'; | ||
| console.log ('Генерация 25-ти объектов в массиве:', createDescriptionsPhotoArray()); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /* eslint-disable no-console */ | ||
| import './data.js'; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Утилиты должны быть чистыми функциями. Импортом с сайд эффектом (то есть импорт без переменной) вообще опасная штука, отлаживать такое очень сложно |
||
| export const createRandomRangeInteger = (min, max) => Math.floor(Math.random() * (max - min + 1) + min); | ||
| export const getRandomArrayElement = (array) => array[createRandomRangeInteger(0, array.length - 1)]; | ||
| export const createIdGenerator = (min, max) => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Убери пожалуйста все экспорты вниз |
||
| const arrayIds = []; | ||
| for (let i = min; i <= max; i++) { | ||
| arrayIds.push(i); | ||
| } | ||
| for (const i of arrayIds) { | ||
| const j = createRandomRangeInteger(0,i); | ||
| [arrayIds[i], arrayIds[j]] = [arrayIds[j], arrayIds[i]]; | ||
| } | ||
| let currentIndex = 0; | ||
| return () => { | ||
| if (currentIndex >= arrayIds.length) { | ||
| return null; | ||
| } | ||
| const id = arrayIds[currentIndex]; | ||
| currentIndex++; | ||
| return id; | ||
| }; | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Магические числа 1, 6
Нужно вынести в перечисление