forked from extern/nushell
add some more division for units (#783)
This commit is contained in:
parent
33ffb2c39a
commit
45b3592739
@ -115,6 +115,8 @@ pub fn math_result_type(
|
||||
(Type::Float, Type::Int) => (Type::Float, None),
|
||||
(Type::Int, Type::Float) => (Type::Float, None),
|
||||
(Type::Float, Type::Float) => (Type::Float, None),
|
||||
(Type::Filesize, Type::Filesize) => (Type::Float, None),
|
||||
(Type::Duration, Type::Duration) => (Type::Float, None),
|
||||
|
||||
(Type::Unknown, _) => (Type::Unknown, None),
|
||||
(_, Type::Unknown) => (Type::Unknown, None),
|
||||
|
@ -1110,6 +1110,40 @@ impl Value {
|
||||
Err(ShellError::DivisionByZero(op))
|
||||
}
|
||||
}
|
||||
(Value::Filesize { val: lhs, .. }, Value::Filesize { val: rhs, .. }) => {
|
||||
if *rhs != 0 {
|
||||
if lhs % rhs == 0 {
|
||||
Ok(Value::Int {
|
||||
val: lhs / rhs,
|
||||
span,
|
||||
})
|
||||
} else {
|
||||
Ok(Value::Float {
|
||||
val: (*lhs as f64) / (*rhs as f64),
|
||||
span,
|
||||
})
|
||||
}
|
||||
} else {
|
||||
Err(ShellError::DivisionByZero(op))
|
||||
}
|
||||
}
|
||||
(Value::Duration { val: lhs, .. }, Value::Duration { val: rhs, .. }) => {
|
||||
if *rhs != 0 {
|
||||
if lhs % rhs == 0 {
|
||||
Ok(Value::Int {
|
||||
val: lhs / rhs,
|
||||
span,
|
||||
})
|
||||
} else {
|
||||
Ok(Value::Float {
|
||||
val: (*lhs as f64) / (*rhs as f64),
|
||||
span,
|
||||
})
|
||||
}
|
||||
} else {
|
||||
Err(ShellError::DivisionByZero(op))
|
||||
}
|
||||
}
|
||||
(Value::CustomValue { val: lhs, span }, rhs) => {
|
||||
lhs.operation(*span, Operator::Divide, op, rhs)
|
||||
}
|
||||
|
@ -148,3 +148,13 @@ fn proper_variable_captures_with_nesting() -> TestResult {
|
||||
fn proper_variable_for() -> TestResult {
|
||||
run_test(r#"for x in 1..3 { if $x == 2 { "bob" } } | get 1"#, "bob")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn divide_duration() -> TestResult {
|
||||
run_test(r#"4ms / 4ms"#, "1")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn divide_filesize() -> TestResult {
|
||||
run_test(r#"4mb / 4mb"#, "1")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user