Download dev.to-clone/src/helper/sortPosts.js from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 795 Bytes
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/f5071caa249115f3b590492f1c8eb471e02aae4b/dev.to-clone/src/helper/sortPosts.js
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@f5071caa249115f3b590492f1c8eb471e02aae4b/dev.to-clone/src/helper/sortPosts.js
-
curl -L -o sortPosts.js https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/f5071caa249115f3b590492f1c8eb471e02aae4b/dev.to-clone/src/helper/sortPosts.js
795 Bytes
| import { calculateReaction } from './calculateTotal'; | |
| export const sortPosts = (sort, allPostData, followingTags) => { | |
| let currentData = []; | |
| const matchTag = (tags) => { | |
| return tags?.some((tag) => followingTags?.includes(tag.tagName)) ? 1 : -1; | |
| }; | |
| switch (sort) { | |
| case 'latest': | |
| currentData = allPostData?.sort((a, b) => b.createdAt - a.createdAt); | |
| break; | |
| case 'top': | |
| currentData = allPostData?.sort( | |
| (a, b) => | |
| calculateReaction(b.heart, b.unicorn, b.saved) - | |
| calculateReaction(a.heart, a.unicorn, a.saved) | |
| ); | |
| break; | |
| default: | |
| currentData = allPostData.sort( | |
| (a, b) => matchTag(b.tags) - matchTag(a.tags) | |
| ); | |
| } | |
| return currentData; | |
| }; | |