-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathutil.js
More file actions
144 lines (131 loc) · 3.67 KB
/
Copy pathutil.js
File metadata and controls
144 lines (131 loc) · 3.67 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import AbstractSpec from './spec/spec'
import predicateSpec from './spec/predicate'
import { get } from './registry'
import * as p from './predicates'
import { isRegex, regexConform, regexExplain } from './regex'
import { invalid, optional, count, minCount, maxCount } from './symbols'
const EXTRACT_SYMBOL_KEY = /Symbol\((.*?)\)/
export function symbolToString(sym) {
if (typeof sym === 'symbol') {
return sym.toString().match(EXTRACT_SYMBOL_KEY)[1]
}
return sym
}
/**
* Takes arrays and returns another array where the first
* item is the set of first items, the second item is the
* set of second items etc.
*
* [[foo, bar], [1, 2]] <=> [[foo, 1], [bar, 2]]
*
* @param {[type]} colls [description]
* @return {[type]} [description]
*/
export function zip(...colls) {
if (colls.length === 0) {
return []
}
const [c0, ...crest] = colls
return c0.map((val, i) => [val, ...crest.map(coll => coll[i])])
}
export function getAllKeys(obj) {
return [
...Object.keys(obj), // enumerable keys
...Object.getOwnPropertySymbols(obj) // symbol keys
].filter(x => [invalid, optional, count, minCount, maxCount].indexOf(x) === -1) // filter own symbols
}
export function cardinality(coll) {
if (p.array(coll)) {
return coll.length
}
if (p.set(coll)) {
return coll.size
}
return 0
}
export function getName(thing) {
if (p.nil(thing)) {
return ''
}
// if it's a function return function name
if (p.fn(thing)) {
return thing.name || '[anonymous predicate]'
}
// if it's a regex return regex operation
if (isRegex(thing)) {
return symbolToString(thing.op)
}
// if it's a spec call toString() implementation
if (isSpecInstance(thing)) {
return thing.toString()
}
// if it's a symbol convert it to string
if (p.symbol(thing)) {
return symbolToString(thing)
}
return thing.toString()
}
function toArray(value) {
return p.array(value) ? value : [value]
}
export function conform(spec, value) {
if (isRegex(spec)) {
return regexConform(spec, toArray(value))
}
return specize(spec).conform(value)
}
/**
* Takes predicate and value. If predicate is convertable to a spec, conforms
* value to spec. Else calls predicate.
*
* @param {[type]} predicate [description]
* @param {[type]} value [description]
* @param {Boolean} [returnBoolean=false] [description]
* @return {[type]} [description]
*/
export function dt(predicate, value, returnBoolean = false) {
if (predicate) {
const spec = toSpec(predicate)
if (spec) {
return spec.conform(value)
}
if (p.fn(predicate)) {
if (returnBoolean) {
return predicate(value)
}
// normalize undefined and null
return predicate(value) ? (value === undefined ? null : value) : invalid
}
throw new Error(`${getName(predicate)} is a ${typeof predicate}, not a function. Expected predicate`)
}
return value
}
export function isSpecInstance(spec) {
return spec instanceof AbstractSpec
}
export function toSpec(maybeSpec) {
// check if maybespec is a spec or a symbol in registry
if (isSpecInstance(maybeSpec) || isRegex(maybeSpec)) {
return maybeSpec
}
if (get(maybeSpec)) {
return get(maybeSpec)
}
return null
}
export function specize(spec) {
if (toSpec(spec)) {
return toSpec(spec)
}
if (p.fn(spec)) {
return predicateSpec(spec)
}
throw new Error(`Cannot coerce ${getName(spec)} to spec`)
}
export function valid(spec, value) {
return conform(spec, value) !== invalid
}
export function explain(spec, path, via, value) {
const s = specize(spec)
return isRegex(s) ? regexExplain(s, path, via, value) : s.explain(path, via, value)
}