nushell/crates/nu-command/src/strings/str_/trim/trim_.rs

555 lines
17 KiB
Rust
Raw Normal View History

use crate::input_handler::{operate, CmdArgument};
2021-12-02 05:38:44 +01:00
use nu_engine::CallExt;
use nu_protocol::{
ast::{Call, CellPath},
engine::{Command, EngineState, Stack},
Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Type, Value,
2021-12-02 05:38:44 +01:00
};
#[derive(Clone)]
pub struct SubCommand;
struct Arguments {
to_trim: Option<char>,
Simplify `str trim` command (#8205) ### What? This change removes 3 flags (`--all`, `--both`, and `--format`) from `str trim`. This is a net reduction of ~450 LoC and `str trim` no longer depends on `fancy_regex`. ### Why? I found these flags to be quite confusing when reviewing `str trim` earlier today: 1. `--all` removes characters even if they're in the centre of the the string. - This is arguably not "trimming"! In all programming languages I'm familiar with, trimming only affects the start and end of a string. - If someone needs to do this, `str replace` is more natural IMO 2. `--both` trims from the left and right - Confusing and unnecessary given that this is also the default behaviour 3. `--format` replaces multiple spaces with a single space, even in the centre of the string - Again, I don't think this falls under the scope of "trimming". IMO `str replace` is a more natural fit I believe that `str trim` is simpler and easier to understand after this change. ### Before ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string -a, --all - trims all characters from both sides of the string *and* in the middle -b, --both - trims all characters from left and right side of the string -f, --format - trims spaces replacing multiple characters with singles in the middle ``` ### After ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string ```
2023-02-26 21:23:30 +01:00
trim_side: TrimSide,
cell_paths: Option<Vec<CellPath>>,
mode: ActionMode,
}
impl CmdArgument for Arguments {
fn take_cell_paths(&mut self) -> Option<Vec<CellPath>> {
self.cell_paths.take()
}
2021-12-02 05:38:44 +01:00
}
Simplify `str trim` command (#8205) ### What? This change removes 3 flags (`--all`, `--both`, and `--format`) from `str trim`. This is a net reduction of ~450 LoC and `str trim` no longer depends on `fancy_regex`. ### Why? I found these flags to be quite confusing when reviewing `str trim` earlier today: 1. `--all` removes characters even if they're in the centre of the the string. - This is arguably not "trimming"! In all programming languages I'm familiar with, trimming only affects the start and end of a string. - If someone needs to do this, `str replace` is more natural IMO 2. `--both` trims from the left and right - Confusing and unnecessary given that this is also the default behaviour 3. `--format` replaces multiple spaces with a single space, even in the centre of the string - Again, I don't think this falls under the scope of "trimming". IMO `str replace` is a more natural fit I believe that `str trim` is simpler and easier to understand after this change. ### Before ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string -a, --all - trims all characters from both sides of the string *and* in the middle -b, --both - trims all characters from left and right side of the string -f, --format - trims spaces replacing multiple characters with singles in the middle ``` ### After ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string ```
2023-02-26 21:23:30 +01:00
pub enum TrimSide {
Left,
Right,
Both,
2021-12-02 05:38:44 +01:00
}
impl Command for SubCommand {
fn name(&self) -> &str {
"str trim"
}
fn signature(&self) -> Signature {
Signature::build("str trim")
.input_output_types(vec![(Type::String, Type::String)])
.vectorizes_over_list(true)
.rest(
2021-12-02 05:38:44 +01:00
"rest",
SyntaxShape::CellPath,
"For a data structure input, trim strings at the given cell paths",
)
2021-12-02 05:38:44 +01:00
.named(
"char",
SyntaxShape::String,
"character to trim (default: whitespace)",
Some('c'),
)
.switch(
"left",
"trims characters only from the beginning of the string",
2021-12-02 05:38:44 +01:00
Some('l'),
)
.switch(
"right",
"trims characters only from the end of the string",
2021-12-02 05:38:44 +01:00
Some('r'),
)
}
fn usage(&self) -> &str {
"Trim whitespace or specific character"
}
fn search_terms(&self) -> Vec<&str> {
vec!["whitespace", "strip", "lstrip", "rstrip"]
2021-12-02 05:38:44 +01:00
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let character = call.get_flag::<Spanned<String>>(engine_state, stack, "char")?;
let to_trim = match character.as_ref() {
Some(v) => {
if v.item.chars().count() > 1 {
return Err(ShellError::GenericError(
"Trim only works with single character".into(),
"needs single character".into(),
Some(v.span),
None,
Vec::new(),
));
}
v.item.chars().next()
}
None => None,
};
let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 0)?;
let cell_paths = (!cell_paths.is_empty()).then_some(cell_paths);
let mode = match cell_paths {
None => ActionMode::Global,
Some(_) => ActionMode::Local,
};
Simplify `str trim` command (#8205) ### What? This change removes 3 flags (`--all`, `--both`, and `--format`) from `str trim`. This is a net reduction of ~450 LoC and `str trim` no longer depends on `fancy_regex`. ### Why? I found these flags to be quite confusing when reviewing `str trim` earlier today: 1. `--all` removes characters even if they're in the centre of the the string. - This is arguably not "trimming"! In all programming languages I'm familiar with, trimming only affects the start and end of a string. - If someone needs to do this, `str replace` is more natural IMO 2. `--both` trims from the left and right - Confusing and unnecessary given that this is also the default behaviour 3. `--format` replaces multiple spaces with a single space, even in the centre of the string - Again, I don't think this falls under the scope of "trimming". IMO `str replace` is a more natural fit I believe that `str trim` is simpler and easier to understand after this change. ### Before ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string -a, --all - trims all characters from both sides of the string *and* in the middle -b, --both - trims all characters from left and right side of the string -f, --format - trims spaces replacing multiple characters with singles in the middle ``` ### After ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string ```
2023-02-26 21:23:30 +01:00
let left = call.has_flag("left");
let right = call.has_flag("right");
let trim_side = match (left, right) {
(true, true) => TrimSide::Both,
(true, false) => TrimSide::Left,
(false, true) => TrimSide::Right,
(false, false) => TrimSide::Both,
};
let args = Arguments {
to_trim,
Simplify `str trim` command (#8205) ### What? This change removes 3 flags (`--all`, `--both`, and `--format`) from `str trim`. This is a net reduction of ~450 LoC and `str trim` no longer depends on `fancy_regex`. ### Why? I found these flags to be quite confusing when reviewing `str trim` earlier today: 1. `--all` removes characters even if they're in the centre of the the string. - This is arguably not "trimming"! In all programming languages I'm familiar with, trimming only affects the start and end of a string. - If someone needs to do this, `str replace` is more natural IMO 2. `--both` trims from the left and right - Confusing and unnecessary given that this is also the default behaviour 3. `--format` replaces multiple spaces with a single space, even in the centre of the string - Again, I don't think this falls under the scope of "trimming". IMO `str replace` is a more natural fit I believe that `str trim` is simpler and easier to understand after this change. ### Before ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string -a, --all - trims all characters from both sides of the string *and* in the middle -b, --both - trims all characters from left and right side of the string -f, --format - trims spaces replacing multiple characters with singles in the middle ``` ### After ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string ```
2023-02-26 21:23:30 +01:00
trim_side,
cell_paths,
mode,
};
operate(action, args, input, call.head, engine_state.ctrlc.clone())
2021-12-02 05:38:44 +01:00
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Trim whitespace",
example: "'Nu shell ' | str trim",
result: Some(Value::test_string("Nu shell")),
},
Example {
description: "Trim a specific character",
example: "'=== Nu shell ===' | str trim -c '=' | str trim",
result: Some(Value::test_string("Nu shell")),
},
Example {
description: "Trim whitespace from the beginning of string",
example: "' Nu shell ' | str trim -l",
result: Some(Value::test_string("Nu shell ")),
},
Example {
description: "Trim a specific character",
example: "'=== Nu shell ===' | str trim -c '='",
result: Some(Value::test_string(" Nu shell ")),
},
Example {
description: "Trim whitespace from the end of string",
example: "' Nu shell ' | str trim -r",
result: Some(Value::test_string(" Nu shell")),
},
Example {
description: "Trim a specific character",
example: "'=== Nu shell ===' | str trim -r -c '='",
result: Some(Value::test_string("=== Nu shell ")),
},
]
}
}
#[derive(Debug, Copy, Clone)]
pub enum ActionMode {
Local,
Global,
}
fn action(input: &Value, arg: &Arguments, head: Span) -> Value {
let char_ = arg.to_trim;
Simplify `str trim` command (#8205) ### What? This change removes 3 flags (`--all`, `--both`, and `--format`) from `str trim`. This is a net reduction of ~450 LoC and `str trim` no longer depends on `fancy_regex`. ### Why? I found these flags to be quite confusing when reviewing `str trim` earlier today: 1. `--all` removes characters even if they're in the centre of the the string. - This is arguably not "trimming"! In all programming languages I'm familiar with, trimming only affects the start and end of a string. - If someone needs to do this, `str replace` is more natural IMO 2. `--both` trims from the left and right - Confusing and unnecessary given that this is also the default behaviour 3. `--format` replaces multiple spaces with a single space, even in the centre of the string - Again, I don't think this falls under the scope of "trimming". IMO `str replace` is a more natural fit I believe that `str trim` is simpler and easier to understand after this change. ### Before ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string -a, --all - trims all characters from both sides of the string *and* in the middle -b, --both - trims all characters from left and right side of the string -f, --format - trims spaces replacing multiple characters with singles in the middle ``` ### After ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string ```
2023-02-26 21:23:30 +01:00
let trim_side = &arg.trim_side;
let mode = &arg.mode;
2021-12-02 05:38:44 +01:00
match input {
Value::String { val: s, .. } => Value::String {
Simplify `str trim` command (#8205) ### What? This change removes 3 flags (`--all`, `--both`, and `--format`) from `str trim`. This is a net reduction of ~450 LoC and `str trim` no longer depends on `fancy_regex`. ### Why? I found these flags to be quite confusing when reviewing `str trim` earlier today: 1. `--all` removes characters even if they're in the centre of the the string. - This is arguably not "trimming"! In all programming languages I'm familiar with, trimming only affects the start and end of a string. - If someone needs to do this, `str replace` is more natural IMO 2. `--both` trims from the left and right - Confusing and unnecessary given that this is also the default behaviour 3. `--format` replaces multiple spaces with a single space, even in the centre of the string - Again, I don't think this falls under the scope of "trimming". IMO `str replace` is a more natural fit I believe that `str trim` is simpler and easier to understand after this change. ### Before ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string -a, --all - trims all characters from both sides of the string *and* in the middle -b, --both - trims all characters from left and right side of the string -f, --format - trims spaces replacing multiple characters with singles in the middle ``` ### After ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string ```
2023-02-26 21:23:30 +01:00
val: trim(s, char_, trim_side),
2021-12-02 05:38:44 +01:00
span: head,
},
Standardise the use of ShellError::UnsupportedInput and ShellError::TypeMismatch and add spans to every instance of the former (#7217) # Description * I was dismayed to discover recently that UnsupportedInput and TypeMismatch are used *extremely* inconsistently across the codebase. UnsupportedInput is sometimes used for input type-checks (as per the name!!), but *also* used for argument type-checks. TypeMismatch is also used for both. I thus devised the following standard: input type-checking *only* uses UnsupportedInput, and argument type-checking *only* uses TypeMismatch. Moreover, to differentiate them, UnsupportedInput now has *two* error arrows (spans), one pointing at the command and the other at the input origin, while TypeMismatch only has the one (because the command should always be nearby) * In order to apply that standard, a very large number of UnsupportedInput uses were changed so that the input's span could be retrieved and delivered to it. * Additionally, I noticed many places where **errors are not propagated correctly**: there are lots of `match` sites which take a Value::Error, then throw it away and replace it with a new Value::Error with less/misleading information (such as reporting the error as an "incorrect type"). I believe that the earliest errors are the most important, and should always be propagated where possible. * Also, to standardise one broad subset of UnsupportedInput error messages, who all used slightly different wordings of "expected `<type>`, got `<type>`", I created OnlySupportsThisInputType as a variant of it. * Finally, a bunch of error sites that had "repeated spans" - i.e. where an error expected two spans, but `call.head` was given for both - were fixed to use different spans. # Example BEFORE ``` 〉20b | str starts-with 'a' Error: nu::shell::unsupported_input (link) × Unsupported input ╭─[entry #31:1:1] 1 │ 20b | str starts-with 'a' · ┬ · ╰── Input's type is filesize. This command only works with strings. ╰──── 〉'a' | math cos Error: nu::shell::unsupported_input (link) × Unsupported input ╭─[entry #33:1:1] 1 │ 'a' | math cos · ─┬─ · ╰── Only numerical values are supported, input type: String ╰──── 〉0x[12] | encode utf8 Error: nu::shell::unsupported_input (link) × Unsupported input ╭─[entry #38:1:1] 1 │ 0x[12] | encode utf8 · ───┬── · ╰── non-string input ╰──── ``` AFTER ``` 〉20b | str starts-with 'a' Error: nu::shell::pipeline_mismatch (link) × Pipeline mismatch. ╭─[entry #1:1:1] 1 │ 20b | str starts-with 'a' · ┬ ───────┬─────── · │ ╰── only string input data is supported · ╰── input type: filesize ╰──── 〉'a' | math cos Error: nu::shell::pipeline_mismatch (link) × Pipeline mismatch. ╭─[entry #2:1:1] 1 │ 'a' | math cos · ─┬─ ────┬─── · │ ╰── only numeric input data is supported · ╰── input type: string ╰──── 〉0x[12] | encode utf8 Error: nu::shell::pipeline_mismatch (link) × Pipeline mismatch. ╭─[entry #3:1:1] 1 │ 0x[12] | encode utf8 · ───┬── ───┬── · │ ╰── only string input data is supported · ╰── input type: binary ╰──── ``` # User-Facing Changes Various error messages suddenly make more sense (i.e. have two arrows instead of one). # Tests + Formatting Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date.
2022-12-23 07:48:53 +01:00
// Propagate errors by explicitly matching them before the final case.
Value::Error { .. } => input.clone(),
2021-12-02 05:38:44 +01:00
other => match mode {
ActionMode::Global => match other {
Value::Record { cols, vals, span } => {
let new_vals = vals.iter().map(|v| action(v, arg, head)).collect();
2021-12-02 05:38:44 +01:00
Value::Record {
cols: cols.to_vec(),
vals: new_vals,
span: *span,
}
}
Value::List { vals, span } => {
let new_vals = vals.iter().map(|v| action(v, arg, head)).collect();
2021-12-02 05:38:44 +01:00
Value::List {
vals: new_vals,
span: *span,
}
}
_ => input.clone(),
},
ActionMode::Local => {
Value::Error {
Standardise the use of ShellError::UnsupportedInput and ShellError::TypeMismatch and add spans to every instance of the former (#7217) # Description * I was dismayed to discover recently that UnsupportedInput and TypeMismatch are used *extremely* inconsistently across the codebase. UnsupportedInput is sometimes used for input type-checks (as per the name!!), but *also* used for argument type-checks. TypeMismatch is also used for both. I thus devised the following standard: input type-checking *only* uses UnsupportedInput, and argument type-checking *only* uses TypeMismatch. Moreover, to differentiate them, UnsupportedInput now has *two* error arrows (spans), one pointing at the command and the other at the input origin, while TypeMismatch only has the one (because the command should always be nearby) * In order to apply that standard, a very large number of UnsupportedInput uses were changed so that the input's span could be retrieved and delivered to it. * Additionally, I noticed many places where **errors are not propagated correctly**: there are lots of `match` sites which take a Value::Error, then throw it away and replace it with a new Value::Error with less/misleading information (such as reporting the error as an "incorrect type"). I believe that the earliest errors are the most important, and should always be propagated where possible. * Also, to standardise one broad subset of UnsupportedInput error messages, who all used slightly different wordings of "expected `<type>`, got `<type>`", I created OnlySupportsThisInputType as a variant of it. * Finally, a bunch of error sites that had "repeated spans" - i.e. where an error expected two spans, but `call.head` was given for both - were fixed to use different spans. # Example BEFORE ``` 〉20b | str starts-with 'a' Error: nu::shell::unsupported_input (link) × Unsupported input ╭─[entry #31:1:1] 1 │ 20b | str starts-with 'a' · ┬ · ╰── Input's type is filesize. This command only works with strings. ╰──── 〉'a' | math cos Error: nu::shell::unsupported_input (link) × Unsupported input ╭─[entry #33:1:1] 1 │ 'a' | math cos · ─┬─ · ╰── Only numerical values are supported, input type: String ╰──── 〉0x[12] | encode utf8 Error: nu::shell::unsupported_input (link) × Unsupported input ╭─[entry #38:1:1] 1 │ 0x[12] | encode utf8 · ───┬── · ╰── non-string input ╰──── ``` AFTER ``` 〉20b | str starts-with 'a' Error: nu::shell::pipeline_mismatch (link) × Pipeline mismatch. ╭─[entry #1:1:1] 1 │ 20b | str starts-with 'a' · ┬ ───────┬─────── · │ ╰── only string input data is supported · ╰── input type: filesize ╰──── 〉'a' | math cos Error: nu::shell::pipeline_mismatch (link) × Pipeline mismatch. ╭─[entry #2:1:1] 1 │ 'a' | math cos · ─┬─ ────┬─── · │ ╰── only numeric input data is supported · ╰── input type: string ╰──── 〉0x[12] | encode utf8 Error: nu::shell::pipeline_mismatch (link) × Pipeline mismatch. ╭─[entry #3:1:1] 1 │ 0x[12] | encode utf8 · ───┬── ───┬── · │ ╰── only string input data is supported · ╰── input type: binary ╰──── ``` # User-Facing Changes Various error messages suddenly make more sense (i.e. have two arrows instead of one). # Tests + Formatting Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date.
2022-12-23 07:48:53 +01:00
error: ShellError::UnsupportedInput(
"Only string values are supported".into(),
format!("input type: {:?}", other.get_type()),
head,
// This line requires the Value::Error match above.
other.expect_span(),
),
2021-12-02 05:38:44 +01:00
}
}
},
}
}
Simplify `str trim` command (#8205) ### What? This change removes 3 flags (`--all`, `--both`, and `--format`) from `str trim`. This is a net reduction of ~450 LoC and `str trim` no longer depends on `fancy_regex`. ### Why? I found these flags to be quite confusing when reviewing `str trim` earlier today: 1. `--all` removes characters even if they're in the centre of the the string. - This is arguably not "trimming"! In all programming languages I'm familiar with, trimming only affects the start and end of a string. - If someone needs to do this, `str replace` is more natural IMO 2. `--both` trims from the left and right - Confusing and unnecessary given that this is also the default behaviour 3. `--format` replaces multiple spaces with a single space, even in the centre of the string - Again, I don't think this falls under the scope of "trimming". IMO `str replace` is a more natural fit I believe that `str trim` is simpler and easier to understand after this change. ### Before ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string -a, --all - trims all characters from both sides of the string *and* in the middle -b, --both - trims all characters from left and right side of the string -f, --format - trims spaces replacing multiple characters with singles in the middle ``` ### After ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string ```
2023-02-26 21:23:30 +01:00
fn trim(s: &str, char_: Option<char>, trim_side: &TrimSide) -> String {
2021-12-02 05:38:44 +01:00
let delimiters = match char_ {
Some(c) => vec![c],
// Trying to make this trim work like rust default trim()
// which uses is_whitespace() as a default
None => vec![
' ', // space
'\x09', // horizontal tab
'\x0A', // new line, line feed
'\x0B', // vertical tab
'\x0C', // form feed, new page
'\x0D', // carriage return
], //whitespace
};
Simplify `str trim` command (#8205) ### What? This change removes 3 flags (`--all`, `--both`, and `--format`) from `str trim`. This is a net reduction of ~450 LoC and `str trim` no longer depends on `fancy_regex`. ### Why? I found these flags to be quite confusing when reviewing `str trim` earlier today: 1. `--all` removes characters even if they're in the centre of the the string. - This is arguably not "trimming"! In all programming languages I'm familiar with, trimming only affects the start and end of a string. - If someone needs to do this, `str replace` is more natural IMO 2. `--both` trims from the left and right - Confusing and unnecessary given that this is also the default behaviour 3. `--format` replaces multiple spaces with a single space, even in the centre of the string - Again, I don't think this falls under the scope of "trimming". IMO `str replace` is a more natural fit I believe that `str trim` is simpler and easier to understand after this change. ### Before ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string -a, --all - trims all characters from both sides of the string *and* in the middle -b, --both - trims all characters from left and right side of the string -f, --format - trims spaces replacing multiple characters with singles in the middle ``` ### After ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string ```
2023-02-26 21:23:30 +01:00
match trim_side {
TrimSide::Left => s.trim_start_matches(&delimiters[..]).to_string(),
TrimSide::Right => s.trim_end_matches(&delimiters[..]).to_string(),
TrimSide::Both => s.trim_matches(&delimiters[..]).to_string(),
2021-12-02 05:38:44 +01:00
}
}
#[cfg(test)]
mod tests {
use crate::strings::str_::trim::trim_::*;
use nu_protocol::{Span, Value};
2021-12-02 05:38:44 +01:00
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(SubCommand {})
}
fn make_record(cols: Vec<&str>, vals: Vec<&str>) -> Value {
Value::Record {
cols: cols.iter().map(|x| x.to_string()).collect(),
vals: vals
.iter()
.map(|x| Value::test_string(x.to_string()))
2021-12-02 05:38:44 +01:00
.collect(),
2021-12-19 08:46:13 +01:00
span: Span::test_data(),
2021-12-02 05:38:44 +01:00
}
}
fn make_list(vals: Vec<&str>) -> Value {
Value::List {
vals: vals
.iter()
.map(|x| Value::test_string(x.to_string()))
2021-12-02 05:38:44 +01:00
.collect(),
2021-12-19 08:46:13 +01:00
span: Span::test_data(),
2021-12-02 05:38:44 +01:00
}
}
#[test]
fn trims() {
let word = Value::test_string("andres ");
let expected = Value::test_string("andres");
let args = Arguments {
to_trim: None,
Simplify `str trim` command (#8205) ### What? This change removes 3 flags (`--all`, `--both`, and `--format`) from `str trim`. This is a net reduction of ~450 LoC and `str trim` no longer depends on `fancy_regex`. ### Why? I found these flags to be quite confusing when reviewing `str trim` earlier today: 1. `--all` removes characters even if they're in the centre of the the string. - This is arguably not "trimming"! In all programming languages I'm familiar with, trimming only affects the start and end of a string. - If someone needs to do this, `str replace` is more natural IMO 2. `--both` trims from the left and right - Confusing and unnecessary given that this is also the default behaviour 3. `--format` replaces multiple spaces with a single space, even in the centre of the string - Again, I don't think this falls under the scope of "trimming". IMO `str replace` is a more natural fit I believe that `str trim` is simpler and easier to understand after this change. ### Before ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string -a, --all - trims all characters from both sides of the string *and* in the middle -b, --both - trims all characters from left and right side of the string -f, --format - trims spaces replacing multiple characters with singles in the middle ``` ### After ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string ```
2023-02-26 21:23:30 +01:00
trim_side: TrimSide::Both,
cell_paths: None,
mode: ActionMode::Local,
};
let actual = action(&word, &args, Span::test_data());
2021-12-02 05:38:44 +01:00
assert_eq!(actual, expected);
}
#[test]
fn trims_global() {
let word = Value::test_string(" global ");
let expected = Value::test_string("global");
let args = Arguments {
to_trim: None,
Simplify `str trim` command (#8205) ### What? This change removes 3 flags (`--all`, `--both`, and `--format`) from `str trim`. This is a net reduction of ~450 LoC and `str trim` no longer depends on `fancy_regex`. ### Why? I found these flags to be quite confusing when reviewing `str trim` earlier today: 1. `--all` removes characters even if they're in the centre of the the string. - This is arguably not "trimming"! In all programming languages I'm familiar with, trimming only affects the start and end of a string. - If someone needs to do this, `str replace` is more natural IMO 2. `--both` trims from the left and right - Confusing and unnecessary given that this is also the default behaviour 3. `--format` replaces multiple spaces with a single space, even in the centre of the string - Again, I don't think this falls under the scope of "trimming". IMO `str replace` is a more natural fit I believe that `str trim` is simpler and easier to understand after this change. ### Before ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string -a, --all - trims all characters from both sides of the string *and* in the middle -b, --both - trims all characters from left and right side of the string -f, --format - trims spaces replacing multiple characters with singles in the middle ``` ### After ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string ```
2023-02-26 21:23:30 +01:00
trim_side: TrimSide::Both,
cell_paths: None,
mode: ActionMode::Global,
};
let actual = action(&word, &args, Span::test_data());
2021-12-02 05:38:44 +01:00
assert_eq!(actual, expected);
}
#[test]
fn global_trim_ignores_numbers() {
let number = Value::test_int(2020);
let expected = Value::test_int(2020);
let args = Arguments {
to_trim: None,
Simplify `str trim` command (#8205) ### What? This change removes 3 flags (`--all`, `--both`, and `--format`) from `str trim`. This is a net reduction of ~450 LoC and `str trim` no longer depends on `fancy_regex`. ### Why? I found these flags to be quite confusing when reviewing `str trim` earlier today: 1. `--all` removes characters even if they're in the centre of the the string. - This is arguably not "trimming"! In all programming languages I'm familiar with, trimming only affects the start and end of a string. - If someone needs to do this, `str replace` is more natural IMO 2. `--both` trims from the left and right - Confusing and unnecessary given that this is also the default behaviour 3. `--format` replaces multiple spaces with a single space, even in the centre of the string - Again, I don't think this falls under the scope of "trimming". IMO `str replace` is a more natural fit I believe that `str trim` is simpler and easier to understand after this change. ### Before ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string -a, --all - trims all characters from both sides of the string *and* in the middle -b, --both - trims all characters from left and right side of the string -f, --format - trims spaces replacing multiple characters with singles in the middle ``` ### After ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string ```
2023-02-26 21:23:30 +01:00
trim_side: TrimSide::Both,
cell_paths: None,
mode: ActionMode::Global,
};
2021-12-02 05:38:44 +01:00
let actual = action(&number, &args, Span::test_data());
2021-12-02 05:38:44 +01:00
assert_eq!(actual, expected);
}
#[test]
fn global_trim_row() {
let row = make_record(vec!["a", "b"], vec![" c ", " d "]);
// ["a".to_string() => string(" c "), " b ".to_string() => string(" d ")];
let expected = make_record(vec!["a", "b"], vec!["c", "d"]);
let args = Arguments {
to_trim: None,
Simplify `str trim` command (#8205) ### What? This change removes 3 flags (`--all`, `--both`, and `--format`) from `str trim`. This is a net reduction of ~450 LoC and `str trim` no longer depends on `fancy_regex`. ### Why? I found these flags to be quite confusing when reviewing `str trim` earlier today: 1. `--all` removes characters even if they're in the centre of the the string. - This is arguably not "trimming"! In all programming languages I'm familiar with, trimming only affects the start and end of a string. - If someone needs to do this, `str replace` is more natural IMO 2. `--both` trims from the left and right - Confusing and unnecessary given that this is also the default behaviour 3. `--format` replaces multiple spaces with a single space, even in the centre of the string - Again, I don't think this falls under the scope of "trimming". IMO `str replace` is a more natural fit I believe that `str trim` is simpler and easier to understand after this change. ### Before ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string -a, --all - trims all characters from both sides of the string *and* in the middle -b, --both - trims all characters from left and right side of the string -f, --format - trims spaces replacing multiple characters with singles in the middle ``` ### After ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string ```
2023-02-26 21:23:30 +01:00
trim_side: TrimSide::Both,
cell_paths: None,
mode: ActionMode::Global,
};
let actual = action(&row, &args, Span::test_data());
2021-12-02 05:38:44 +01:00
assert_eq!(actual, expected);
}
#[test]
fn global_trim_table() {
let row = make_list(vec![" a ", "d"]);
let expected = make_list(vec!["a", "d"]);
let args = Arguments {
to_trim: None,
Simplify `str trim` command (#8205) ### What? This change removes 3 flags (`--all`, `--both`, and `--format`) from `str trim`. This is a net reduction of ~450 LoC and `str trim` no longer depends on `fancy_regex`. ### Why? I found these flags to be quite confusing when reviewing `str trim` earlier today: 1. `--all` removes characters even if they're in the centre of the the string. - This is arguably not "trimming"! In all programming languages I'm familiar with, trimming only affects the start and end of a string. - If someone needs to do this, `str replace` is more natural IMO 2. `--both` trims from the left and right - Confusing and unnecessary given that this is also the default behaviour 3. `--format` replaces multiple spaces with a single space, even in the centre of the string - Again, I don't think this falls under the scope of "trimming". IMO `str replace` is a more natural fit I believe that `str trim` is simpler and easier to understand after this change. ### Before ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string -a, --all - trims all characters from both sides of the string *and* in the middle -b, --both - trims all characters from left and right side of the string -f, --format - trims spaces replacing multiple characters with singles in the middle ``` ### After ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string ```
2023-02-26 21:23:30 +01:00
trim_side: TrimSide::Both,
cell_paths: None,
mode: ActionMode::Global,
};
let actual = action(&row, &args, Span::test_data());
2021-12-02 05:38:44 +01:00
assert_eq!(actual, expected);
}
#[test]
fn trims_custom_character_both_ends() {
let word = Value::test_string("!#andres#!");
let expected = Value::test_string("#andres#");
let args = Arguments {
to_trim: Some('!'),
Simplify `str trim` command (#8205) ### What? This change removes 3 flags (`--all`, `--both`, and `--format`) from `str trim`. This is a net reduction of ~450 LoC and `str trim` no longer depends on `fancy_regex`. ### Why? I found these flags to be quite confusing when reviewing `str trim` earlier today: 1. `--all` removes characters even if they're in the centre of the the string. - This is arguably not "trimming"! In all programming languages I'm familiar with, trimming only affects the start and end of a string. - If someone needs to do this, `str replace` is more natural IMO 2. `--both` trims from the left and right - Confusing and unnecessary given that this is also the default behaviour 3. `--format` replaces multiple spaces with a single space, even in the centre of the string - Again, I don't think this falls under the scope of "trimming". IMO `str replace` is a more natural fit I believe that `str trim` is simpler and easier to understand after this change. ### Before ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string -a, --all - trims all characters from both sides of the string *and* in the middle -b, --both - trims all characters from left and right side of the string -f, --format - trims spaces replacing multiple characters with singles in the middle ``` ### After ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string ```
2023-02-26 21:23:30 +01:00
trim_side: TrimSide::Both,
cell_paths: None,
mode: ActionMode::Local,
};
let actual = action(&word, &args, Span::test_data());
2021-12-02 05:38:44 +01:00
assert_eq!(actual, expected);
}
#[test]
fn trims_whitespace_from_left() {
let word = Value::test_string(" andres ");
let expected = Value::test_string("andres ");
let args = Arguments {
to_trim: None,
Simplify `str trim` command (#8205) ### What? This change removes 3 flags (`--all`, `--both`, and `--format`) from `str trim`. This is a net reduction of ~450 LoC and `str trim` no longer depends on `fancy_regex`. ### Why? I found these flags to be quite confusing when reviewing `str trim` earlier today: 1. `--all` removes characters even if they're in the centre of the the string. - This is arguably not "trimming"! In all programming languages I'm familiar with, trimming only affects the start and end of a string. - If someone needs to do this, `str replace` is more natural IMO 2. `--both` trims from the left and right - Confusing and unnecessary given that this is also the default behaviour 3. `--format` replaces multiple spaces with a single space, even in the centre of the string - Again, I don't think this falls under the scope of "trimming". IMO `str replace` is a more natural fit I believe that `str trim` is simpler and easier to understand after this change. ### Before ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string -a, --all - trims all characters from both sides of the string *and* in the middle -b, --both - trims all characters from left and right side of the string -f, --format - trims spaces replacing multiple characters with singles in the middle ``` ### After ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string ```
2023-02-26 21:23:30 +01:00
trim_side: TrimSide::Left,
cell_paths: None,
mode: ActionMode::Local,
};
let actual = action(&word, &args, Span::test_data());
2021-12-02 05:38:44 +01:00
assert_eq!(actual, expected);
}
#[test]
fn global_trim_left_ignores_numbers() {
let number = Value::test_int(2020);
let expected = Value::test_int(2020);
let args = Arguments {
to_trim: None,
Simplify `str trim` command (#8205) ### What? This change removes 3 flags (`--all`, `--both`, and `--format`) from `str trim`. This is a net reduction of ~450 LoC and `str trim` no longer depends on `fancy_regex`. ### Why? I found these flags to be quite confusing when reviewing `str trim` earlier today: 1. `--all` removes characters even if they're in the centre of the the string. - This is arguably not "trimming"! In all programming languages I'm familiar with, trimming only affects the start and end of a string. - If someone needs to do this, `str replace` is more natural IMO 2. `--both` trims from the left and right - Confusing and unnecessary given that this is also the default behaviour 3. `--format` replaces multiple spaces with a single space, even in the centre of the string - Again, I don't think this falls under the scope of "trimming". IMO `str replace` is a more natural fit I believe that `str trim` is simpler and easier to understand after this change. ### Before ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string -a, --all - trims all characters from both sides of the string *and* in the middle -b, --both - trims all characters from left and right side of the string -f, --format - trims spaces replacing multiple characters with singles in the middle ``` ### After ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string ```
2023-02-26 21:23:30 +01:00
trim_side: TrimSide::Left,
cell_paths: None,
mode: ActionMode::Global,
};
let actual = action(&number, &args, Span::test_data());
2021-12-02 05:38:44 +01:00
assert_eq!(actual, expected);
}
#[test]
fn trims_left_global() {
let word = Value::test_string(" global ");
let expected = Value::test_string("global ");
let args = Arguments {
to_trim: None,
Simplify `str trim` command (#8205) ### What? This change removes 3 flags (`--all`, `--both`, and `--format`) from `str trim`. This is a net reduction of ~450 LoC and `str trim` no longer depends on `fancy_regex`. ### Why? I found these flags to be quite confusing when reviewing `str trim` earlier today: 1. `--all` removes characters even if they're in the centre of the the string. - This is arguably not "trimming"! In all programming languages I'm familiar with, trimming only affects the start and end of a string. - If someone needs to do this, `str replace` is more natural IMO 2. `--both` trims from the left and right - Confusing and unnecessary given that this is also the default behaviour 3. `--format` replaces multiple spaces with a single space, even in the centre of the string - Again, I don't think this falls under the scope of "trimming". IMO `str replace` is a more natural fit I believe that `str trim` is simpler and easier to understand after this change. ### Before ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string -a, --all - trims all characters from both sides of the string *and* in the middle -b, --both - trims all characters from left and right side of the string -f, --format - trims spaces replacing multiple characters with singles in the middle ``` ### After ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string ```
2023-02-26 21:23:30 +01:00
trim_side: TrimSide::Left,
cell_paths: None,
mode: ActionMode::Global,
};
let actual = action(&word, &args, Span::test_data());
2021-12-02 05:38:44 +01:00
assert_eq!(actual, expected);
}
#[test]
fn global_trim_left_row() {
let row = make_record(vec!["a", "b"], vec![" c ", " d "]);
let expected = make_record(vec!["a", "b"], vec!["c ", "d "]);
let args = Arguments {
to_trim: None,
Simplify `str trim` command (#8205) ### What? This change removes 3 flags (`--all`, `--both`, and `--format`) from `str trim`. This is a net reduction of ~450 LoC and `str trim` no longer depends on `fancy_regex`. ### Why? I found these flags to be quite confusing when reviewing `str trim` earlier today: 1. `--all` removes characters even if they're in the centre of the the string. - This is arguably not "trimming"! In all programming languages I'm familiar with, trimming only affects the start and end of a string. - If someone needs to do this, `str replace` is more natural IMO 2. `--both` trims from the left and right - Confusing and unnecessary given that this is also the default behaviour 3. `--format` replaces multiple spaces with a single space, even in the centre of the string - Again, I don't think this falls under the scope of "trimming". IMO `str replace` is a more natural fit I believe that `str trim` is simpler and easier to understand after this change. ### Before ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string -a, --all - trims all characters from both sides of the string *and* in the middle -b, --both - trims all characters from left and right side of the string -f, --format - trims spaces replacing multiple characters with singles in the middle ``` ### After ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string ```
2023-02-26 21:23:30 +01:00
trim_side: TrimSide::Left,
cell_paths: None,
mode: ActionMode::Global,
};
let actual = action(&row, &args, Span::test_data());
2021-12-02 05:38:44 +01:00
assert_eq!(actual, expected);
}
#[test]
fn global_trim_left_table() {
let row = Value::List {
vals: vec![
Value::test_string(" a "),
Value::test_int(65),
Value::test_string(" d"),
2021-12-02 05:38:44 +01:00
],
2021-12-19 08:46:13 +01:00
span: Span::test_data(),
2021-12-02 05:38:44 +01:00
};
let expected = Value::List {
vals: vec![
Value::test_string("a "),
Value::test_int(65),
Value::test_string("d"),
2021-12-02 05:38:44 +01:00
],
2021-12-19 08:46:13 +01:00
span: Span::test_data(),
2021-12-02 05:38:44 +01:00
};
let args = Arguments {
to_trim: None,
Simplify `str trim` command (#8205) ### What? This change removes 3 flags (`--all`, `--both`, and `--format`) from `str trim`. This is a net reduction of ~450 LoC and `str trim` no longer depends on `fancy_regex`. ### Why? I found these flags to be quite confusing when reviewing `str trim` earlier today: 1. `--all` removes characters even if they're in the centre of the the string. - This is arguably not "trimming"! In all programming languages I'm familiar with, trimming only affects the start and end of a string. - If someone needs to do this, `str replace` is more natural IMO 2. `--both` trims from the left and right - Confusing and unnecessary given that this is also the default behaviour 3. `--format` replaces multiple spaces with a single space, even in the centre of the string - Again, I don't think this falls under the scope of "trimming". IMO `str replace` is a more natural fit I believe that `str trim` is simpler and easier to understand after this change. ### Before ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string -a, --all - trims all characters from both sides of the string *and* in the middle -b, --both - trims all characters from left and right side of the string -f, --format - trims spaces replacing multiple characters with singles in the middle ``` ### After ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string ```
2023-02-26 21:23:30 +01:00
trim_side: TrimSide::Left,
cell_paths: None,
mode: ActionMode::Global,
};
let actual = action(&row, &args, Span::test_data());
2021-12-02 05:38:44 +01:00
assert_eq!(actual, expected);
}
#[test]
fn trims_custom_chars_from_left() {
let word = Value::test_string("!!! andres !!!");
let expected = Value::test_string(" andres !!!");
let args = Arguments {
to_trim: Some('!'),
Simplify `str trim` command (#8205) ### What? This change removes 3 flags (`--all`, `--both`, and `--format`) from `str trim`. This is a net reduction of ~450 LoC and `str trim` no longer depends on `fancy_regex`. ### Why? I found these flags to be quite confusing when reviewing `str trim` earlier today: 1. `--all` removes characters even if they're in the centre of the the string. - This is arguably not "trimming"! In all programming languages I'm familiar with, trimming only affects the start and end of a string. - If someone needs to do this, `str replace` is more natural IMO 2. `--both` trims from the left and right - Confusing and unnecessary given that this is also the default behaviour 3. `--format` replaces multiple spaces with a single space, even in the centre of the string - Again, I don't think this falls under the scope of "trimming". IMO `str replace` is a more natural fit I believe that `str trim` is simpler and easier to understand after this change. ### Before ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string -a, --all - trims all characters from both sides of the string *and* in the middle -b, --both - trims all characters from left and right side of the string -f, --format - trims spaces replacing multiple characters with singles in the middle ``` ### After ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string ```
2023-02-26 21:23:30 +01:00
trim_side: TrimSide::Left,
cell_paths: None,
mode: ActionMode::Local,
};
let actual = action(&word, &args, Span::test_data());
2021-12-02 05:38:44 +01:00
assert_eq!(actual, expected);
}
#[test]
fn trims_whitespace_from_right() {
let word = Value::test_string(" andres ");
let expected = Value::test_string(" andres");
Simplify `str trim` command (#8205) ### What? This change removes 3 flags (`--all`, `--both`, and `--format`) from `str trim`. This is a net reduction of ~450 LoC and `str trim` no longer depends on `fancy_regex`. ### Why? I found these flags to be quite confusing when reviewing `str trim` earlier today: 1. `--all` removes characters even if they're in the centre of the the string. - This is arguably not "trimming"! In all programming languages I'm familiar with, trimming only affects the start and end of a string. - If someone needs to do this, `str replace` is more natural IMO 2. `--both` trims from the left and right - Confusing and unnecessary given that this is also the default behaviour 3. `--format` replaces multiple spaces with a single space, even in the centre of the string - Again, I don't think this falls under the scope of "trimming". IMO `str replace` is a more natural fit I believe that `str trim` is simpler and easier to understand after this change. ### Before ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string -a, --all - trims all characters from both sides of the string *and* in the middle -b, --both - trims all characters from left and right side of the string -f, --format - trims spaces replacing multiple characters with singles in the middle ``` ### After ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string ```
2023-02-26 21:23:30 +01:00
let args = Arguments {
to_trim: None,
Simplify `str trim` command (#8205) ### What? This change removes 3 flags (`--all`, `--both`, and `--format`) from `str trim`. This is a net reduction of ~450 LoC and `str trim` no longer depends on `fancy_regex`. ### Why? I found these flags to be quite confusing when reviewing `str trim` earlier today: 1. `--all` removes characters even if they're in the centre of the the string. - This is arguably not "trimming"! In all programming languages I'm familiar with, trimming only affects the start and end of a string. - If someone needs to do this, `str replace` is more natural IMO 2. `--both` trims from the left and right - Confusing and unnecessary given that this is also the default behaviour 3. `--format` replaces multiple spaces with a single space, even in the centre of the string - Again, I don't think this falls under the scope of "trimming". IMO `str replace` is a more natural fit I believe that `str trim` is simpler and easier to understand after this change. ### Before ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string -a, --all - trims all characters from both sides of the string *and* in the middle -b, --both - trims all characters from left and right side of the string -f, --format - trims spaces replacing multiple characters with singles in the middle ``` ### After ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string ```
2023-02-26 21:23:30 +01:00
trim_side: TrimSide::Right,
cell_paths: None,
mode: ActionMode::Local,
};
let actual = action(&word, &args, Span::test_data());
2021-12-02 05:38:44 +01:00
assert_eq!(actual, expected);
}
#[test]
fn trims_right_global() {
let word = Value::test_string(" global ");
let expected = Value::test_string(" global");
let args = Arguments {
to_trim: None,
Simplify `str trim` command (#8205) ### What? This change removes 3 flags (`--all`, `--both`, and `--format`) from `str trim`. This is a net reduction of ~450 LoC and `str trim` no longer depends on `fancy_regex`. ### Why? I found these flags to be quite confusing when reviewing `str trim` earlier today: 1. `--all` removes characters even if they're in the centre of the the string. - This is arguably not "trimming"! In all programming languages I'm familiar with, trimming only affects the start and end of a string. - If someone needs to do this, `str replace` is more natural IMO 2. `--both` trims from the left and right - Confusing and unnecessary given that this is also the default behaviour 3. `--format` replaces multiple spaces with a single space, even in the centre of the string - Again, I don't think this falls under the scope of "trimming". IMO `str replace` is a more natural fit I believe that `str trim` is simpler and easier to understand after this change. ### Before ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string -a, --all - trims all characters from both sides of the string *and* in the middle -b, --both - trims all characters from left and right side of the string -f, --format - trims spaces replacing multiple characters with singles in the middle ``` ### After ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string ```
2023-02-26 21:23:30 +01:00
trim_side: TrimSide::Right,
cell_paths: None,
mode: ActionMode::Global,
};
let actual = action(&word, &args, Span::test_data());
2021-12-02 05:38:44 +01:00
assert_eq!(actual, expected);
}
#[test]
fn global_trim_right_ignores_numbers() {
let number = Value::test_int(2020);
let expected = Value::test_int(2020);
let args = Arguments {
to_trim: None,
Simplify `str trim` command (#8205) ### What? This change removes 3 flags (`--all`, `--both`, and `--format`) from `str trim`. This is a net reduction of ~450 LoC and `str trim` no longer depends on `fancy_regex`. ### Why? I found these flags to be quite confusing when reviewing `str trim` earlier today: 1. `--all` removes characters even if they're in the centre of the the string. - This is arguably not "trimming"! In all programming languages I'm familiar with, trimming only affects the start and end of a string. - If someone needs to do this, `str replace` is more natural IMO 2. `--both` trims from the left and right - Confusing and unnecessary given that this is also the default behaviour 3. `--format` replaces multiple spaces with a single space, even in the centre of the string - Again, I don't think this falls under the scope of "trimming". IMO `str replace` is a more natural fit I believe that `str trim` is simpler and easier to understand after this change. ### Before ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string -a, --all - trims all characters from both sides of the string *and* in the middle -b, --both - trims all characters from left and right side of the string -f, --format - trims spaces replacing multiple characters with singles in the middle ``` ### After ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string ```
2023-02-26 21:23:30 +01:00
trim_side: TrimSide::Right,
cell_paths: None,
mode: ActionMode::Global,
};
let actual = action(&number, &args, Span::test_data());
2021-12-02 05:38:44 +01:00
assert_eq!(actual, expected);
}
#[test]
fn global_trim_right_row() {
let row = make_record(vec!["a", "b"], vec![" c ", " d "]);
let expected = make_record(vec!["a", "b"], vec![" c", " d"]);
let args = Arguments {
to_trim: None,
Simplify `str trim` command (#8205) ### What? This change removes 3 flags (`--all`, `--both`, and `--format`) from `str trim`. This is a net reduction of ~450 LoC and `str trim` no longer depends on `fancy_regex`. ### Why? I found these flags to be quite confusing when reviewing `str trim` earlier today: 1. `--all` removes characters even if they're in the centre of the the string. - This is arguably not "trimming"! In all programming languages I'm familiar with, trimming only affects the start and end of a string. - If someone needs to do this, `str replace` is more natural IMO 2. `--both` trims from the left and right - Confusing and unnecessary given that this is also the default behaviour 3. `--format` replaces multiple spaces with a single space, even in the centre of the string - Again, I don't think this falls under the scope of "trimming". IMO `str replace` is a more natural fit I believe that `str trim` is simpler and easier to understand after this change. ### Before ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string -a, --all - trims all characters from both sides of the string *and* in the middle -b, --both - trims all characters from left and right side of the string -f, --format - trims spaces replacing multiple characters with singles in the middle ``` ### After ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string ```
2023-02-26 21:23:30 +01:00
trim_side: TrimSide::Right,
cell_paths: None,
mode: ActionMode::Global,
};
let actual = action(&row, &args, Span::test_data());
2021-12-02 05:38:44 +01:00
assert_eq!(actual, expected);
}
#[test]
fn global_trim_right_table() {
let row = Value::List {
vals: vec![
Value::test_string(" a "),
Value::test_int(65),
Value::test_string(" d"),
2021-12-02 05:38:44 +01:00
],
2021-12-19 08:46:13 +01:00
span: Span::test_data(),
2021-12-02 05:38:44 +01:00
};
let expected = Value::List {
vals: vec![
Value::test_string(" a"),
Value::test_int(65),
Value::test_string(" d"),
2021-12-02 05:38:44 +01:00
],
2021-12-19 08:46:13 +01:00
span: Span::test_data(),
2021-12-02 05:38:44 +01:00
};
let args = Arguments {
to_trim: None,
Simplify `str trim` command (#8205) ### What? This change removes 3 flags (`--all`, `--both`, and `--format`) from `str trim`. This is a net reduction of ~450 LoC and `str trim` no longer depends on `fancy_regex`. ### Why? I found these flags to be quite confusing when reviewing `str trim` earlier today: 1. `--all` removes characters even if they're in the centre of the the string. - This is arguably not "trimming"! In all programming languages I'm familiar with, trimming only affects the start and end of a string. - If someone needs to do this, `str replace` is more natural IMO 2. `--both` trims from the left and right - Confusing and unnecessary given that this is also the default behaviour 3. `--format` replaces multiple spaces with a single space, even in the centre of the string - Again, I don't think this falls under the scope of "trimming". IMO `str replace` is a more natural fit I believe that `str trim` is simpler and easier to understand after this change. ### Before ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string -a, --all - trims all characters from both sides of the string *and* in the middle -b, --both - trims all characters from left and right side of the string -f, --format - trims spaces replacing multiple characters with singles in the middle ``` ### After ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string ```
2023-02-26 21:23:30 +01:00
trim_side: TrimSide::Right,
cell_paths: None,
mode: ActionMode::Global,
};
let actual = action(&row, &args, Span::test_data());
2021-12-02 05:38:44 +01:00
assert_eq!(actual, expected);
}
#[test]
fn trims_custom_chars_from_right() {
let word = Value::test_string("#@! andres !@#");
let expected = Value::test_string("#@! andres !@");
let args = Arguments {
to_trim: Some('#'),
Simplify `str trim` command (#8205) ### What? This change removes 3 flags (`--all`, `--both`, and `--format`) from `str trim`. This is a net reduction of ~450 LoC and `str trim` no longer depends on `fancy_regex`. ### Why? I found these flags to be quite confusing when reviewing `str trim` earlier today: 1. `--all` removes characters even if they're in the centre of the the string. - This is arguably not "trimming"! In all programming languages I'm familiar with, trimming only affects the start and end of a string. - If someone needs to do this, `str replace` is more natural IMO 2. `--both` trims from the left and right - Confusing and unnecessary given that this is also the default behaviour 3. `--format` replaces multiple spaces with a single space, even in the centre of the string - Again, I don't think this falls under the scope of "trimming". IMO `str replace` is a more natural fit I believe that `str trim` is simpler and easier to understand after this change. ### Before ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string -a, --all - trims all characters from both sides of the string *and* in the middle -b, --both - trims all characters from left and right side of the string -f, --format - trims spaces replacing multiple characters with singles in the middle ``` ### After ``` 〉help str trim Trim whitespace or specific character Search terms: whitespace, strip, lstrip, rstrip Usage: > str trim {flags} ...(rest) Flags: -h, --help - Display the help message for this command -c, --char <String> - character to trim (default: whitespace) -l, --left - trims characters only from the beginning of the string -r, --right - trims characters only from the end of the string ```
2023-02-26 21:23:30 +01:00
trim_side: TrimSide::Right,
cell_paths: None,
mode: ActionMode::Local,
};
let actual = action(&word, &args, Span::test_data());
2021-12-02 05:38:44 +01:00
assert_eq!(actual, expected);
}
}