From 9da41c8a37d9c49ee4a16d5bd520fd6ae01c14ec Mon Sep 17 00:00:00 2001 From: Martin Schuster Date: Fri, 28 Jan 2022 15:43:38 +0100 Subject: [PATCH] fix: where-filter null handling (#457) --- src/builtin/filters/array.ts | 5 ++++- test/integration/builtin/filters/array.ts | 12 ++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/builtin/filters/array.ts b/src/builtin/filters/array.ts index 5da7b201d..ade6e1128 100644 --- a/src/builtin/filters/array.ts +++ b/src/builtin/filters/array.ts @@ -3,6 +3,7 @@ import { toArray } from '../../util/collection' import { isTruthy } from '../../render/boolean' import { FilterImpl } from '../../template/filter/filter-impl' import { Scope } from '../../context/scope' +import { isComparable } from '../../drop/comparable' export const join = (v: any[], arg: string) => v.join(arg === undefined ? ' ' : arg) export const last = (v: any) => isArray(v) ? arrayLast(v) : '' @@ -40,7 +41,9 @@ export function slice (v: T[], begin: number, length = 1): T[] { export function where (this: FilterImpl, arr: T[], property: string, expected?: any): T[] { return toArray(arr).filter(obj => { const value = this.context.getFromScope(obj, String(property).split('.')) - return expected === undefined ? isTruthy(value, this.context) : value === expected + if (expected === undefined) return isTruthy(value, this.context) + if (isComparable(expected)) return expected.equals(value) + return value === expected }) } diff --git a/test/integration/builtin/filters/array.ts b/test/integration/builtin/filters/array.ts index 8563c18c1..3561eb329 100644 --- a/test/integration/builtin/filters/array.ts +++ b/test/integration/builtin/filters/array.ts @@ -183,6 +183,18 @@ describe('filters/array', function () { - Boring sneakers `) }) + it('should support filter by null property', function () { + return test(`{% assign untyped_products = products | where: "type", null %} + Untyped products: + {% for product in untyped_products -%} + - {{ product.title }} + {% endfor %}`, { products }, ` + Untyped products: + - Coffee mug + - Limited edition sneakers + - Boring sneakers + `) + }) it('should support nested property', async function () { const authors = [ { name: 'Alice', books: { year: 2019 } },