Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
190 changes: 190 additions & 0 deletions Fluid.Tests/ArrayFiltersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -659,5 +659,195 @@ public async Task SumWithDecimalsAndArguments(string filterArgument, decimal exp

Assert.Equal(expectedValue, result.ToNumberValue());
}

[Fact]
public async Task Reject()
{
var input = new ArrayValue(new[] {
new ObjectValue(new { Title = "a", Pinned = true }),
new ObjectValue(new { Title = "b", Pinned = false }),
new ObjectValue(new { Title = "c", Pinned = true })
});

var options = new TemplateOptions();
var context = new TemplateContext(options);
options.MemberAccessStrategy.Register(new { Title = "a", Pinned = true }.GetType());

var arguments1 = new FilterArguments().Add(new StringValue("Pinned"));

var result1 = await ArrayFilters.Reject(input, arguments1, context);

Assert.Single(result1.Enumerate(context));

var arguments2 = new FilterArguments()
.Add(new StringValue("Pinned"))
.Add(BooleanValue.Create(false))
;

var result2 = await ArrayFilters.Reject(input, arguments2, context);

Assert.Equal(2, result2.Enumerate(context).Count());

var arguments3 = new FilterArguments()
.Add(new StringValue("Title"))
.Add(new StringValue("c"));

var result3 = await ArrayFilters.Reject(input, arguments3, context);

Assert.Equal(2, result3.Enumerate(context).Count());
}

[Fact]
public async Task Find()
{
var input = new ArrayValue(new[] {
new ObjectValue(new { Title = "a", Pinned = true }),
new ObjectValue(new { Title = "b", Pinned = false }),
new ObjectValue(new { Title = "c", Pinned = true })
});

var options = new TemplateOptions();
var context = new TemplateContext(options);
options.MemberAccessStrategy.Register(new { Title = "a", Pinned = true }.GetType());

var arguments1 = new FilterArguments().Add(new StringValue("Pinned")).Add(BooleanValue.True);

var result1 = await ArrayFilters.Find(input, arguments1, context);

Assert.Equal(input.Values[0], result1);

var arguments2 = new FilterArguments()
.Add(new StringValue("Pinned"))
.Add(BooleanValue.Create(false))
;

var result2 = await ArrayFilters.Find(input, arguments2, context);

Assert.Equal(input.Values[1], result2);

var arguments3 = new FilterArguments()
.Add(new StringValue("Title"))
.Add(new StringValue("c"));

var result3 = await ArrayFilters.Find(input, arguments3, context);

Assert.Equal(input.Values[2], result3);

var arguments4 = new FilterArguments();

var result4 = await ArrayFilters.Find(input, arguments4, context);

Assert.Equal(NilValue.Instance, result4);

var arguments5 = new FilterArguments()
.Add(new StringValue("Title"))
.Add(new StringValue("d"));

var result5 = await ArrayFilters.Find(input, arguments5, context);

Assert.Equal(NilValue.Instance, result5);
}

[Fact]
public async Task FindIndex()
{
var input = new ArrayValue(new[] {
new ObjectValue(new { Title = "a", Pinned = true }),
new ObjectValue(new { Title = "b", Pinned = false }),
new ObjectValue(new { Title = "c", Pinned = true })
});

var options = new TemplateOptions();
var context = new TemplateContext(options);
options.MemberAccessStrategy.Register(new { Title = "a", Pinned = true }.GetType());

var arguments1 = new FilterArguments().Add(new StringValue("Pinned")).Add(BooleanValue.True);

var result1 = await ArrayFilters.FindIndex(input, arguments1, context);

Assert.Equal(0, result1.ToNumberValue());

var arguments2 = new FilterArguments()
.Add(new StringValue("Pinned"))
.Add(BooleanValue.Create(false))
;

var result2 = await ArrayFilters.FindIndex(input, arguments2, context);

Assert.Equal(1, result2.ToNumberValue());

var arguments3 = new FilterArguments()
.Add(new StringValue("Title"))
.Add(new StringValue("c"));

var result3 = await ArrayFilters.FindIndex(input, arguments3, context);

Assert.Equal(2, result3.ToNumberValue());

var arguments4 = new FilterArguments();

var result4 = await ArrayFilters.FindIndex(input, arguments4, context);

Assert.Equal(NilValue.Instance, result4);

var arguments5 = new FilterArguments()
.Add(new StringValue("Title"))
.Add(new StringValue("d"));

var result5 = await ArrayFilters.FindIndex(input, arguments5, context);

Assert.Equal(NilValue.Instance, result5);
}

