diff --git a/CHANGELOG.md b/CHANGELOG.md index c85264d..ecd75cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # 变更日志 +## 0.2.0 / 2018-10-10 + +- 将`extend`拆分为`extend`和`extendDeep` + ## 0.1.0 / 2018-10-10 - 新增 `assign()` diff --git a/doc/api.md b/doc/api.md index f17fcb6..37f537d 100644 --- a/doc/api.md +++ b/doc/api.md @@ -19,11 +19,15 @@ 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对象 @@ -31,10 +35,7 @@ assign({}, {a: 1}, {a: 2, b: 1}) // {a: 2, b: 1} 举个例子(要包含代码用例) ```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} ``` diff --git a/package.json b/package.json index d2b6532..ff71ab4 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.js b/src/index.js index dc5549d..c4a01f4 100644 --- a/src/index.js +++ b/src/index.js @@ -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'); diff --git a/test/test.js b/test/test.js index 94fa30a..1c9b680 100644 --- a/test/test.js +++ b/test/test.js @@ -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); @@ -19,7 +20,7 @@ describe('单元测试', function() { }); }); - describe('extend:shallow', function() { + describe('extend', function() { it('常规', function() { // 缺省参数 var h = extend({}, a, b); @@ -27,19 +28,12 @@ describe('单元测试', function() { 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);