Skip to content

Commit eadb6f3

Browse files
committed
fix: slice filter on negative begin, #117
1 parent dba26f2 commit eadb6f3

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

src/builtin/filters/array.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ export default {
99
'sort': <T>(v: T[], arg: (lhs: T, rhs: T) => number) => v.sort(arg),
1010
'size': (v: string | any[]) => v.length,
1111
'concat': <T1, T2>(v: T1[], arg: T2[] | T2): Array<T1 | T2> => Array.prototype.concat.call(v, arg),
12-
'slice': <T>(v: T[], begin: number, length: number): T[] => {
13-
if (length === undefined) length = 1
12+
'slice': <T>(v: T[], begin: number, length: number = 1): T[] => {
13+
begin = begin < 0 ? v.length + begin : begin
1414
return v.slice(begin, begin + length)
1515
},
1616
'uniq': function<T> (arr: T[]): T[] {

test/integration/builtin/filters/array.ts

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ describe('filters/array', function () {
4343
it('should slice third char by 2', () => test('{{ "Liquid" | slice: 2 }}', 'q'))
4444
it('should slice substr by 2,5', () => test('{{ "Liquid" | slice: 2, 5 }}', 'quid'))
4545
it('should slice substr by -3,2', () => test('{{ "Liquid" | slice: -3, 2 }}', 'ui'))
46+
it('should slice substr by -2,2', () => test('{{ "abc" | slice: -2, 2 }}', 'bc'))
4647
it('should support array', () => test('{{ "1,2,3,4" | split: "," | slice: 1,2 | join }}', '2 3'))
4748
})
4849
it('should support sort', function () {

0 commit comments

Comments
 (0)