Repository navigation
Expand file tree
/
Copy pathutils.js
More file actions
62 lines (49 loc) · 1.35 KB
/
Copy pathutils.js
File metadata and controls
62 lines (49 loc) · 1.35 KB
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
const { curry, pipe } = require("./core")
// get :: s -> {s: a} -> a | undefined
const get = curry((prop, obj) => obj[prop])
// last :: [a] -> a | undefined
const last = xs => xs[xs.length - 1]
// init :: [a] -> [a]
const init = xs => xs.slice(0, -1)
// split :: (String | RegExp) -> String -> [String]
const split = curry((separator, xs) => xs.split(separator))
// is :: (* -> {*}) -> Boolean
const is = curry(
(Constructor, instance) => Object(instance).constructor === Constructor
)
// map :: Functor f => (a -> b) -> f a -> f b
const mapArray = curry((f, xs) => xs.map(f))
// mapObject :: (a -> b) -> Object -> Object
const mapObject = curry((f, obj) => {
return Object.entries(obj).reduce((mapped, [key, value]) => {
mapped[key] = f(value)
return mapped
}, {})
})
// filter :: (a -> Bool) -> [a] -> [a]
const filter = curry((p, xs) => xs.filter(p))
// reduce :: ((a, b) -> a) -> b -> [a] -> a
const reduce = curry((f, id, xs) => xs.reduce(f, id))
// isObject :: a -> Bool
const isObject = is(Object)
// map :: Functor f => (a -> b) -> f a -> f b
const map = curry((f, functor) => {
return Array.isArray(functor) ? mapArray(f, functor) : mapObject(f, functor)
})
// pluck :: String -> [{ String: a}] -> [a]
const pluck = pipe(
get,
map
)
module.exports = {
last,
init,
split,
is,
map,
filter,
reduce,
isObject,
pluck,
get
}