Repository: MiSawa/xq (rust)

Issue:
length filter on numbers should be absolute values
Currently xq returns the number value as it is but should take the absolute values.
```sh
❯ echo -128 | jq length
128

❯ echo -128 | xq length
-128

❯ xq --version
xq 0.1.4-01d8350a1d5a9f885b2b8b5c1222eb04b742ccb3
```


Pull request:
Fix length on negative number
Fixes #24 

Patch:
diff --git a/src/intrinsic/mod.rs b/src/intrinsic/mod.rs
index bb473d0..5f331bf 100644
--- a/src/intrinsic/mod.rs
+++ b/src/intrinsic/mod.rs
@@ -9,7 +9,7 @@ use crate::{
     Array, Value,
 };
 use itertools::Itertools;
-use num::ToPrimitive;
+use num::{Float, ToPrimitive};
 use phf::phf_map;
 use std::rc::Rc;
 
@@ -121,7 +121,7 @@ fn length(context: Value) -> Result<Value> {
     Ok(Value::number(match context {
         Value::Null => 0,
         Value::Boolean(_) => return Err(QueryExecutionError::InvalidArgType("length", context)),
-        Value::Number(_) => return Ok(context),
+        Value::Number(n) => return Ok(n.abs().into()),
         Value::String(s) => s.chars().count(),
         Value::Array(a) => a.len(),
         Value::Object(o) => o.len(),
@@ -279,4 +279,4 @@ fn ends_with(context: Value, s: Value) -> Result<Value> {
         (Value::String(lhs), Value::String(rhs)) => Ok(lhs.ends_with(rhs.as_ref()).into()),
         (context, _) => Err(QueryExecutionError::InvalidArgType("endswith", context)),
     }
-}
\ No newline at end of file
+}


Test patch:
diff --git a/tests/hand_written/mod.rs b/tests/hand_written/mod.rs
index 46f52a3..57979f2 100644
--- a/tests/hand_written/mod.rs
+++ b/tests/hand_written/mod.rs
@@ -84,3 +84,16 @@ test!(
     {"a": "a", "b": "b", "c": "c", "d": "d", "e": "e"}
     "#
 );
+
+test!(
+    length_on_number_yields_abs_value,
+    r#"
+    map(length)
+    "#,
+    r#"
+    [0, 128, -128, -1.5]
+    "#,
+    r#"
+    [0, 128, 128, 1.5]
+    "#
+);
\ No newline at end of file