Skip to content

Commit

Permalink
deep clone
Browse files Browse the repository at this point in the history
  • Loading branch information
aui committed Apr 25, 2017
1 parent 4b568ca commit a9f856e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## v4.4.3

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

## v4.4.2

Expand Down
8 changes: 5 additions & 3 deletions lib/template-node.js
Original file line number Diff line number Diff line change
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
2 changes: 1 addition & 1 deletion lib/template-web.js

Large diffs are not rendered by default.

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 a9f856e

Please sign in to comment.