mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 12:20:40 -07:00
fix: sort filter unexpectedly modifies original array, #475
This commit is contained in:
@@ -53,6 +53,8 @@
|
|||||||
<td align="center"><a href="https://www.aleksandrhovhannisyan.com/"><img src="https://avatars.githubusercontent.com/u/19352442?v=4?s=100" width="100px;" alt=""/></a></td>
|
<td align="center"><a href="https://www.aleksandrhovhannisyan.com/"><img src="https://avatars.githubusercontent.com/u/19352442?v=4?s=100" width="100px;" alt=""/></a></td>
|
||||||
<td align="center"><a href="https://github.com/jg-rp"><img src="https://avatars.githubusercontent.com/u/72664870?v=4?s=100" width="100px;" alt=""/></a></td>
|
<td align="center"><a href="https://github.com/jg-rp"><img src="https://avatars.githubusercontent.com/u/72664870?v=4?s=100" width="100px;" alt=""/></a></td>
|
||||||
<td align="center"><a href="https://github.com/ameyaapte1"><img src="https://avatars.githubusercontent.com/u/16054747?v=4?s=100" width="100px;" alt=""/></a></td>
|
<td align="center"><a href="https://github.com/ameyaapte1"><img src="https://avatars.githubusercontent.com/u/16054747?v=4?s=100" width="100px;" alt=""/></a></td>
|
||||||
|
<td align="center"><a href="https://github.com/tbdrz"><img src="https://avatars.githubusercontent.com/u/50599116?v=4?s=100" width="100px;" alt=""/></a></td>
|
||||||
|
<td align="center"><a href="http://santialbo.com"><img src="https://avatars.githubusercontent.com/u/1557563?v=4?s=100" width="100px;" alt=""/></a></td>
|
||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ export const reverse = (v: any[]) => [...v].reverse()
|
|||||||
|
|
||||||
export function sort<T> (this: FilterImpl, arr: T[], property?: string) {
|
export function sort<T> (this: FilterImpl, arr: T[], property?: string) {
|
||||||
const getValue = (obj: Scope) => property ? this.context.getFromScope(obj, property.split('.')) : obj
|
const getValue = (obj: Scope) => property ? this.context.getFromScope(obj, property.split('.')) : obj
|
||||||
return toArray(arr).sort((lhs, rhs) => {
|
return [...toArray(arr)].sort((lhs, rhs) => {
|
||||||
lhs = getValue(lhs)
|
lhs = getValue(lhs)
|
||||||
rhs = getValue(rhs)
|
rhs = getValue(rhs)
|
||||||
return lhs < rhs ? -1 : (lhs > rhs ? 1 : 0)
|
return lhs < rhs ? -1 : (lhs > rhs ? 1 : 0)
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ export class Output extends TemplateImpl<OutputToken> implements Template {
|
|||||||
super(token)
|
super(token)
|
||||||
this.value = new Value(token.content, liquid)
|
this.value = new Value(token.content, liquid)
|
||||||
}
|
}
|
||||||
public * render (ctx: Context, emitter: Emitter): Generator<unknown, void, unknown> {
|
public * render (ctx: Context, emitter: Emitter): IterableIterator<unknown> {
|
||||||
const val = yield this.value.value(ctx, false)
|
const val = yield this.value.value(ctx, false)
|
||||||
emitter.write(val)
|
emitter.write(val)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -138,6 +138,10 @@ describe('filters/array', function () {
|
|||||||
const c = { name: { first: 'Carol' } }
|
const c = { name: { first: 'Carol' } }
|
||||||
return test(tpl, { arr: [b, c, a, c] }, 'Alice Bob Carol Carol')
|
return test(tpl, { arr: [b, c, a, c] }, 'Alice Bob Carol Carol')
|
||||||
})
|
})
|
||||||
|
it('should not change the original array', () => {
|
||||||
|
const arr = ["one", "two", "three", "four", "five"]
|
||||||
|
return test("{{arr | sort}} {{arr}}", { arr }, 'fivefouronethreetwo onetwothreefourfive')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
describe('uniq', function () {
|
describe('uniq', function () {
|
||||||
it('should uniq string list', function () {
|
it('should uniq string list', function () {
|
||||||
|
|||||||
Reference in New Issue
Block a user