Skip to content

Commit

Permalink
feat: 将extend拆分为extendextendDeep
Browse files Browse the repository at this point in the history
  • Loading branch information
yanhaijing committed Oct 10, 2018
1 parent 3e23a99 commit ece83ff
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 33 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# 变更日志

## 0.2.0 / 2018-10-10

-`extend`拆分为`extend``extendDeep`

## 0.1.0 / 2018-10-10

- 新增 `assign()`
Expand Down
17 changes: 9 additions & 8 deletions doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,23 @@ assign({}, {a: 1}, {a: 2, b: 1}) // {a: 2, b: 1}
```

## extend
将源对象属性进行浅拷贝或深拷贝,合并到目标对象,类似jQuery的`$.extend`
将源对象属性进行浅拷贝,合并到目标对象,类似jQuery的`$.extend(false)`

其实际为assign的别名

## extendDeep
将源对象属性进行深拷贝,合并到目标对象,类似jQuery的`$.extend(true)`

函数参数和返回值(要遵守下面的例子的规则)

- param {boolean} deep 是否进行递归赋值,如果为false或不传都为浅赋值,传true为深度递归赋值
- param {object} target 目标对象
- param {object} [...source] 源原对象
- return {object} target对象

举个例子(要包含代码用例)

```js
// 浅赋值
extend({}, {a: 1}, {a: 2, b: 1}) // {a: 2, b: 1}
extend(false, {}, {a: 1}, {a: 2, b: 1}) // {a: 2, b: 1}

// 深度递归赋值
extend(true, {}, {a: 1}, {a: 2, b: 1}) // {a: 2, b: 1}
extendDeep({}, {a: 1}, {a: 2, b: 1}) // {a: 2, b: 1}
extendDeep(false, {}, {a: 1}, {a: 2, b: 1}) // {a: 2, b: 1}
extendDeep({}, {a: 1}, {a: 2, b: 1}) // {a: 2, b: 1}
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jsmini/extend",
"version": "0.1.0",
"version": "0.2.0",
"description": "最好用的js第三方库脚手架",
"main": "dist/index.js",
"jsnext:main": "dist/index.esm.js",
Expand Down
18 changes: 4 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,11 @@ export const assign = isFunction(Object.assign) ? Object.assign : function assig
return target;
};

export function extend(deep, target, ...sourceList) {
if (isObject(deep)) {
deep = false;
sourceList.unshift(target);
target = deep;
} else {
deep = !!deep;
}


// 浅拷贝
if (!deep) {
return assign(target, ...sourceList);
}
export function extend(...args) {
return assign(...args);
}

export function extendDeep(target, ...sourceList) {
// 深拷贝
if (!isObject(target) && !isArray(target)) {
throw new TypeError('extend target param must is object');
Expand Down
14 changes: 4 additions & 10 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var expect = require('expect.js');

var assign = require('../dist/index.js').assign;
var extend = require('../dist/index.js').extend;
var extendDeep = require('../dist/index.js').extendDeep;

describe('单元测试', function() {
this.timeout(1000);
Expand All @@ -19,27 +20,20 @@ describe('单元测试', function() {
});
});

describe('extend:shallow', function() {
describe('extend', function() {
it('常规', function() {
// 缺省参数
var h = extend({}, a, b);

expect(h.a).to.equal(b.a);
expect(h.b1).to.equal(b.b1);
expect(h.b2).to.equal(b.b2);

// 显示参数
var h = extend(false, {}, a, b);

expect(h.a).to.equal(b.a);
expect(h.b1).to.equal(b.b1);
expect(h.b2).to.equal(b.b2);
});
});

describe('extend:deep', function() {
describe('extendDeep', function() {
it('常规', function() {
var h = extend(true, {}, a, b);
var h = extendDeep({}, a, b);

expect(h.a).to.equal(b.a);
expect(h.b1).to.equal(b.b1);
Expand Down

0 comments on commit ece83ff

Please sign in to comment.