Skip to content

Commit

Permalink
deepclone options
Browse files Browse the repository at this point in the history
  • Loading branch information
aui committed Apr 25, 2017
2 parents 6d1b329 + b23a5f6 commit 1b9d81d
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 24 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# CHANGELOG

## v4.4.3
## v4.5.0

1. 支持对 `options.imports` 的深拷贝 [#1](https://github.com/aui/express-art-template/issues/1)
2. 支持对 `options.rules` 的覆盖

## v4.4.2

Expand Down
10 changes: 6 additions & 4 deletions lib/template-node.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! art-template@4.4.3 | https://github.com/aui/art-template */
/*! art-template@4.5.0 | https://github.com/aui/art-template */
module.exports =
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
Expand Down Expand Up @@ -236,8 +236,8 @@ var htmlMinifier = __webpack_require__(7);
var resolveFilename = __webpack_require__(11);

var toString = Object.prototype.toString;
var isObject = function isObject(value) {
return value !== null && toString.call(value).slice(8, -1) === 'Object';
var toType = function toType(value) {
return value === null ? 'Null' : toString.call(value).slice(8, -1);
};

/**
Expand All @@ -250,7 +250,9 @@ function extend(options) {

for (var name in options) {
var value = options[name];
if (isObject(value)) {
var type = toType(value);

if (type === 'Array' || type === 'Object') {
copy[name] = extend.call(copy[name], value);
} else {
copy[name] = value;
Expand Down
4 changes: 2 additions & 2 deletions lib/template-web.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "art-template",
"description": "JavaScript Template Engine",
"homepage": "http://aui.github.com/art-template/",
"version": "4.4.3",
"version": "4.5.0",
"keywords": [
"template"
],
Expand Down
10 changes: 6 additions & 4 deletions src/compile/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ const resolveFilename = require('./adapter/resolve-filename');


const toString = Object.prototype.toString;
const isObject = value => {
return value !== null && toString.call(value).slice(8, -1) === 'Object';
const toType = value => {
return value === null ? 'Null' : toString.call(value).slice(8, -1);
};


Expand All @@ -26,8 +26,10 @@ function extend(options) {
const copy = Object.create(this);

for (let name in options) {
let value = options[name];
if (isObject(value)) {
const value = options[name];
const type = toType(value);

if (type === 'Array' || type === 'Object') {
copy[name] = extend.call(copy[name], value);
} else {
copy[name] = value;
Expand Down
24 changes: 12 additions & 12 deletions test/compile/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,24 @@ module.exports = {
},

'rules': () => {
// const rules = defaults.rules;
// const length = rules.length;
// const options = defaults.$extend({
// rules: [null]
// });

// assert.deepEqual(null, options.rules[2]);
// assert.deepEqual(rules[0], options.rules[0]);
// assert.deepEqual(length, rules.length);

const rules = defaults.rules;
const length = rules.length;
const options = defaults.$extend({
rules: []
rules: [null]
});

assert.deepEqual(0, options.rules.length);
assert.deepEqual(null, options.rules[0]);
assert.deepEqual(rules[1], options.rules[1]);
assert.deepEqual(length, rules.length);

// const rules = defaults.rules;
// const length = rules.length;
// const options = defaults.$extend({
// rules: []
// });

// assert.deepEqual(0, options.rules.length);
// assert.deepEqual(length, rules.length);
}
}
};

0 comments on commit 1b9d81d

Please sign in to comment.