Skip to content

Commit

Permalink
fix: slice filter on negative begin, #117
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Apr 1, 2019
1 parent dba26f2 commit eadb6f3
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/builtin/filters/array.ts
Expand Up @@ -9,8 +9,8 @@ export default {
'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),
'slice': <T>(v: T[], begin: number, length: number): T[] => {
if (length === undefined) length = 1
'slice': <T>(v: T[], begin: number, length: number = 1): T[] => {
begin = begin < 0 ? v.length + begin : begin
return v.slice(begin, begin + length)
},
'uniq': function<T> (arr: T[]): T[] {
Expand Down
1 change: 1 addition & 0 deletions test/integration/builtin/filters/array.ts
Expand Up @@ -43,6 +43,7 @@ describe('filters/array', function () {
it('should slice third char by 2', () => test('{{ "Liquid" | slice: 2 }}', 'q'))
it('should slice substr by 2,5', () => test('{{ "Liquid" | slice: 2, 5 }}', 'quid'))
it('should slice substr by -3,2', () => test('{{ "Liquid" | slice: -3, 2 }}', 'ui'))
it('should slice substr by -2,2', () => test('{{ "abc" | slice: -2, 2 }}', 'bc'))
it('should support array', () => test('{{ "1,2,3,4" | split: "," | slice: 1,2 | join }}', '2 3'))
})
it('should support sort', function () {
Expand Down

0 comments on commit eadb6f3

Please sign in to comment.