Skip to content

Commit

Permalink
feat: 添加assign和extend函数
Browse files Browse the repository at this point in the history
  • Loading branch information
yanhaijing committed Oct 10, 2018
1 parent 8a64449 commit 579c470
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 58 deletions.
28 changes: 2 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

## 兼容性
单元测试保证支持如下环境:
Expand Down Expand Up @@ -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)
35 changes: 25 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"rollup-plugin-node-resolve": "3.0.3"
},
"dependencies": {
"@jsmini/is": "0.7.1",
"babel-runtime": "6.26.0"
}
}
84 changes: 75 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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;
}
13 changes: 0 additions & 13 deletions src/test.js

This file was deleted.

0 comments on commit 579c470

Please sign in to comment.