Skip to content

Commit

Permalink
fix: reverse filter not pure, see #126
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed May 19, 2019
1 parent 224679a commit 505c408
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/builtin/filters/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default {
'last': <T>(v: T[]): T => last(v),
'first': <T>(v: T[]): T => v[0],
'map': <T1, T2>(arr: {[key: string]: T1}[], arg: string): T1[] => arr.map(v => v[arg]),
'reverse': (v: any[]) => v.reverse(),
'reverse': (v: any[]) => [...v].reverse(),
'sort': <T>(v: T[], arg: (lhs: T, rhs: T) => number) => v.sort(arg),
'size': (v: string | any[]) => v.length,
'concat': <T1, T2>(v: T1[], arg: T2[] | T2): Array<T1 | T2> => Array.prototype.concat.call(v, arg),
Expand Down
20 changes: 17 additions & 3 deletions test/integration/builtin/filters/array.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { test } from '../../../stub/render'
import Liquid from '../../../../src/liquid'
import {expect} from 'chai'

describe('filters/array', function () {
let liquid: Liquid
beforeEach(function () {
liquid = new Liquid()
})
describe('join', function () {
it('should support join', function () {
const src = '{% assign beatles = "John, Paul, George, Ringo" | split: ", " %}' +
Expand All @@ -21,9 +27,17 @@ describe('filters/array', function () {
it('should support map', function () {
return test('{{posts | map: "category"}}', 'foo,bar')
})
it('should support reverse', function () {
return test('{{ "Ground control to Major Tom." | split: "" | reverse | join: "" }}',
'.moT rojaM ot lortnoc dnuorG')
describe('reverse', function () {
it('should support reverse', async function () {
const html = await liquid.parseAndRender('{{ "Ground control to Major Tom." | split: "" | reverse | join: "" }}')
expect(html).to.equal('.moT rojaM ot lortnoc dnuorG')
})
it('should be pure', async function () {
const scope = {arr: ['a', 'b', 'c']}
await liquid.parseAndRender('{{ arr | reverse | join: "" }}', scope)
const html = await liquid.parseAndRender('{{ arr | join: "" }}', scope)
expect(html).to.equal('abc')
})
})
describe('size', function () {
it('should return string length',
Expand Down

0 comments on commit 505c408

Please sign in to comment.