-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdef_data.nu
More file actions
52 lines (47 loc) · 1.42 KB
/
def_data.nu
File metadata and controls
52 lines (47 loc) · 1.42 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
#compact with empty strings and nulls
export def scompact [
...columns: string # the columns to compactify
--invert(-i) # select the opposite
] {
mut out = $in
for column in $columns {
if $invert {
$out = ($out | upsert $column {|row| if not ($row | get $column | is-empty) {null} else {$row | get $column}} | compact $column )
} else {
$out = ($out | upsert $column {|row| if ($row | get $column | is-empty) {null} else {$row | get $column}} | compact $column )
}
}
return $out
}
#flatten a record keys
#
#Example:
# flatten-keys $env.config '$env.config'
def flatten-keys [rec: record, root: string] {
$rec | columns | each {|key|
let is_record = (
$rec | get $key | describe --detailed | get type | $in == record
)
# Recusively return each key plus its subkeys
[$'($root).($key)'] ++ match $is_record {
true => (flatten-keys ($rec | get $key) $'($root).($key)')
false => []
}
} | flatten
}
# Check if date is further in the past than specified duration
export def older-than [
date: duration
]: datetime -> bool {
$in < ((date now) - $date)
}
# Check if date is closer to the present than specified duration
export def newer-than [
date: duration
]: datetime -> bool {
$in > ((date now) - $date)
}
#Calculates a past datetime by subtracting a duration from the current time.
export def ago []: [ duration -> datetime ] {
(date now) - $in
}