Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor stdlib-candidate for nupm #790

Merged
merged 4 commits into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 0 additions & 84 deletions stdlib-candidate/fs.nu

This file was deleted.

8 changes: 8 additions & 0 deletions stdlib-candidate/nupm.nuon
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
name: "std-rfc"
description: "Official candidates for Nushell standard library"
documentation: "https://github.com/nushell/nu_scripts/blob/main/stdlib-candidate/std-rfc/README.md"
license: "https://github.com/nushell/nu_scripts/blob/main/LICENSE"
version: 0.1.0
type: "module"
}
69 changes: 0 additions & 69 deletions stdlib-candidate/record/mod.nu

This file was deleted.

35 changes: 35 additions & 0 deletions stdlib-candidate/std-rfc/fs.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# rename a bulk of files in a directory using a closure
#
# the reason behind this command is quite simple
# - sometimes one receives a bunch of files with integer ids: 1, 2, 3, ...
# - these ids come rarely with padding... i.e. 1 instead of 001 when there are 3-digit ids
# - this means that file with id 9 will be sorted way after file with id 1000
#
# this command allows to do such a task!
#
# # Examples
# rename files in `/foo` with a name that has an id to have 3 digits with 0-padding
# > file bulk-rename /foo {
# parse "some_format_{id}"
# | get 0
# | update id { fill --alignment r --character 0 --width 3 }
# | $"some_format_($in.id)"
# }
export def "file bulk-rename" [
directory: path, # the path where files need to be renamed in bulk
stem_update: closure, # the code to run on the stem of the files: should start with parsing the format and end with reconstructing the same format
--verbose, # be verbose when moving the files around
]: nothing -> nothing {
ls --full-paths $directory | insert new {|row|
$row.name | path parse | update stem $stem_update | path join
}
| each {
if $verbose {
mv --force --verbose $in.name $in.new
} else {
mv --force $in.name $in.new
}
}

null
}
6 changes: 6 additions & 0 deletions stdlib-candidate/std-rfc/mod.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# modules
export module record/
export module str.nu
# commands
export use fs.nu *
export use set-env.nu *
46 changes: 46 additions & 0 deletions stdlib-candidate/std-rfc/record/mod.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Merge a list of records
export def "list merge" []: list<record> -> record {
let list = $in
mut result = {}
for $obj in $list {
$result = ($result | merge $obj)
}
$result
}

# Filter fields name by predicate
export def "filter-name predicate" [
pred: closure # Predicate closure that checks fields name
]: record -> record {
let $obj_input = $in
$obj_input
| columns
| where { $in | do $pred }
| each {|input|
{ $input: ($obj_input | get $input) }
}
| list merge
}

# Filter fields name by text checking
export def "filter-name text" [
filter: string # Text to match with
--regex(-r) # Match by regex
]: record -> record {
let obj = $in
$obj | filter-name predicate { not ($in | (if $regex {find -r $filter} else {find $filter}) | is-empty) }
}

# Filter fields value by predicate
export def "filter-value predicate" [
pred: closure # Predicate closure that checks fields value
]: record -> record {
let $obj_input = $in
$obj_input
| columns
| where {|col| $obj_input | get $col | do $pred }
| each {|input|
{ $input: ($obj_input | get $input) }
}
| list merge
}
File renamed without changes.
17 changes: 17 additions & 0 deletions stdlib-candidate/std-rfc/str.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export def append [tail: string]: [string -> string, list<string> -> list<string>] {
let input = $in
match ($input | describe | str replace --regex '<.*' '') {
"string" => { $input ++ $tail },
"list" => { $input | each {|el| $el ++ $tail} },
_ => $input
}
}

export def prepend [head: string]: [string -> string, list<string> -> list<string>] {
let input = $in
match ($input | describe | str replace --regex '<.*' '') {
"string" => { $head ++ $input },
"list" => { $input | each {|el| $head ++ $el } },
_ => $input
}
}
33 changes: 0 additions & 33 deletions stdlib-candidate/str.nu

This file was deleted.

50 changes: 50 additions & 0 deletions stdlib-candidate/tests/fs.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use std assert
use ../std-rfc "file bulk-rename"

alias rename = file bulk-rename

export def "test file bulk-rename" [] {
let test_dir = $nu.temp-path | path join (random uuid)

mkdir $test_dir
seq 1 10 | each {|i| touch ($test_dir | path join $"some_($i)_format.txt") }

let expected = [
"some_10_format.txt",
"some_1_format.txt",
"some_2_format.txt",
"some_3_format.txt",
"some_4_format.txt",
"some_5_format.txt",
"some_6_format.txt",
"some_7_format.txt",
"some_8_format.txt",
"some_9_format.txt",
]
let actual = glob $"($test_dir)/*" | str replace $test_dir "" | str trim --left --char "/"
assert equal ($actual | sort) $expected

rename $test_dir {
parse "some_{i}_format"
| get 0
| update i { fill --alignment r --character 0 --width 3 }
| $"some_($in.i)_format"
}

let expected = [
"some_001_format.txt",
"some_002_format.txt",
"some_003_format.txt",
"some_004_format.txt",
"some_005_format.txt",
"some_006_format.txt",
"some_007_format.txt",
"some_008_format.txt",
"some_009_format.txt",
"some_010_format.txt",
]
let actual = glob $"($test_dir)/*" | str replace $test_dir "" | str trim --left --char "/"
assert equal ($actual | sort) $expected

rm -rf $test_dir
}
3 changes: 3 additions & 0 deletions stdlib-candidate/tests/mod.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export module fs.nu
export module record.nu
export module str.nu
20 changes: 20 additions & 0 deletions stdlib-candidate/tests/record.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use std assert
use ../std-rfc record

export def "test list_merge" [] {
assert equal ([{a:1} {b:2} {c:3} {d:4}] | record list merge) {a:1 b:2 c:3 d:4}
}

export def "test filter-name predicate" [] {
assert equal ({aa:1 ab:2 ba:3 bb:4 ca:5 cb:6} | record filter-name predicate {$in | str contains a}) {aa:1 ab:2 ba:3 ca:5}
}

export def "test filter-name text" [] {
assert equal ({aa:1 ab:2 ba:3 bb:4 ca:5 cb:6} | record filter-name text a) {aa:1 ab:2 ba:3 ca:5}
assert equal ({aa:1 ab:2 ba:3 bb:4 ca:5 cb:6} | record filter-name text -r ^a) {aa:1 ab:2}
assert equal ({aa:1 ab:2 ba:3 bb:4 ca:5 cb:6} | record filter-name text -r ^A) {}
}

export def "test filter-value predicate" [] {
assert equal ({aa:1 ab:2 ba:3 bb:4 ca:5 cb:6} | record filter-value predicate { $in mod 2 == 0 }) {ab:2 bb:4 cb:6}
}
12 changes: 12 additions & 0 deletions stdlib-candidate/tests/str.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use std assert
use ../std-rfc str

export def "test append" [] {
assert equal ("foo" | str append "/") "foo/"
assert equal (["foo", "bar", "baz"] | str append "/") ["foo/", "bar/", "baz/"]
}

export def "test prepend" [] {
assert equal ("foo" | str prepend "/") "/foo"
assert equal (["foo", "bar", "baz"] | str prepend "/") ["/foo", "/bar", "/baz"]
}