From c100a2e25e06d4d77f7d3f0b3bcf7ca65e36a9ce Mon Sep 17 00:00:00 2001 From: harttle Date: Thu, 3 Nov 2016 01:01:36 +0800 Subject: [PATCH] refactor: use _.assign --- index.js | 15 +++++++++------ src/scope.js | 5 +++-- src/util/underscore.js | 9 +++++++++ 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 0a69d53a1..a74332378 100644 --- a/index.js +++ b/index.js @@ -38,9 +38,11 @@ var _engine = { return this.parser.parse(tokens); }, render: function(tpl, ctx, opts) { - opts = opts || {}; - opts.strict_variables = opts.strict_variables || false; - opts.strict_filters = opts.strict_filters || false; + opts = _.assign({ + strict_variables: false, + strict_filters: false, + root: [] + }, opts); this.renderer.initRegister(opts); var scope = Scope.factory(ctx, { strict: opts.strict_variables, @@ -59,7 +61,7 @@ var _engine = { }); }, renderFile: function(filepath, ctx, opts) { - opts = opts || {}; + opts = _.assign({}, opts); return this.getTemplate(filepath, opts.root) .then(templates => this.render(templates, ctx, opts)) .catch(e => { @@ -122,7 +124,7 @@ var _engine = { }; function factory(options) { - options = options || {}; + options = _.assign({}, options); options.root = normalizeStringArray(options.root); if (!options.root.length) options.root = ['.']; @@ -148,7 +150,8 @@ factory.evalValue = Syntax.evalValue; factory.Types = { ParseError: Errors.ParseError, TokenizationEroor: Errors.TokenizationError, - RenderBreak: Errors.RenderBreak + RenderBreak: Errors.RenderBreak, + AssertionError: Errors.AssertionError }; module.exports = factory; diff --git a/src/scope.js b/src/scope.js index 4741cf5db..f605915eb 100644 --- a/src/scope.js +++ b/src/scope.js @@ -148,8 +148,9 @@ function matchRightBracket(str, begin) { } exports.factory = function(_ctx, opts) { - opts = opts || {}; - opts.strict = opts.strict || false; + opts = _.assign({ + strict: false + }, opts); var scope = Object.create(Scope); scope.opts = opts; diff --git a/src/util/underscore.js b/src/util/underscore.js index ab2da2981..d45a93fe2 100644 --- a/src/util/underscore.js +++ b/src/util/underscore.js @@ -25,6 +25,14 @@ function forOwn(object, iteratee) { return object; } +function assign(dst, src) { + dst = dst || {}; + forOwn(src, function(v, k) { + dst[k] = v; + }); + return dst; +} + function isArray(value) { return value instanceof Array; } @@ -32,3 +40,4 @@ function isArray(value) { exports.isString = isString; exports.isArray = isArray; exports.forOwn = forOwn; +exports.assign = assign;