From 1559ec7370176549ef1629b36376e6a68cc27702 Mon Sep 17 00:00:00 2001 From: Kevin Van Der Werff Date: Mon, 21 Oct 2019 23:27:53 +0200 Subject: [PATCH] :art: add getAllTags & remove polyfill --- store/data.js | 38 ++++++++++---------------------------- utils/pure.js | 8 ++++++++ 2 files changed, 18 insertions(+), 28 deletions(-) diff --git a/store/data.js b/store/data.js index 4be5fd9..48610a9 100644 --- a/store/data.js +++ b/store/data.js @@ -1,27 +1,13 @@ import resources from '../resources' import * as R from 'ramda' -import { includesElOf, getAllResources, tagsNotEmpty, partiallyIncludesElOf, cleanString } from '../utils/pure' - -// Polyfill for flat -if (!Array.prototype.flat) { - Object.defineProperty(Array.prototype, 'flat', { - configurable: true, - value: function flat () { - var depth = isNaN(arguments[0]) ? 1 : Number(arguments[0]) - - return depth ? Array.prototype.reduce.call(this, function (acc, cur) { - if (Array.isArray(cur)) { - acc.push.apply(acc, flat.call(cur, depth - 1)) - } else { - acc.push(cur) - } - - return acc - }, []) : Array.prototype.slice.call(this) - }, - writable: true, - }) -} +import { + getAllResources, + getAllTags, + includesElOf, + partiallyIncludesElOf, + tagsNotEmpty, + cleanString, +} from '../utils/pure' export const state = () => ({ resources: resources.map(category => ({ @@ -36,11 +22,7 @@ export const state = () => ({ }), })), // List of all tags, duplicates removed - tags: [...new Set( - resources - .map(resource => resource.resources).flat() - .map(resource => resource.tags).flat() - )], + tags: getAllTags(resources), }) export const getters = { @@ -49,7 +31,7 @@ export const getters = { findCategory: state => categoryTitle => { // equalsCategoryTitle :: Category -> Bool const equalsCategoryTitle = R.compose( - R.equals(R.toLower(categoryTitle)), R.toLower, R.prop('title') + R.equals(cleanString(categoryTitle)), cleanString, R.prop('title') ) // findCategory :: [Category] -> Category const findCategory = R.find(equalsCategoryTitle) diff --git a/utils/pure.js b/utils/pure.js index bde492f..997fbe2 100644 --- a/utils/pure.js +++ b/utils/pure.js @@ -24,6 +24,14 @@ export const isNotEmpty = R.compose(R.not, R.isEmpty) // getAllResources :: [Category] -> [Resource] export const getAllResources = R.compose(R.flatten, R.map(R.prop('resources'))) +// getAllTags :: [Category] -> [String] +export const getAllTags = R.compose( + R.uniq, + R.flatten, + R.map(R.prop('tags')), + getAllResources +) + // tagsNotEmpty :: Resource -> Bool export const tagsNotEmpty = R.compose(isNotEmpty, R.prop('tags'))