Skip to content

Commit

Permalink
Add min, max function
Browse files Browse the repository at this point in the history
Update README
Increase version
  • Loading branch information
mitghi committed Mar 25, 2023
1 parent 198efb3 commit 68036dd
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "jetro"
version = "0.2.0"
version = "0.2.1"
edition = "2021"
authors = ["Milad (Mike) Taghavi <mitghi.at.gmail.com>"]
license = "MIT"
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ By convention, functions are denoted using `#` operator. Functions can be compos
| #keys | Keys associated with an object |
| #values | Values associated with an object |
| #reverse | Reverse the list |
| #min | Min value of numbers |
| #max | Max value of numbers |
| #all | Whether all boolean values are true |
| #sum | Sum of numbers |
| #formats('format with placeholder {} {}', 'key_a', 'key_b') [ -> \| ->* 'binding_value' ] | Insert formatted key:value into object or return it as single key:value |
Expand Down
70 changes: 70 additions & 0 deletions src/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,74 @@ impl Callable for Values {
}
}

struct Min;
impl Callable for Min {
fn call(
&mut self,
_func: &Func,
value: &Value,
_ctx: Option<&mut Context<'_>>,
) -> Result<Value, Error> {
match &value {
Value::Array(ref array) => {
let mut result = array
.iter()
.filter(|v| v.is_number())
.map(|v| v.as_f64().unwrap())
.collect::<Vec<f64>>();
result.sort_by(|a, b| a.partial_cmp(b).unwrap());
match result.get(0) {
Some(output) => {
return Ok(Value::Number(
serde_json::Number::from_f64(*output).unwrap(),
));
}
_ => {
return Ok(Value::Array(Vec::<Value>::new()));
}
}
}
_ => {
return Err(Error::FuncEval("expected value to be array".to_owned()));
}
}
}
}

struct Max;
impl Callable for Max {
fn call(
&mut self,
_func: &Func,
value: &Value,
_ctx: Option<&mut Context<'_>>,
) -> Result<Value, Error> {
match &value {
Value::Array(ref array) => {
let mut result = array
.iter()
.filter(|v| v.is_number())
.map(|v| v.as_f64().unwrap())
.collect::<Vec<f64>>();
result.sort_by(|a, b| b.partial_cmp(a).unwrap());
match result.get(0) {
Some(output) => {
return Ok(Value::Number(
serde_json::Number::from_f64(*output).unwrap(),
));
}
_ => {
return Ok(Value::Array(Vec::<Value>::new()));
}
}
}
_ => {
return Err(Error::FuncEval("expected value to be array".to_owned()));
}
}
}
}

impl Default for FuncRegistry {
fn default() -> Self {
let mut output = FuncRegistry::new();
Expand All @@ -386,6 +454,8 @@ impl Default for FuncRegistry {
output.register("map", Box::new(MapFn));
output.register("keys", Box::new(Keys));
output.register("values", Box::new(Values));
output.register("min", Box::new(Min));
output.register("max", Box::new(Max));
output
}
}
Expand Down

0 comments on commit 68036dd

Please sign in to comment.