mirror of
https://github.com/harttle/liquidjs.git
synced 2026-10-02 16:45:14 -07:00
refactor(filters): simplify join output-size accounting
Sum stringified element lengths in a single pass and keep the guarded Array.prototype.join for the result, instead of building an intermediate parts array. Co-authored-by: Cursor <[email protected]>
This commit is contained in:
@@ -8,16 +8,10 @@ import { EmptyDrop } from '../drop'
|
|||||||
export const join = argumentsToValue(function (this: FilterImpl, v: any[], arg: string) {
|
export const join = argumentsToValue(function (this: FilterImpl, v: any[], arg: string) {
|
||||||
const array = toArray(v)
|
const array = toArray(v)
|
||||||
const sep = isNil(arg) ? ' ' : stringify(arg)
|
const sep = isNil(arg) ? ' ' : stringify(arg)
|
||||||
const parts: string[] = []
|
let outputSize = sep.length * Math.max(array.length - 1, 0)
|
||||||
let outputSize = array.length > 0 ? sep.length * (array.length - 1) : 0
|
for (let i = 0; i < array.length; i++) outputSize += String(array[i]).length
|
||||||
for (let i = 0; i < array.length; i++) {
|
|
||||||
const item = array[i]
|
|
||||||
const part = isNil(item) ? '' : String(item)
|
|
||||||
outputSize += part.length
|
|
||||||
parts.push(part)
|
|
||||||
}
|
|
||||||
this.context.memoryLimit.use(outputSize)
|
this.context.memoryLimit.use(outputSize)
|
||||||
return parts.join(sep)
|
return Array.prototype.join.call(array, sep)
|
||||||
})
|
})
|
||||||
export const last = argumentsToValue(function (this: FilterImpl, v: any) {
|
export const last = argumentsToValue(function (this: FilterImpl, v: any) {
|
||||||
return isArrayLike(v) ? readArrayElement(v, -1, this.context.ownPropertyOnly) : ''
|
return isArrayLike(v) ? readArrayElement(v, -1, this.context.ownPropertyOnly) : ''
|
||||||
|
|||||||
Reference in New Issue
Block a user