From 579c47048b3f67fa06000c904099675d589cac88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A2=9C=E6=B5=B7=E9=95=9C?= Date: Wed, 10 Oct 2018 17:34:06 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0assign=E5=92=8Cextend?= =?UTF-8?q?=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 28 ++-------------- package-lock.json | 35 ++++++++++++++------ package.json | 1 + src/index.js | 84 ++++++++++++++++++++++++++++++++++++++++++----- src/test.js | 13 -------- 5 files changed, 103 insertions(+), 58 deletions(-) delete mode 100644 src/test.js diff --git a/README.md b/README.md index f25c018..51babf2 100644 --- a/README.md +++ b/README.md @@ -3,28 +3,11 @@ [![](https://img.shields.io/badge/Powered%20by-jslib%20extend-brightgreen.svg)](https://github.com/yanhaijing/jslib-extend) [![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/jsmini/extend/blob/master/LICENSE) [![Build Status](https://travis-ci.org/jsmini/extend.svg?branch=master)](https://travis-ci.org/jsmini/extend) -[![npm](https://img.shields.io/badge/npm-0.2.0-orange.svg)](https://www.npmjs.com/package/extend) +[![npm](https://img.shields.io/badge/npm-0.2.0-orange.svg)](https://www.npmjs.com/package/@jsmini/extend) [![NPM downloads](http://img.shields.io/npm/dm/@jsmini/extend.svg?style=flat-square)](http://www.npmtrends.com/@jsmini/extend) [![Percentage of issues still open](http://isitmaintained.com/badge/open/jsmini/extend.svg)](http://isitmaintained.com/project/jsmini/extend "Percentage of issues still open") -最好用的js第三方库脚手架,fork或clone本仓库,即可搭建完成一个新库的基础框架 - -## 特性 - -- ES6编写源码,编译生成生产代码 -- 集成 babel-runtime (默认关闭) -- 第三方依赖自动注入(自动剔除第三方依赖无用代码tree shaking) -- 多环境支持(支持浏览器原生,支持AMD,CMD,支持Webpack,Rollup,fis等,支持Node) -- 集成单元测试环境 -- 集成代码风格校验eslint -- 集成可持续构建工具[travis-ci](https://www.travis-ci.org/) -- 支持自定义banner -- 集成[jsmini](https://github.com/jsmini) -- 集成ISSUE_TEMPLATE -- 支持[sideEffects](https://juejin.im/post/5b4ff9ece51d45190c18bb65) -- 支持一键重命名 - -**注意:** 如果不同时使用 export 与 export default 可打开legacy模式,legacy模式下的模块系统可以兼容ie6-8,见rollup配置文件 +将对象属性递归放到目标对象,类似jQuery的`$.extend` ## 兼容性 单元测试保证支持如下环境: @@ -140,10 +123,3 @@ $ npm run rename # 重命名命令 [TODO.md](https://github.com/jsmini/extend/blob/master/TODO.md) ## 谁在使用 - -- [type](https://github.com/jsmini/type) -- [is](https://github.com/jsmini/is) -- [guid](https://github.com/jsmini/guid) -- [inherits](https://github.com/jsmini/inherits) -- [event](https://github.com/jsmini/event) -- [console](https://github.com/jsmini/console) diff --git a/package-lock.json b/package-lock.json index 9e82343..2adff70 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,23 +1,21 @@ { - "name": "@jsmini/base", + "name": "@jsmini/extend", "version": "0.2.0", "lockfileVersion": 1, "requires": true, "dependencies": { "@jsmini/is": { - "version": "0.4.0", - "resolved": "http://registry.npmjs.org/@jsmini/is/-/is-0.4.0.tgz", - "integrity": "sha512-RZEXdECJ7nOt/QlBxWC5Rdn5zODBlv6SdGLgy58OK4G/IHrB3O5ZSiqbf2MPdN94OnORYAC8xG190Siqw+9QpA==", - "dev": true, + "version": "0.7.1", + "resolved": "https://registry.npmjs.org/@jsmini/is/-/is-0.7.1.tgz", + "integrity": "sha512-qGuzavc41g2Hrb92eOneFguFuxK21fSrzLw5cW27laUD7a/E+nLst2bPcfbaNQPGMbRR7VGRbBvIbqRwIUZtjw==", "requires": { - "@jsmini/type": "0.4.0" + "@jsmini/type": "0.4.1" } }, "@jsmini/type": { - "version": "0.4.0", - "resolved": "https://registry.npmjs.org/@jsmini/type/-/type-0.4.0.tgz", - "integrity": "sha512-aRfW1dVDQPeBordr3ShLrpD1K5Ecfcyb20r9X8wpH0qpZ0vFBUSakYcJS3N67eoebYHVc7GIzN0sQIq2sKyPMg==", - "dev": true + "version": "0.4.1", + "resolved": "https://registry.npmjs.org/@jsmini/type/-/type-0.4.1.tgz", + "integrity": "sha512-gkd/6HyMzYzl8a53FihQXxUb1MDyOMaZTGbajy+1tl7lZapuPEJ1L/Jm96iesyRQCRTc5eyEvR1++srmwsYIjw==" }, "@types/acorn": { "version": "4.0.3", @@ -880,6 +878,23 @@ "requires": { "@jsmini/is": "0.4.0", "yargs": "11.0.0" + }, + "dependencies": { + "@jsmini/is": { + "version": "0.4.0", + "resolved": "http://registry.npmjs.org/@jsmini/is/-/is-0.4.0.tgz", + "integrity": "sha512-RZEXdECJ7nOt/QlBxWC5Rdn5zODBlv6SdGLgy58OK4G/IHrB3O5ZSiqbf2MPdN94OnORYAC8xG190Siqw+9QpA==", + "dev": true, + "requires": { + "@jsmini/type": "0.4.0" + } + }, + "@jsmini/type": { + "version": "0.4.0", + "resolved": "http://registry.npmjs.org/@jsmini/type/-/type-0.4.0.tgz", + "integrity": "sha512-aRfW1dVDQPeBordr3ShLrpD1K5Ecfcyb20r9X8wpH0qpZ0vFBUSakYcJS3N67eoebYHVc7GIzN0sQIq2sKyPMg==", + "dev": true + } } }, "chalk": { diff --git a/package.json b/package.json index 09ae833..ff71ab4 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "rollup-plugin-node-resolve": "3.0.3" }, "dependencies": { + "@jsmini/is": "0.7.1", "babel-runtime": "6.26.0" } } diff --git a/src/index.js b/src/index.js index c34bde1..1cc7827 100644 --- a/src/index.js +++ b/src/index.js @@ -1,14 +1,80 @@ -import { yan } from './test.js'; +@import { isBoolean, isObject, isArray } from '@jsmini/is'; -import yan2 from './test.js'; +// Object.create(null) 的对象,没有hasOwnProperty方法 +function hasOwnProp(obj, key) { + return Object.prototype.hasOwnProperty.call(obj, key); +} -console.log(yan); -console.log(yan2); +export function assign(target, ...sourceList) { + if (!isObject(target)) { + throw new TypeError('assign first param must is object'); + } -var a = 1 + 1; -var b = a; -console.log(a); -console.log(b); + for (let i = 0; i < sourceList.length; i++) { + const source = sourceList[i]; -export const name = 'base'; + if (isObject(source)) { + for(let key in source) { + if (hasOwnProp(source, key)) { + target[key] = source[key]; + } + } + } + } + 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); + } + + // 深拷贝 + if (!isObject(target)) { + throw new TypeError('extend target param must is object'); + } + + for (let i = 0; i < sourceList.length; i++) { + const source = sourceList[i]; + for (let name in source) { + const src = target[name]; + const copy = source[name]; + + //避免无限循环 + if (target === copy) { + continue; + } + + // 非可枚举属性 + if (!hasOwnProp(source, name)) { + continue; + } + + let copyIsArr; + if (copy && (isObject(copy) || (copyIsArr = isArray(copy)))) { + let clone; + if (copyIsArr) { + clone = src && isArray(src) ? src : []; + } else { + clone = src && isObject(src) ? src : {}; + } + target[name] = extend(true, clone, copy); + } else if (typeof copy !== 'undefined'){ + target[name] = copy; + } + } + } + + return target; +} diff --git a/src/test.js b/src/test.js deleted file mode 100644 index 41f0cee..0000000 --- a/src/test.js +++ /dev/null @@ -1,13 +0,0 @@ -export const yan = 'yan'; - -const yan2 = 'yan2'; - -export default yan2; - -class A { - aaa() {} -} - -export class B extends A { - bbb() {} -}