diff --git a/lib/src/fnc/array.rs b/lib/src/fnc/array.rs index 5831a871..8d68d79f 100644 --- a/lib/src/fnc/array.rs +++ b/lib/src/fnc/array.rs @@ -23,6 +23,16 @@ pub fn any((arg,): (Value,)) -> Result { } } +pub fn append((array, value): (Value, Value)) -> Result { + match array { + Value::Array(mut v) => { + v.push(value); + Ok(v.into()) + } + _ => Ok(Value::None), + } +} + pub fn combine(arrays: (Value, Value)) -> Result { Ok(match arrays { (Value::Array(v), Value::Array(w)) => v.combine(w).into(), @@ -86,6 +96,7 @@ pub fn insert((array, value, index): (Value, Value, Option)) -> Result { @@ -124,6 +135,65 @@ pub fn min((arg,): (Value,)) -> Result { } } +pub fn pop((arg,): (Value,)) -> Result { + match arg { + Value::Array(mut v) => Ok(v.pop().into()), + _ => Ok(Value::None), + } +} + +pub fn prepend((array, value): (Value, Value)) -> Result { + match array { + Value::Array(mut v) => { + v.insert(0, value); + Ok(v.into()) + } + _ => Ok(Value::None), + } +} + +pub fn push((array, value): (Value, Value)) -> Result { + match array { + Value::Array(mut v) => { + v.push(value); + Ok(v.into()) + } + _ => Ok(Value::None), + } +} + +pub fn remove((array, index): (Value, Value)) -> Result { + match (array, index) { + (Value::Array(mut v), Value::Number(i)) => { + let mut i = i.as_int(); + // Negative index means start from the back + if i < 0 { + i += v.len() as i64; + } + // Invalid index so return array unaltered + if i > v.len() as i64 || i < 0 { + return Ok(v.into()); + } + // Remove the value from the array + v.remove(i as usize); + // Return the array + Ok(v.into()) + } + (Value::Array(v), _) => Ok(v.into()), + (_, _) => Ok(Value::None), + } +} + +pub fn reverse((arg,): (Value,)) -> Result { + match arg { + Value::Array(mut v) => { + v.reverse(); + Ok(v.into()) + } + _ => Ok(Value::None), + } +} + pub fn sort((array, order): (Value, Option)) -> Result { match array { Value::Array(mut v) => match order { diff --git a/lib/src/fnc/mod.rs b/lib/src/fnc/mod.rs index 6dd24223..ea8ec2af 100644 --- a/lib/src/fnc/mod.rs +++ b/lib/src/fnc/mod.rs @@ -68,6 +68,7 @@ pub fn synchronous(ctx: &Context<'_>, name: &str, args: Vec) -> Result array::all, "array::any" => array::any, + "array::append" => array::append, "array::combine" => array::combine, "array::complement" => array::complement, "array::concat" => array::concat, @@ -80,6 +81,11 @@ pub fn synchronous(ctx: &Context<'_>, name: &str, args: Vec) -> Result array::len, "array::max" => array::max, "array::min" => array::min, + "array::pop" => array::pop, + "array::prepend" => array::prepend, + "array::push" => array::push, + "array::remove" => array::remove, + "array::reverse" => array::reverse, "array::sort" => array::sort, "array::union" => array::union, "array::sort::asc" => array::sort::asc, diff --git a/lib/src/sql/function.rs b/lib/src/sql/function.rs index ef17e3b1..7965a765 100644 --- a/lib/src/sql/function.rs +++ b/lib/src/sql/function.rs @@ -302,24 +302,34 @@ fn function_names(i: &str) -> IResult<&str, &str> { fn function_array(i: &str) -> IResult<&str, &str> { alt(( - tag("array::all"), - tag("array::any"), - tag("array::combine"), - tag("array::complement"), - tag("array::concat"), - tag("array::difference"), - tag("array::distinct"), - tag("array::flatten"), - tag("array::group"), - tag("array::insert"), - tag("array::intersect"), - tag("array::len"), - tag("array::max"), - tag("array::min"), - tag("array::sort::asc"), - tag("array::sort::desc"), - tag("array::sort"), - tag("array::union"), + alt(( + tag("array::all"), + tag("array::any"), + tag("array::append"), + tag("array::combine"), + tag("array::complement"), + tag("array::concat"), + tag("array::difference"), + tag("array::distinct"), + tag("array::flatten"), + tag("array::group"), + tag("array::insert"), + )), + alt(( + tag("array::intersect"), + tag("array::len"), + tag("array::max"), + tag("array::min"), + tag("array::pop"), + tag("array::prepend"), + tag("array::push"), + tag("array::remove"), + tag("array::reverse"), + tag("array::sort::asc"), + tag("array::sort::desc"), + tag("array::sort"), + tag("array::union"), + )), ))(i) }