[Fact]
public async Task Has()
{
var input = new ArrayValue(new[] {
new ObjectValue(new { Title = "a", Pinned = true }),
new ObjectValue(new { Title = "b", Pinned = false }),
new ObjectValue(new { Title = "c", Pinned = true })
});

var options = new TemplateOptions();
var context = new TemplateContext(options);
options.MemberAccessStrategy.Register(new { Title = "a", Pinned = true }.GetType());

var arguments1 = new FilterArguments().Add(new StringValue("Pinned")).Add(BooleanValue.True);

var result1 = await ArrayFilters.Has(input, arguments1, context);

Assert.Equal(BooleanValue.True, result1);

var arguments2 = new FilterArguments()
.Add(new StringValue("Pinned"))
.Add(BooleanValue.Create(false))
;

var result2 = await ArrayFilters.Has(input, arguments2, context);

Assert.Equal(BooleanValue.True, result2);

var arguments3 = new FilterArguments()
.Add(new StringValue("Title"))
.Add(new StringValue("c"));

var result3 = await ArrayFilters.Has(input, arguments3, context);

Assert.Equal(BooleanValue.True, result3);

var arguments4 = new FilterArguments();

var result4 = await ArrayFilters.Has(input, arguments4, context);

Assert.Equal(BooleanValue.False, result4);

var arguments5 = new FilterArguments()
.Add(new StringValue("Title"))
.Add(new StringValue("d"));

var result5 = await ArrayFilters.Has(input, arguments5, context);

Assert.Equal(BooleanValue.False, result5);
}
}
}
114 changes: 113 additions & 1 deletion Fluid/Filters/ArrayFilters.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Fluid.Values;
using Fluid.Values;

namespace Fluid.Filters
{
Expand All @@ -18,6 +18,10 @@ public static FilterCollection WithArrayFilters(this FilterCollection filters)
filters.AddFilter("uniq", Uniq);
filters.AddFilter("where", Where);
filters.AddFilter("sum", Sum);
filters.AddFilter("find", Find);
filters.AddFilter("find_index", FindIndex);
filters.AddFilter("has", Has);
filters.AddFilter("reject", Reject);
return filters;
}

Expand Down Expand Up @@ -162,6 +166,114 @@ public static async ValueTask<FluidValue> Where(FluidValue input, FilterArgument
return new ArrayValue(list);
}

public static async ValueTask<FluidValue> Find(FluidValue input, FilterArguments arguments, TemplateContext context)
{
if (input.Type != FluidValues.Array)
{
return input;
}

// First argument is the property name to match
var member = arguments.At(0).ToStringValue();

// Second argument is the value to match
var targetValue = arguments.At(1);
if (targetValue.IsNil())
{
return NilValue.Instance;
}

FluidValue result = NilValue.Instance;

foreach (var item in input.Enumerate(context))
{
var itemValue = await item.GetValueAsync(member, context);

if (targetValue.Equals(itemValue))
{
result = item;
break;
}
}

return result;
}

public static async ValueTask<FluidValue> FindIndex(FluidValue input, FilterArguments arguments, TemplateContext context)
{
if (input.Type != FluidValues.Array)
{
return input;
}

// First argument is the property name to match
var member = arguments.At(0).ToStringValue();

// Second argument is the value to match
var targetValue = arguments.At(1);
if (targetValue.IsNil())
{
return NilValue.Instance;
}

FluidValue result = NilValue.Instance;
var index = 0;

foreach (var item in input.Enumerate(context))
{
var itemValue = await item.GetValueAsync(member, context);

if (targetValue.Equals(itemValue))
{
result = NumberValue.Create(index);
break;
}

index++;
}

return result;
}

public static async ValueTask<FluidValue> Has(FluidValue input, FilterArguments arguments, TemplateContext context)
{
var result = await Find(input, arguments, context);

return result.Equals(NilValue.Instance) ? BooleanValue.False : BooleanValue.True;
}

public static async ValueTask<FluidValue> Reject(FluidValue input, FilterArguments arguments, TemplateContext context)
{
if (input.Type != FluidValues.Array)
{
return input;
}

// First argument is the property name to match
var member = arguments.At(0).ToStringValue();

// Second argument is the value to not match, or 'true' if none is defined
var targetValue = arguments.At(1);
if (targetValue.IsNil())
{
targetValue = BooleanValue.True;
}

var list = new List<FluidValue>();

foreach (var item in input.Enumerate(context))
{
var itemValue = await item.GetValueAsync(member, context);

if (!targetValue.Equals(itemValue))
{
list.Add(item);
}
}

return new ArrayValue(list);
}

public static ValueTask<FluidValue> Size(FluidValue input, FilterArguments arguments, TemplateContext context)
{
return input.GetValueAsync("size", context);
Expand Down