mirror of
https://github.com/nushell/nushell
synced 2024-12-27 05:23:11 +00:00
Implement modulo for duration (#10745)
# Description This PR adds the ability to use modulo with durations: ```nu (2min + 31sec) mod 20sec # 11sec ``` # User-Facing Changes Allows to use `<duration> mod <duration>`
This commit is contained in:
parent
0a8f27f6f2
commit
27e6271402
2 changed files with 15 additions and 0 deletions
|
@ -3445,6 +3445,13 @@ impl Value {
|
||||||
Err(ShellError::DivisionByZero { span: op })
|
Err(ShellError::DivisionByZero { span: op })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
(Value::Duration { val: lhs, .. }, Value::Duration { val: rhs, .. }) => {
|
||||||
|
if *rhs != 0 {
|
||||||
|
Ok(Value::duration(lhs % rhs, span))
|
||||||
|
} else {
|
||||||
|
Err(ShellError::DivisionByZero { span: op })
|
||||||
|
}
|
||||||
|
}
|
||||||
(Value::CustomValue { val: lhs, .. }, rhs) => {
|
(Value::CustomValue { val: lhs, .. }, rhs) => {
|
||||||
lhs.operation(span, Operator::Math(Math::Modulo), op, rhs)
|
lhs.operation(span, Operator::Math(Math::Modulo), op, rhs)
|
||||||
}
|
}
|
||||||
|
|
|
@ -120,6 +120,14 @@ fn test_filesize_op() -> TestResult {
|
||||||
run_test("-5kb + 4.5kb", "-500 B")
|
run_test("-5kb + 4.5kb", "-500 B")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_duration_op() -> TestResult {
|
||||||
|
run_test("4min + 20sec", "4min 20sec").unwrap();
|
||||||
|
run_test("42sec * 2", "1min 24sec").unwrap();
|
||||||
|
run_test("(3min + 14sec) / 2", "1min 37sec").unwrap();
|
||||||
|
run_test("(4min + 20sec) mod 69sec", "53sec")
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn lt() -> TestResult {
|
fn lt() -> TestResult {
|
||||||
run_test("1 < 3", "true").unwrap();
|
run_test("1 < 3", "true").unwrap();
|
||||||
|
|
Loading…
Reference in a new issue