From 485424cc8917459a8d6eb1b36de7124c7acdd952 Mon Sep 17 00:00:00 2001 From: harttle Date: Mon, 30 Oct 2017 02:22:56 +0800 Subject: [PATCH] feature: concat filter --- README.md | 2 +- filters.js | 1 + test/filters.js | 38 ++++++++++++++++++++++++++++++++++++++ test/tokenizer.js | 2 +- 4 files changed, 41 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7ec8349af..e21d0ef89 100644 --- a/README.md +++ b/README.md @@ -100,7 +100,7 @@ Otherwise, undefined variables will cause an exception. Defaults to `false`. * `trim_value_left` is similiar to `trim_value_right`, whereas the `\n` is exclusive. Defaults to `false`. See [Whitespace Control][whitespace control] for details. -* `greedy` is used to specify whether `trim_left`/`trim_right` is greedy. When set to `true`, all successive blank characters including `\n` will be trimed regardless of line breaks. Defaults to `true`. +* `greedy` is used to specify whether `trim_left`/`trim_right` is greedy. When set to `true`, all consecutive blank characters including `\n` will be trimed regardless of line breaks. Defaults to `true`. ## Use with Express.js diff --git a/filters.js b/filters.js index e644d3624..e8073f413 100644 --- a/filters.js +++ b/filters.js @@ -22,6 +22,7 @@ var filters = { 'append': (v, arg) => v + arg, 'capitalize': str => stringify(str).charAt(0).toUpperCase() + str.slice(1), 'ceil': v => Math.ceil(v), + 'concat': (v, arg) => Array.prototype.concat.call(v, arg), 'date': (v, arg) => { if (v === 'now') v = new Date() return v instanceof Date ? strftime(v, arg) : '' diff --git a/test/filters.js b/test/filters.js index 7998ec13d..b093681bd 100644 --- a/test/filters.js +++ b/test/filters.js @@ -45,6 +45,44 @@ describe('filters', function () { it('should return "184" for 183.357', () => test('{{ 183.357 | ceil }}', '184')) }) + describe('concat', function () { + it('should concat arrays', () => test(` + {%- assign fruits = "apples, oranges, peaches" | split: ", " -%} + {%- assign vegetables = "carrots, turnips, potatoes" | split: ", " -%} + + {%- assign everything = fruits | concat: vegetables -%} + + {%- for item in everything -%} + - {{ item }} + {% endfor -%}`, + `- apples + - oranges + - peaches + - carrots + - turnips + - potatoes + `)) + it('should support chained concat', () => test(` + {%- assign fruits = "apples, oranges, peaches" | split: ", " -%} + {%- assign vegetables = "carrots, turnips, potatoes" | split: ", " -%} + {%- assign furniture = "chairs, tables, shelves" | split: ", " -%} + {%- assign everything = fruits | concat: vegetables | concat: furniture -%} + + {%- for item in everything -%} + - {{ item }} + {% endfor -%}`, + `- apples + - oranges + - peaches + - carrots + - turnips + - potatoes + - chairs + - tables + - shelves + `)) + }) + describe('date', function () { it('should support date: %a %b %d %Y', function () { var str = ctx.date.toDateString() diff --git a/test/tokenizer.js b/test/tokenizer.js index 71fa5c533..ab85ef426 100644 --- a/test/tokenizer.js +++ b/test/tokenizer.js @@ -33,7 +33,7 @@ describe('tokenizer', function () { expect(tokens[1].type).to.equal('value') expect(tokens[1].value).to.equal('foo | date: "%Y-%m-%d"') }) - it('should handle successive value and tags', function () { + it('should handle consecutive value and tags', function () { var html = '{{foo}}{{bar}}{%foo%}{%bar%}' var tokens = parse(html)