This repository was archived by the owner on Sep 4, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 167
/
Copy pathpure.js
79 lines (66 loc) · 2.06 KB
/
pure.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*eslint-disable */
import * as R from 'ramda'
/// TYPES ///
// const Category = {
// title: String,
// slug: String,
// resources: [Resource],
// }
// const RawResource = {
// title: String,
// desc: String,
// url: String,
// tags: [String]
// }
// const Resource = {
// title: String,
// cleanTitle: String,
// desc: String,
// path: String,
// url: String,
// tags: [String],
// }
/// FUNCTIONS ///
// isNotEmpty [a] -> Bool
export const isNotEmpty = R.compose(R.not, R.isEmpty)
// getAllResources :: [Category] -> [Resource]
export const getAllResources = R.compose(R.flatten, R.pluck('resources'))
// getAllTags :: [Category] -> [String]
export const getAllTags = R.compose(
R.uniq,
R.flatten,
R.pluck('tags'),
getAllResources
)
// tagsNotEmpty :: Resource -> Bool
export const tagsNotEmpty = R.compose(isNotEmpty, R.prop('tags'))
// cleanString :: String -> String
export const cleanString = R.compose(R.toLower, R.trim)
// removeSpacesLower :: String -> String
export const cleanStringAndRemoveSpaces = R.compose(R.replace(/ /g, ''), cleanString)
// true if list2 has element that appears in list1 else false
// includesElOf :: [a] -> [a] -> Bool
export const includesElOf = R.curry((list1, list2) => R.any(el => R.includes(el, list2), list1))
// Similar to includesElOf, but partially included strings are also allowed
// partiallyIncludesElOf :: [String] -> [String] -> Bool
export const partiallyIncludesElOf = R.curry((list1, list2) =>
R.any(el2 =>
R.any(R.includes(el2), list1),
list2)
)
// addCleanTitleAndPath :: RawResource -> Resource
const addCleanTitleAndPath = R.curry((slug, obj) => {
const cleanTitle = cleanStringAndRemoveSpaces(obj.title)
return {
...obj,
cleanTitle,
path: `${slug}?card=${cleanTitle}`,
}
})
// transformToResources :: [RawResource] -> [Resource]
export const transformToResources = categories => {
const resourcesLens = R.lens(R.prop('resources'), R.assoc('resources'))
return R.map(category =>
R.over(resourcesLens, R.map(addCleanTitleAndPath(category.slug)), category),
categories)
}