Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export const uniqueArray = originalArray => [...new Set(originalArray)]
- export function slugify(textToSlugify) {
- return textToSlugify
- .toLowerCase()
- .replace(/[^\w\s-]/g, '') // remove non-word [a-z0-9_], non-whitespace, non-hyphen characters
- .replace(/[\s_-]+/g, '-') // swap any length of whitespace, underscore, hyphen characters with a single -
- .replace(/^-+|-+$/g, '') // remove leading, trailing -
- }
- export const createTagsList = (postsArray) => {
- const tagsArray = []
- postsArray.forEach((post) => {
- if (post.tags.length) {
- post.tags.forEach(tag => tagsArray.push(tag))
- }
- })
- return uniqueArray(tagsArray)
- }
- export const getPostsFromTag = (posts, tag) =>
- posts.filter(post => post.tags.map(tag => slugifyText(tag)).includes(tag))
- export const slugifyText = str =>
- slugify(str, { replacement: '-', lower: true, remove: /[$*_+~.()'"!\-:@]/g })
- export const arrayToObject = (arr, keyField) =>
- Object.assign({}, ...arr.map(item => ({ [item[keyField]]: item })))
- export const arrayFromObject = object =>
- Object.keys(object).map(key => object[key])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement