Skip to content

Commit

Permalink
feat(filters): Implement basic compact support
Browse files Browse the repository at this point in the history
This was missed when adding support for `nil`.  `compact` removes `nil`s
from an array.  This does not implement the optional parameter to filter
out objects with a `nil` property.
  • Loading branch information
epage committed Jan 14, 2018
1 parent ebb4f40 commit c0eadd5
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,10 +515,15 @@ pub fn map(input: &Value, args: &[Value]) -> FilterResult {
pub fn compact(input: &Value, args: &[Value]) -> FilterResult {
check_args_len(args, 0, 1)?;

// No-op. liquid-rust doesn't have Null but this is left in for compatibility with existing
// shopofy/liquid code. Should it be a no-op, error, or not exist?
let array = input
.as_array()
.ok_or_else(|| FilterError::InvalidType("Array expected".to_owned()))?;

Ok(input.clone())
// TODO optional property parameter

let result: Vec<_> = array.iter().filter(|v| !v.is_nil()).cloned().collect();

Ok(Value::array(result))
}

pub fn replace(input: &Value, args: &[Value]) -> FilterResult {
Expand Down

0 comments on commit c0eadd5

Please # to comment.