Skip to content

Commit 9da41c8

Browse files
authored
fix: where-filter null handling (#457)
1 parent 2717dae commit 9da41c8

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/builtin/filters/array.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { toArray } from '../../util/collection'
33
import { isTruthy } from '../../render/boolean'
44
import { FilterImpl } from '../../template/filter/filter-impl'
55
import { Scope } from '../../context/scope'
6+
import { isComparable } from '../../drop/comparable'
67

78
export const join = (v: any[], arg: string) => v.join(arg === undefined ? ' ' : arg)
89
export const last = (v: any) => isArray(v) ? arrayLast(v) : ''
@@ -40,7 +41,9 @@ export function slice<T> (v: T[], begin: number, length = 1): T[] {
4041
export function where<T extends object> (this: FilterImpl, arr: T[], property: string, expected?: any): T[] {
4142
return toArray(arr).filter(obj => {
4243
const value = this.context.getFromScope(obj, String(property).split('.'))
43-
return expected === undefined ? isTruthy(value, this.context) : value === expected
44+
if (expected === undefined) return isTruthy(value, this.context)
45+
if (isComparable(expected)) return expected.equals(value)
46+
return value === expected
4447
})
4548
}
4649

test/integration/builtin/filters/array.ts

+12
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,18 @@ describe('filters/array', function () {
183183
- Boring sneakers
184184
`)
185185
})
186+
it('should support filter by null property', function () {
187+
return test(`{% assign untyped_products = products | where: "type", null %}
188+
Untyped products:
189+
{% for product in untyped_products -%}
190+
- {{ product.title }}
191+
{% endfor %}`, { products }, `
192+
Untyped products:
193+
- Coffee mug
194+
- Limited edition sneakers
195+
- Boring sneakers
196+
`)
197+
})
186198
it('should support nested property', async function () {
187199
const authors = [
188200
{ name: 'Alice', books: { year: 2019 } },

0 commit comments

Comments
 (0)