From a0698f09bb45d616abdf29d5c1297f1037dcc1ba Mon Sep 17 00:00:00 2001 From: Kevin Van Der Werff Date: Mon, 21 Oct 2019 16:16:10 +0200 Subject: [PATCH] :art: Refactor into pure.js --- store/data.js | 22 +++++----------------- utils/pure.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 17 deletions(-) create mode 100644 utils/pure.js diff --git a/store/data.js b/store/data.js index cd68909..e537819 100644 --- a/store/data.js +++ b/store/data.js @@ -1,5 +1,6 @@ import resources from '../resources' -import { prop, compose, flatten, map, filter, isEmpty, not, any, includes, curry } from 'ramda' +import { prop, compose, filter } from 'ramda' +import { includesElOf, getAllResources, tagsNotEmpty } from '../utils/pure' // Polyfill for flat if (!Array.prototype.flat) { @@ -22,14 +23,6 @@ if (!Array.prototype.flat) { }) } -// True if list2 has element that appears in list1 -// includesElOf([1, 2], [3]) -> false -// includesElOf([1, 2])([3]) -> false -// includesElOf([1, 2], [2]) -> true -// includesElOf([1, 2])([2]) -> true -// includesElOf :: [a] -> [a] -> Bool -// const includesElOf = curry((list1, list2) => any(flip(includes)(list2), list1)) - export const state = () => ({ resources: resources.map(category => ({ ...category, @@ -57,17 +50,12 @@ export const getters = { return Object.assign(state.resources.find(category => category.title.toLowerCase() === categoryTitle.toLowerCase())) }, findByTags: state => tags => { - // true if list2 has element that appears in list1 else false - // includesElOf [a] -> [a] -> Bool - const includesElOf = curry((list1, list2) => any(el => includes(el, list2), list1)) - // getAllResources :: [Category] -> [Resource] - const getAllResources = compose(flatten, map(prop('resources'))) - // tagsNotEmpty :: [Resource] -> Bool - const tagsNotEmpty = compose(not, isEmpty, prop('tags')) // containsTags :: [Resource] -> [Resource] const containsTags = filter(tagsNotEmpty) + // includesDesiredTags :: Resource -> Bool + const includesDesiredTags = compose(includesElOf(tags), prop('tags')) // findResourcesByTag :: [Resource] -> [Resource] - const findResourcesByTag = filter(compose(includesElOf(tags), prop('tags'))) + const findResourcesByTag = filter(includesDesiredTags) // getDesiredResources :: [Category] -> [Resource] const getDesiredResources = compose(findResourcesByTag, containsTags, getAllResources) diff --git a/utils/pure.js b/utils/pure.js new file mode 100644 index 0000000..4ae4722 --- /dev/null +++ b/utils/pure.js @@ -0,0 +1,33 @@ +/*eslint-disable */ +import * as R from 'ramda' + +/// Types +const Resource = { + title: String, + cleanTitle: String, + desc: String, + path: String, + url: String, + tags: [String], +} + +const Category = { + title: String, + slug: String, + resources: [Resource], +} + +/// Functions +// getAllResources :: [Category] -> [Resource] +const getAllResources = R.compose(R.flatten, R.map(R.prop('resources'))) + +// tagsNotEmpty :: Resource -> Bool +const tagsNotEmpty = R.compose(R.not, R.isEmpty, R.prop('tags')) + +// true if list2 has element that appears in list1 else false +// includesElOf([1, 2])([2]) -> true +// includesElOf([1, 2], [3]) -> false +// includesElOf :: [a] -> [a] -> Bool +const includesElOf = R.curry((list1, list2) => R.any(el => R.includes(el, list2), list1)) + +export { getAllResources, tagsNotEmpty, includesElOf } \ No newline at end of file