Skip to content

Commit

Permalink
Refactor stdlib-candidate for nupm (#790)
Browse files Browse the repository at this point in the history
Easier to review individual commits because of renames. Happy to provide
DiffNow links if helpful. Involved:

1. Moving scripts to a subdirectory
2. Copying `nupm.nuon` from another directory
3. Making modules work
4. Extracting tests
5. Fixing tests (related to nushell/nushell#12193)

To test first set up nupm then:

```console
nu_scripts on  std-nupm-integration
❯ $env.NUPM_REGISTRIES.nupm_test = 'https://raw.githubusercontent.com/texastoland/nupm/registry-std-rfc/registry.nuon'

nu_scripts on  std-nupm-integration
❯ nupm install std-rfc
╭──────────┬───────────────────────────────────────────╮
│ name     │ std-rfc                                   │
│ version  │ 0.1.0                                     │
│ url      │ https://github.com/texastoland/nu_scripts │
│ revision │ 65aa7cc                                   │
│ path     │ stdlib-candidate                          │
│ type     │ git                                       │
╰──────────┴───────────────────────────────────────────╯
Cloning into 'nu_scripts-4a047f13a05fe35393f3a8d73377b02c-65aa7cc'...
remote: Enumerating objects: 8015, done.
remote: Counting objects: 100% (822/822), done.
remote: Compressing objects: 100% (333/333), done.
remote: Total 8015 (delta 538), reused 641 (delta 445), pack-reused 7193
Receiving objects: 100% (8015/8015), 49.72 MiB | 23.12 MiB/s, done.
Resolving deltas: 100% (4605/4605), done.
Note: switching to '65aa7cc'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at 65aa7cc Fix nupm test --dir stdlib-candidate
2024-03-12T20:42:49.462|INF|installing package std-rfc

nu_scripts on  std-nupm-integration took 4s
❯ use std-rfc set-env

nu_scripts on  std-nupm-integration
❯ set-env -h
Gracefully set an environment variable or merge a nested option.
...etc.

nu_scripts on  std-nupm-integration
❯ nupm test --dir stdlib-candidate
Testing package /Users/texas/Developer/nu_scripts/stdlib-candidate
tests record filter-name predicate ... SUCCESS
tests record filter-value predicate ... SUCCESS
tests record list_merge ... SUCCESS
tests str append ... SUCCESS
tests fs file bulk-rename ... SUCCESS
tests str prepend ... SUCCESS
tests record filter-name text ... SUCCESS
Ran 7 tests. 7 succeeded, 0 failed.
```
  • Loading branch information
texastoland authored Mar 16, 2024
1 parent 13a73ab commit cf88c11
Show file tree
Hide file tree
Showing 14 changed files with 197 additions and 186 deletions.
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 *
File renamed without changes.
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"]
}

0 comments on commit cf88c11

Please sign in to comment.