fix: "filter is not a function" for uniq

This commit is contained in:
Yang Jun
2024-08-23 21:27:53 +08:00
committed by Jun Yang
parent 2d59cff0a6
commit 68387c31ea
5 changed files with 22 additions and 14 deletions
+3 -3
View File
@@ -3,7 +3,7 @@ import { Drop } from '../drop/drop'
import { __assign } from 'tslib' import { __assign } from 'tslib'
import { NormalizedFullOptions, defaultOptions, RenderOptions } from '../liquid-options' import { NormalizedFullOptions, defaultOptions, RenderOptions } from '../liquid-options'
import { Scope } from './scope' import { Scope } from './scope'
import { isArray, isNil, isUndefined, isString, isFunction, toLiquid, InternalUndefinedVariableError, toValueSync, isObject, Limiter } from '../util' import { hasOwnProperty, isArray, isNil, isUndefined, isString, isFunction, toLiquid, InternalUndefinedVariableError, toValueSync, isObject, Limiter } from '../util'
type PropertyKey = string | number; type PropertyKey = string | number;
@@ -133,7 +133,7 @@ export function readProperty (obj: Scope, key: PropertyKey, ownPropertyOnly: boo
return value return value
} }
export function readJSProperty (obj: Scope, key: PropertyKey, ownPropertyOnly: boolean) { export function readJSProperty (obj: Scope, key: PropertyKey, ownPropertyOnly: boolean) {
if (ownPropertyOnly && !Object.hasOwnProperty.call(obj, key) && !(obj instanceof Drop)) return undefined if (ownPropertyOnly && !hasOwnProperty.call(obj, key) && !(obj instanceof Drop)) return undefined
return obj[key] return obj[key]
} }
@@ -148,7 +148,7 @@ function readLast (obj: Scope) {
} }
function readSize (obj: Scope) { function readSize (obj: Scope) {
if (obj.hasOwnProperty('size') || obj['size'] !== undefined) return obj['size'] if (hasOwnProperty.call(obj, 'size') || obj['size'] !== undefined) return obj['size']
if (isArray(obj) || isString(obj)) return obj.length if (isArray(obj) || isString(obj)) return obj.length
if (typeof obj === 'object') return Object.keys(obj).length if (typeof obj === 'object') return Object.keys(obj).length
} }
+3 -8
View File
@@ -1,4 +1,4 @@
import { toArray, argumentsToValue, toValue, stringify, caseInsensitiveCompare, isArray, isNil, last as arrayLast, hasOwnProperty } from '../util' import { toArray, argumentsToValue, toValue, stringify, caseInsensitiveCompare, isArray, isNil, last as arrayLast } from '../util'
import { equals, evalToken, isTruthy } from '../render' import { equals, evalToken, isTruthy } from '../render'
import { Value, FilterImpl } from '../template' import { Value, FilterImpl } from '../template'
import { Tokenizer } from '../parser' import { Tokenizer } from '../parser'
@@ -187,14 +187,9 @@ export function * find_exp<T extends object> (this: FilterImpl, arr: T[], itemNa
} }
export function uniq<T> (this: FilterImpl, arr: T[]): T[] { export function uniq<T> (this: FilterImpl, arr: T[]): T[] {
arr = toValue(arr) arr = toArray(arr)
this.context.memoryLimit.use(arr.length) this.context.memoryLimit.use(arr.length)
const u = {} return [...new Set(arr)]
return (arr || []).filter(val => {
if (hasOwnProperty.call(u, String(val))) return false
u[String(val)] = true
return true
})
} }
export function sample<T> (this: FilterImpl, v: T[] | string, count = 1): T | string | (T | string)[] { export function sample<T> (this: FilterImpl, v: T[] | string, count = 1): T | string | (T | string)[] {
+3
View File
@@ -1,4 +1,5 @@
import { assert } from './assert' import { assert } from './assert'
import { toNumber } from './underscore'
export class Limiter { export class Limiter {
private message: string private message: string
@@ -9,10 +10,12 @@ export class Limiter {
this.limit = limit this.limit = limit
} }
use (count: number) { use (count: number) {
count = toNumber(count)
assert(this.base + count <= this.limit, this.message) assert(this.base + count <= this.limit, this.message)
this.base += count this.base += count
} }
check (count: number) { check (count: number) {
count = toNumber(count)
assert(count <= this.limit, this.message) assert(count <= this.limit, this.message)
} }
} }
+5
View File
@@ -66,6 +66,11 @@ export function toValue (value: any): any {
return (value instanceof Drop && isFunction(value.valueOf)) ? value.valueOf() : value return (value instanceof Drop && isFunction(value.valueOf)) ? value.valueOf() : value
} }
export function toNumber (value: any): number {
value = Number(value)
return isNaN(value) ? 0 : value
}
export function isNumber (value: any): value is number { export function isNumber (value: any): value is number {
return typeof value === 'number' return typeof value === 'number'
} }
+8 -3
View File
@@ -496,9 +496,14 @@ describe('Issues', function () {
expect(() => liquid.parse({} as any)).not.toThrow() expect(() => liquid.parse({} as any)).not.toThrow()
}) })
it('Unexpected "RenderError: memory alloc limit exceeded" #737', () => { it('Unexpected "RenderError: memory alloc limit exceeded" #737', () => {
const liquid = new Liquid(); const liquid = new Liquid()
const context = { x: ["a", "b"] }; const context = { x: ['a', 'b'] }
const template = "{{ x | join: 5 }}" const template = '{{ x | join: 5 }}'
expect(liquid.parseAndRender(template, context)).resolves.toEqual('a5b') expect(liquid.parseAndRender(template, context)).resolves.toEqual('a5b')
}) })
it('{{ 123 | uniq }} throws #737', () => {
const liquid = new Liquid()
expect(liquid.parseAndRender('{{ 113 | uniq }}')).resolves.toEqual('113')
expect(liquid.parseAndRender("{{ '113' | uniq }}")).resolves.toEqual('113')
})
}) })