Skip to content

Commit

Permalink
fix: sort filter unexpectedly modifies original array, #475
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Feb 20, 2022
1 parent ab5e245 commit dbc0497
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 2 deletions.
2 changes: 2 additions & 0 deletions docs/themes/navy/layout/partial/all-contributors.swig
Original file line number Diff line number Diff line change
Expand Up @@ -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://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/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>
</table>

Expand Down
2 changes: 1 addition & 1 deletion src/builtin/filters/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const reverse = (v: any[]) => [...v].reverse()

export function sort<T> (this: FilterImpl, arr: T[], property?: string) {
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)
rhs = getValue(rhs)
return lhs < rhs ? -1 : (lhs > rhs ? 1 : 0)
Expand Down
2 changes: 1 addition & 1 deletion src/template/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class Output extends TemplateImpl<OutputToken> implements Template {
super(token)
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)
emitter.write(val)
}
Expand Down
4 changes: 4 additions & 0 deletions test/integration/builtin/filters/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ describe('filters/array', function () {
const c = { name: { first: '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 () {
it('should uniq string list', function () {
Expand Down

0 comments on commit dbc0497

Please sign in to comment.