diff --git a/src/filters.js b/src/filters.js index 874e444fc..29c7fb7d0 100644 --- a/src/filters.js +++ b/src/filters.js @@ -40,7 +40,7 @@ const filters = { 'escape_once': str => escape(unescape(str)), 'first': v => v[0], 'floor': v => Math.floor(v), - 'join': (v, arg) => v.join(arg), + 'join': (v, arg) => v.join(arg === undefined ? ' ' : arg), 'last': v => _.last(v), 'lstrip': v => stringify(v).replace(/^\s+/, ''), 'map': (arr, arg) => arr.map(v => v[arg]), @@ -61,8 +61,10 @@ const filters = { }, 'rstrip': str => stringify(str).replace(/\s+$/, ''), 'size': v => v.length, - 'slice': (v, begin, length) => - v.substr(begin, length === undefined ? 1 : length), + 'slice': (v, begin, length) => { + if (length === undefined) length = 1 + return v.slice(begin, begin + length) + }, 'sort': (v, arg) => v.sort(arg), 'split': (v, arg) => stringify(v).split(arg), 'strip': (v) => stringify(v).trim(), diff --git a/test/unit/filters.js b/test/unit/filters.js index 612e2e2f0..c508c3370 100644 --- a/test/unit/filters.js +++ b/test/unit/filters.js @@ -154,10 +154,17 @@ describe('filters', function () { it('should return "3" for 3.5', () => test('{{ "3.5" | floor }}', '3')) }) - it('should support join', function () { - const src = '{% assign beatles = "John, Paul, George, Ringo" | split: ", " %}' + - '{{ beatles | join: " and " }}' - return test(src, 'John and Paul and George and Ringo') + describe('join', function () { + it('should support join', function () { + const src = '{% assign beatles = "John, Paul, George, Ringo" | split: ", " %}' + + '{{ beatles | join: " and " }}' + return test(src, 'John and Paul and George and Ringo') + }) + it('should default separator to space', function () { + const src = '{% assign beatles = "John, Paul, George, Ringo" | split: ", " %}' + + '{{ beatles | join }}' + return test(src, 'John Paul George Ringo') + }) }) it('should support split/last', function () { @@ -277,6 +284,7 @@ describe('filters', 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 support array', () => test('{{ "1,2,3,4" | split: "," | slice: 1,2 | join }}', '2 3')) }) it('should support sort', function () {