-
Notifications
You must be signed in to change notification settings - Fork 0
add jsfunc #5
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
Open
Seal98
wants to merge
5
commits into
master
Choose a base branch
from
jsfunc
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
add jsfunc #5
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,341 @@ | ||
| (function () { | ||
| var photoPosts = [ | ||
| { | ||
| id: '1', | ||
| description: 'NARUTO best anime', | ||
| createdAt: new Date('2014-12-12T12:12:12'), | ||
| author: 'Misha', | ||
| photoLink: 'img/naruto.png', | ||
| postVisibility: true, | ||
| hashTags: ["#NARUTOBESTANIME"], | ||
| likes: ["Alex"] | ||
| }, | ||
|
|
||
| { | ||
| id: '2', | ||
| description: 'AVATAR best anime', | ||
| createdAt: new Date('2015-12-12T12:12:12'), | ||
| author: 'Misha', | ||
| photoLink: 'img/avatar.png', | ||
| postVisibility: true, | ||
| hashTags: ["#AVATARBESTANIME"], | ||
| likes: ["Egor"] | ||
| }, { | ||
| id: '3', | ||
| description: 'SHAMAN-KING best anime', | ||
| createdAt: new Date('2012-12-12T12:12:12'), | ||
| author: 'Misha2019', | ||
| photoLink: 'img/sham.png', | ||
| postVisibility: true, | ||
| hashTags: ["#SHAMANKINGBESTANIME"], | ||
| likes: ["Pomeedorka"] | ||
| }, | ||
| { | ||
| id: '4', | ||
| description: 'NARURO best anime', | ||
| createdAt: new Date('2013-12-12T12:12:12'), | ||
| author: 'Misha20', | ||
| photoLink: 'img/naruto.png', | ||
| postVisibility: true, | ||
| hashTags: ["#NARUTOBESTANIME"], | ||
| likes: ["Jora"] | ||
| }, | ||
| { | ||
| id: '5', | ||
| description: 'AVATAR best anime', | ||
| createdAt: new Date('2018-12-12T12:12:12'), | ||
| author: 'Misha', | ||
| photoLink: 'img/avatar.png', | ||
| postVisibility: true, | ||
| hashTags: ["#AVATARBESTANIME"], | ||
| likes: ["Jaga"] | ||
| }, | ||
| { | ||
| id: '6', | ||
| description: 'SHAMAN-KING best anime', | ||
| createdAt: new Date('2017-12-12T12:12:12'), | ||
| author: 'Misha22', | ||
| photoLink: 'img/sham.png', | ||
| postVisibility: true, | ||
| hashTags: ["#SHAMANKINGBESTANIME"], | ||
| likes: ["Jaguar"] | ||
| }, | ||
| { | ||
| id: '7', | ||
| description: 'NARUTO best anime', | ||
| createdAt: new Date('2016-12-12T12:12:12'), | ||
| author: 'Misha23', | ||
| photoLink: 'img/naruto.png', | ||
| postVisibility: true, | ||
| hashTags: ["#NARUTOBESTANIME"], | ||
| likes: ["Jagorka"] | ||
| }, | ||
| { | ||
| id: '8', | ||
| description: 'AVATAR best anime', | ||
| createdAt: new Date('2091-12-12T12:12:12'), | ||
| author: 'Misha', | ||
| photoLink: 'img/avatar.png', | ||
| postVisibility: true, | ||
| hashTags: ["#AVATARBESTANIME"], | ||
| likes: ["Igor"] | ||
| }, | ||
| { | ||
| id: '9', | ||
| description: 'SHAMAN-KING best anime', | ||
| createdAt: new Date('2020-12-12T12:12:12'), | ||
| author: 'Misha', | ||
| photoLink: 'img/sham.png', | ||
| postVisibility: true, | ||
| hashTags: ["#SHAMANKINGBESTANIME"], | ||
| likes: ["Egr"] | ||
| }, | ||
|
|
||
| ]; | ||
| var postForFilter = { | ||
| author: 'Misha', | ||
| hashTags: ["#AVATARBESTANIME"] | ||
| }; | ||
| console.log("\n\nСортировка и фильтрация постов. id найденных: \n"); | ||
| var sortedArrOfPosts = getPhotoPosts(1, 2, postForFilter); | ||
| for (var i = 0; i < sortedArrOfPosts.length; i++) { | ||
| console.log("id: " + sortedArrOfPosts[i].id); | ||
| } | ||
| var postOfId = getPhotoPost("5"); | ||
| console.log("Пост по id: " + postOfId.createdAt); | ||
| var postForVal = { | ||
| id: '15', | ||
| description: 'NARUTO best anime (one more post)', | ||
| createdAt: new Date('2012-12-12T12:12:12'), | ||
| author: 'Misha2018', | ||
| photoLink: 'img/naruto.png', | ||
| postVisibility: true, | ||
| hashTags: ["#JUSTNARUTO"], | ||
| likes: ["Maladec"] | ||
| }; | ||
| console.log("Проверка поста на валидность: " + validatePhotoPost(postForVal)); | ||
| var newPost = { | ||
| id: '10', | ||
| description: 'NARUTO best anime (one more post)', | ||
| createdAt: new Date('2012-12-12T12:12:12'), | ||
| author: 'Misha2018', | ||
| photoLink: 'img/naruto.png', | ||
| postVisibility: true, | ||
| hashTags: ["#JUSTNARUTO"], | ||
| likes: ["Maladec"] | ||
| }; | ||
| console.log("Добавление нового поста с предварительной проверкой: " + addPhotoPost(newPost)); | ||
| var postForEditing = { | ||
| description: 'NARUTO best anime (one more Naruto)', | ||
| createdAt: new Date('2007-12-12T12:12:12') | ||
| } | ||
| console.log("Изменение поста по id: " + editPhotoPost("1", postForEditing)); | ||
|
|
||
| for (var i = 0; i < photoPosts.length; i++) { | ||
| if (photoPosts[i].postVisibility) { | ||
| console.log("id: " + photoPosts[i].id + "\n"); | ||
| console.log("description: " + photoPosts[i].description + "\n"); | ||
| console.log("createdAt: " + photoPosts[i].createdAt + "\n"); | ||
| console.log("author: " + photoPosts[i].author + "\n"); | ||
| console.log("photoLink: " + photoPosts[i].photoLink + "\n"); | ||
| console.log("hashTags:\n"); | ||
| for (var j = 0; j < photoPosts[i].hashTags.length; j++) { | ||
| console.log(" " + photoPosts[i].hashTags[j] + "\n"); | ||
| } | ||
| console.log("likes:\n"); | ||
| for (var j = 0; j < photoPosts[i].likes.length; j++) { | ||
| console.log(" " + photoPosts[i].likes[j] + "\n"); | ||
| } | ||
| console.log("\n"); | ||
| } | ||
| } | ||
| console.log("--------------------------------\n\n"); | ||
| console.log("Удаление поста по id: " + removePhotoPost("5")); | ||
|
|
||
| for (var i = 0; i < photoPosts.length; i++) { | ||
| if (photoPosts[i].postVisibility) { | ||
| console.log("id: " + photoPosts[i].id + "\n"); | ||
| console.log("description: " + photoPosts[i].description + "\n"); | ||
| console.log("createdAt: " + photoPosts[i].createdAt + "\n"); | ||
| console.log("author: " + photoPosts[i].author + "\n"); | ||
| console.log("photoLink: " + photoPosts[i].photoLink + "\n"); | ||
| console.log("hashTags:\n"); | ||
| for (var j = 0; j < photoPosts[i].hashTags.length; j++) { | ||
| console.log(" " + photoPosts[i].hashTags[j] + "\n"); | ||
| } | ||
| console.log("likes:\n"); | ||
| for (var j = 0; j < photoPosts[i].likes.length; j++) { | ||
| console.log(" " + photoPosts[i].likes[j] + "\n"); | ||
| } | ||
| console.log("\n"); | ||
| } | ||
| } | ||
|
|
||
| function getPhotoPosts(postStart, number, filterConfig) { | ||
| if (postStart + number > photoPosts.length || postStart < 0) { | ||
| console.log("Нельзя вывести столько постов. Промежуток вывода не совпадает с количеством постов"); | ||
| return null; | ||
| } | ||
| else { | ||
| var sortedArrOfPosts = photoPosts; | ||
| if (filterConfig.author) { | ||
| sortedArrOfPosts = sortedArrOfPosts.filter(function (post) { | ||
| return post.author === filterConfig.author; | ||
| }); | ||
| } | ||
| if (filterConfig.createdAt) { | ||
| sortedArrOfPosts = sortedArrOfPosts.filter(function (post) { | ||
| return post.createdAt === filterConfig.createdAt; | ||
| }); | ||
| } | ||
| if (filterConfig.hashTags) { | ||
| sortedArrOfPosts = sortedArrOfPosts.filter(function (post) { | ||
| for (let hashTagsOfPost of post.hashTags) { | ||
| for (let hashTagsOfFilter of [].concat(filterConfig.hashTags)) { | ||
| if (hashTagsOfPost === hashTagsOfFilter) { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| }); | ||
| } | ||
| sortedArrOfPosts = sortedArrOfPosts.sort(dateComparator).slice(postStart, number + postStart); | ||
|
|
||
| return sortedArrOfPosts; | ||
| } | ||
| } | ||
|
|
||
| function dateComparator(post1, post2) { | ||
| return post1.createdAt - post2.createdAt; | ||
| } | ||
|
|
||
| function getPhotoPost(idOfPost) { | ||
| return photoPosts.find(function findPost(post) { | ||
| return post.id === idOfPost; | ||
| }); | ||
| } | ||
|
|
||
| function validatePhotoPost(postForCheck) { | ||
| if (typeof postForCheck.id !== "string" || postForCheck.id === "") { | ||
| console.log("Пустое поле id"); | ||
| return false; | ||
| } | ||
|
|
||
| if (photoPosts.some(function checkUniqueness(post) { | ||
| return postForCheck.id === post.id; | ||
| })) { | ||
| console.log("id не уникальный"); | ||
| return false; | ||
| } | ||
|
|
||
| if (typeof postForCheck.description !== "string" || postForCheck.description.length > 200 || postForCheck.description === "") { | ||
|
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 validateForEditing(postForCheck); |
||
| console.log("Неверный description"); | ||
| return false; | ||
| } | ||
| if (typeof postForCheck.author !== "string" || postForCheck.author === "") { | ||
| console.log("Поле author пустое"); | ||
| return false; | ||
| } | ||
| if (!(postForCheck.createdAt instanceof Date) || ((postForCheck.createdAt).toString() === 'Invalid Date')) { | ||
| console.log("Поле даты создания не введено, либо имеет неправильную структуру"); | ||
| return false; | ||
| } | ||
| if (typeof postForCheck.photoLink !== "string" || postForCheck.photoLink === "") { | ||
| console.log("Поле photoLink пустое"); | ||
| return false; | ||
| } | ||
| if (!(postForCheck.hashTags instanceof Array)) { | ||
| console.log("Структура поля hashTags не верна"); | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| function validateForEditing(postForCheck) { | ||
| if (typeof postForCheck.description !== "string" || postForCheck.description.length > 200 || postForCheck.description === "") { | ||
| console.log("Неверный description"); | ||
|
|
||
| return false; | ||
| } | ||
| if (typeof postForCheck.author !== "string" || postForCheck.author === "") { | ||
| console.log("Поле author пустое"); | ||
| return false; | ||
| } | ||
| if (!(postForCheck.createdAt instanceof Date) || ((postForCheck.createdAt).toString() === 'Invalid Date')) { | ||
| console.log("Поле даты создания не введено, либо имеет неправильную структуру"); | ||
| return false; | ||
| } | ||
| if (typeof postForCheck.photoLink !== "string" || postForCheck.photoLink === "") { | ||
| console.log("Поле photoLink пустое"); | ||
| return false; | ||
| } | ||
| if (!(postForCheck.hashTags instanceof Array)) { | ||
| console.log("Структура поля hashTags не верна"); | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| function addPhotoPost(newPost) { | ||
| if (!validatePhotoPost(newPost)) { | ||
| return false; | ||
| } | ||
| else { | ||
| photoPosts.push(newPost); | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| function editPhotoPost(idForEd, postForEd) { | ||
| var postOfId = getPhotoPost(idForEd); | ||
| if (!postOfId) { | ||
| return false; | ||
| } | ||
| var postCopy = JSON.parse(JSON.stringify(postOfId)); | ||
| if (postForEd.description != null) { | ||
| postCopy.description = postForEd.description; | ||
| } | ||
| if (postForEd.author != null) { | ||
| postCopy.author = postForEd.author; | ||
| } | ||
| if (postForEd.createdAt != null) { | ||
| postCopy.createdAt = postForEd.createdAt; | ||
| } | ||
| if (postForEd.photoLink != null) { | ||
| postCopy.photoLink = postForEd.photoLink; | ||
| } | ||
| if (postForEd.hashTags != null) { | ||
| postCopy.hashTags = postForEd.hashTags; | ||
| } | ||
|
|
||
| if (validateForEditing(postCopy)) { | ||
| changePostData(postOfId, postCopy); | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| function changePostData(post1, post2) { | ||
| post1.description = post2.description; | ||
|
|
||
| post1.author = post2.author; | ||
|
|
||
| post1.createdAt = post2.createdAt; | ||
|
|
||
| post1.photoLink = post2.photoLink; | ||
|
|
||
| post1.hashTags = post2.hashTags; | ||
| } | ||
|
|
||
| function removePhotoPost(idForRem) { | ||
| var postOfId = getPhotoPost(idForRem); | ||
| if (postOfId == null) { | ||
| console.log("Пост для удаления не найден"); | ||
| return false; | ||
| } | ||
| else { | ||
| postOfId.postVisibility = false; | ||
| return true; | ||
| } | ||
| } | ||
| }()); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Еще фильтрация по хэштегам...