Skip to content

Commit

Permalink
perf(filter): shorthand syntax (#4377)
Browse files Browse the repository at this point in the history
* perf(filter): short hand syntax

* refactor(filter): make _after_html_render as an alia
  • Loading branch information
SukkaW committed Jun 26, 2020
1 parent 46f6da8 commit 65961d3
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/extend/filter.js
Expand Up @@ -4,7 +4,8 @@ const Promise = require('bluebird');

const typeAlias = {
pre: 'before_post_render',
post: 'after_post_render'
post: 'after_post_render',
'after_render:html': '_after_html_render'
};

class Filter {
Expand All @@ -28,8 +29,6 @@ class Filter {

if (typeof fn !== 'function') throw new TypeError('fn must be a function');

if (type === 'after_render:html') type = '_after_html_render';

type = typeAlias[type] || type;
priority = priority == null ? 10 : priority;

Expand All @@ -46,7 +45,7 @@ class Filter {
if (!type) throw new TypeError('type is required');
if (typeof fn !== 'function') throw new TypeError('fn must be a function');

if (type === 'after_render:html') type = '_after_html_render';
type = typeAlias[type] || type;

const list = this.list(type);
if (!list || !list.length) return;
Expand All @@ -58,6 +57,8 @@ class Filter {

exec(type, data, options = {}) {
const filters = this.list(type);
if (filters.length === 0) return Promise.resolve(data);

const ctx = options.context;
const args = options.args || [];

Expand All @@ -71,12 +72,15 @@ class Filter {

execSync(type, data, options = {}) {
const filters = this.list(type);
const filtersLen = filters.length;
if (filtersLen === 0) return data;

const ctx = options.context;
const args = options.args || [];

args.unshift(data);

for (let i = 0, len = filters.length; i < len; i++) {
for (let i = 0, len = filtersLen; i < len; i++) {
const result = Reflect.apply(filters[i], ctx, args);
args[0] = result == null ? args[0] : result;
}
Expand Down

0 comments on commit 65961d3

Please sign in to comment.