-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprelude.jq
80 lines (71 loc) · 1.53 KB
/
prelude.jq
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
# Sane JQ Prelude library
def version:
[0,1,0]
;
def version_string:
"v\(version[0]).\(version[1]).\(version[2])"
;
## Alternative for alternative operator
def Or(default):
if . == null then
default
else
.
end
;
def Or(value; default):
value | Or(default)
;
## Helper for checking type ==
def is_same_type(lhs; rhs):
(lhs|type) == (rhs|type)
;
## Ordering check
def lt(lhs; rhs):
if is_same_type(lhs; rhs) then
lhs < rhs
else
error("Comparing vaules of two different types: left hand side "+
"(\(lhs|@json)) is \(lhs|type) while right hand side (\(rhs|@json)) "+
"is \(rhs|type)")
end
;
def lt(rhs):
lt(.; rhs)
;
def le(lhs; rhs):
if is_same_type(lhs; rhs) then
lhs <= rhs
else
error("Comparing vaules of two different types: left hand side "+
"(\(lhs|@json)) is \(lhs|type) while right hand side (\(rhs|@json)) "+
"is \(rhs|type)")
end
;
def le(rhs):
le(.; rhs)
;
def gt(lhs; rhs):
if is_same_type(lhs; rhs) then
lhs > rhs
else
error("Comparing vaules of two different types: left hand side "+
"(\(lhs|@json)) is \(lhs|type) while right hand side (\(rhs|@json)) "+
"is \(rhs|type)")
end
;
def gt(rhs):
gt(.; rhs)
;
def ge(lhs; rhs):
if is_same_type(lhs; rhs) then
lhs >= rhs
else
error("Comparing vaules of two different types: left hand side "+
"(\(lhs|@json)) is \(lhs|type) while right hand side (\(rhs|@json)) "+
"is \(rhs|type)")
end
;
def ge(rhs):
ge(.; rhs)
;