fix: sort filter unexpectedly modifies original array, #475

This commit is contained in:
Harttle
2022-02-20 13:03:40 +08:00
parent ab5e245fba
commit dbc0497386
4 changed files with 8 additions and 2 deletions
+2
View File
@@ -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>
+1 -1
View File
@@ -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)
+1 -1
View File
@@ -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)
}
@@ -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 () {