-
Notifications
You must be signed in to change notification settings - Fork 0
/
recipes.jq
264 lines (216 loc) · 7.08 KB
/
recipes.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#
# INSTALL
# $ curl -O https://nntrn.github.io/jq-recipes/recipes.jq
#
# USAGE
# $ jq 'include "recipes"; [..] '
#
# SOURCE
# https://github.com/nntrn/jq-recipes
#
# source: _functions/barcharts.md
def barchart($key):
length as $total
| map((.[$key] // "null") | tostring)
| group_by(.)
| (map({ key: .[0], value: length, title_len: (.[0]|tostring|length) }) ) as $columns
| $columns
| sort_by(.value) | reverse
| (max_by(.title_len)|.title_len) as $padding
|
(if (((($columns|length)/$total) > .8) or (($columns|length) > 1000)) then
[ "IGNORING <\($key)>: \($columns|length) out of \($total) rows", ""]
else [
$key,
("-" * ($key|length) ),
map(
[
.key, (" " * ($padding-.title_len)),
"\((.value/$total)*100|tostring|.+".000"|.[0:4])%",
( if (.value == 1) then "▊" else ("█" * (((.value/$total)*100) + (.value|log)|round)) end),
.value
] | join(" ")
),
""
] end)
| flatten
| join("\n");
def run_barchart:
. as $data
| (.[0]|keys) as $cols
| ($cols | map(. as $col | $data | barchart($col)) | join("\n")) as $barcharts
| [ $barcharts ]
| flatten| join("\n")
;
# source: _functions/conversion.md
def to_precision($p):
. |tostring|split(".")
| [.[0], (.[1]|split("")|.[0:($p|tonumber)]|join(""))]
| join(".")
| tonumber;
def humansize(bytes;$p):
(bytes|tonumber) as $size |
if $size > 1073741824 then "\(($size/1073741824)|to_precision($p))G"
elif $size > 1048576 then "\(($size/1048576)|to_precision($p))M"
elif $size > 1024 then "\(($size/1024)|to_precision($p))K"
else $size
end;
def humansize(bytes): humansize(bytes;1);
# source: _functions/describe.md
def describe:
walk(
if (type == "object" or type == "array")
then (if (type == "array") then ([limit(1;.[])]) else . end)
else (
if (type == "string") and (test("^https?"))
then "url"
else ((.|fromdate|"date")? // type)
end
)
end
);
# source: _functions/flatten.md
def flat_object:
[paths(scalars) as $path
| {"key": $path | join("_"), "value": getpath($path)}]
| from_entries;
def flat_array:
map( flat_object );
# source: _functions/github-api.md
def github_raw_url:
[
"curl --create-dirs -o \(.repository.full_name)/\(.path) ",
(.html_url|gsub("/github.com";"/raw.githubusercontent.com")|gsub("/blob/"; "/")),
(if .repository.private then " -H \"Authorization: Bearer $GITHUB_TOKEN\"" else "" end)
] | join("");
# source: _functions/json2csv.md
def json2csv:
(map(keys) | add | unique) as $cols
| map(. as $row | $cols | map($row[.])) as $rows
| $cols, $rows[]
| @csv;
# source: _functions/pick.md
def pick(stream):
. as $in
| reduce path(stream) as $a (null;
setpath($a; $in|getpath($a)) );
def spick($key):
getpath([($key|split(".")[]|select(length > 0))]);
# source: _functions/read-history.md
def history:
map(
if test("#[0-9]{10,12}")
then "\(.|gsub("#";"")|tonumber|todate)"
else "\t\(.)\n"
end
) | join("");
# source: _functions/summary.md
def grouped_summary($item):
{"\($item? // "blank")":group_by(.[$item])|map({"\(.[0][$item]? // "blank")":length})|add};
def summary:
[ (.[0]|keys)[] as $keys | grouped_summary($keys)]
| add
| to_entries
| map(
del(select(((.value//"")|keys[0]|length) > 100)) |
del(select(((.value//"")|values|length) > 400))
)
| map(select(.))
| from_entries;
def summary_wip:
[ (.[0]|keys)[] as $keys | grouped_summary($keys)]
| add
| to_entries
#| map(del(select(((.value//"")|keys|length) > 400)))
| map(select(.)|{key,count:(.value|length)})
| map(.value |= to_entries);
def summary2:
. as $data
| (.[0]|keys)
| map(. as $item | {
key: $item,
value: ($data|map(.[$item])|group_by(.)|map({"\(.[0])": length}))|add
})
| map(select((.value|to_entries|length)< (.90 * ($data|length))))
| from_entries;
# source: _functions/text.md
def split_newlines($s):
if ((type == "string") and (($s|tostring|split("\n")|length) > 1)?)
then ($s|tostring|split("[\\r\\n]+([\\s]+)?";"x"))
elif (type == "object") then to_entries
else $s end;
def recuse_split_newlines: walk(split_newlines(.)|from_entries? // .);
def squo: [39]|implode;
def squote($text): [squo,$text,squo]|join("");
def dquote($text): "\"\($text)\"";
def unsmart($text): $text | gsub("[“”]";"\"") | gsub("[’‘]";"'");
def unsmart: . | unsmart;
# source: _functions/unroll.md
def categorize:
# Returns "object", "array" or "scalar" to indicate the category
# of the piped element.
if type == "object" then "object"
elif type == "array" then "array"
else "scalar"
end;
def pluck($category):
# Plucks the children of a particular category from piped element.
if categorize != "object"
then empty
else to_entries[]
| select(.value | categorize == $category)
| .value
end;
def split:
# Splits the piped element's children into arrays, scalars, and objects
# and returns a meta object containing the children seperated by these
# keys. If the piped element is a scalar or array, this does not look
# at the children, but just returns that element in the meta object.
if categorize == "scalar" then { objects: [], arrays: [], scalars: [.] }
elif categorize == "array" then { objects: [], arrays: [.], scalars: [] }
else { objects: [pluck("object")], arrays : [pluck("array")], scalars: [pluck("scalar")] }
end;
def unwrap:
# Unwraps an array recursively, until the elements of the returned array
# are either scalars or objects but not arrays. If piped element is not
# an array, returns the element as is.
if type != "array" then .
elif length == 0 then empty
else .[] | unwrap
end;
def extract($category):
# Extracts the elements of a particular category from the piped in array.
# If the piped in element is not an array, this fn acts as filter to
# only return the element if it is of the desired category.
unwrap | select(.| categorize == $category);
def unroll:
# Unrolls the passed in object recursively until only scalars are left.
# Returns a row for each leaf node of tree structure of the object and
# elements of the row would be all the scalars encountered at all the
# ancestor levels of this left node.
. | .result += .state.scalars
| .state.objects += [.state.arrays | extract("object")]
| .state.objects += [.state.arrays | extract("scalar")]
| if (.state.objects | length == 0 )
then .result
else ({ data : .state.objects,
state: .state.objects[] | split,
result: .result
} | unroll)
end;
def unrolls($data): { data: $data, state: $data| split, result: [] } | unroll ;
def unrolls: unrolls(.);
# source: _general/codepoints.md
def smart_squotes($s):
$s | if (test("[\\s\\n\\t]";"x")) then "\([39]|implode)\($s)\([39]|implode)" else $s end;
def smart_dquotes($s):
$s | if (test("[\\s\\n\\t]";"x")) then "\($s|@json)" else $s end;
# source: _general/reduce.md
def tocsv:
.[0] as $cols | .[1:]
| map(. as $row
| $cols
| with_entries({ "key": .value,"value": $row[.key]})
);
# source: _general/wrangle.md
def s: [splits(" +")];