Skip to content

Commit dbc0497

Browse files
committed
fix: sort filter unexpectedly modifies original array, #475
1 parent ab5e245 commit dbc0497

File tree

4 files changed

+8
-2
lines changed

4 files changed

+8
-2
lines changed

docs/themes/navy/layout/partial/all-contributors.swig

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
<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>
5454
<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>
5555
<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>
56+
<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>
57+
<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>
5658
</tr>
5759
</table>
5860

src/builtin/filters/array.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const reverse = (v: any[]) => [...v].reverse()
1212

1313
export function sort<T> (this: FilterImpl, arr: T[], property?: string) {
1414
const getValue = (obj: Scope) => property ? this.context.getFromScope(obj, property.split('.')) : obj
15-
return toArray(arr).sort((lhs, rhs) => {
15+
return [...toArray(arr)].sort((lhs, rhs) => {
1616
lhs = getValue(lhs)
1717
rhs = getValue(rhs)
1818
return lhs < rhs ? -1 : (lhs > rhs ? 1 : 0)

src/template/output.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export class Output extends TemplateImpl<OutputToken> implements Template {
1212
super(token)
1313
this.value = new Value(token.content, liquid)
1414
}
15-
public * render (ctx: Context, emitter: Emitter): Generator<unknown, void, unknown> {
15+
public * render (ctx: Context, emitter: Emitter): IterableIterator<unknown> {
1616
const val = yield this.value.value(ctx, false)
1717
emitter.write(val)
1818
}

test/integration/builtin/filters/array.ts

+4
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ describe('filters/array', function () {
138138
const c = { name: { first: 'Carol' } }
139139
return test(tpl, { arr: [b, c, a, c] }, 'Alice Bob Carol Carol')
140140
})
141+
it('should not change the original array', () => {
142+
const arr = ["one", "two", "three", "four", "five"]
143+
return test("{{arr | sort}} {{arr}}", { arr }, 'fivefouronethreetwo onetwothreefourfive')
144+
})
141145
})
142146
describe('uniq', function () {
143147
it('should uniq string list', function () {

0 commit comments

Comments
 (0)