Skip to content

Commit be1a44f

Browse files
author
yong.teng
committed
Object 增加 omit 方法,支持对象拷贝,并剔除指定属性 && Object 增加 equals 方法,深度比较两个对象是否相同
1 parent 3891da1 commit be1a44f

File tree

6 files changed

+121
-4
lines changed

6 files changed

+121
-4
lines changed

.eslintrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ module.exports = {
22
root: true,
33
env: {
44
node: true,
5-
browser: true
5+
browser: true,
6+
es6: true
67
},
78
extends: [
89
'eslint:recommended'

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# 更新日志
22

3+
## [v2.2.0](https://github.com/buession/buession-prototype/releases/tag/2.2.0)(2023-01-18)
4+
5+
### ⭐ 新特性
6+
7+
- Object 增加 omit 方法,支持对象拷贝,并剔除指定属性
8+
- Object 增加 equals 方法,深度比较两个对象是否相同
9+
10+
311
## [v2.1.0](https://github.com/buession/buession-prototype/releases/tag/2.1.0)(2023-01-18)
412

513
### ⭐ 新特性

example/cookie.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<body>
1010
<script src="../dist/prototype.umd.js"></script>
1111
<script type="text/javascript">
12-
document.httpCookie.set("a", "中国");
12+
alert(document.httpCookie.set("a", "中国"));
1313
alert(document.httpCookie.get("a"))
1414
</script>
1515
</body>

example/object.html

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
7+
<title>buession prototype</title>
8+
</head>
9+
<body>
10+
<script src="../dist/prototype.umd.js"></script>
11+
<script type="text/javascript">
12+
var source = {
13+
id: 1,
14+
name: 'buession',
15+
year: 2023
16+
};
17+
var target = Object.omit(source, "id", "year");
18+
var nSource1 = source;
19+
var nSource2 = Object.assign({}, source);
20+
console.log(target);
21+
22+
23+
console.log("字符串比较:" + Object.equals("a", "a"));
24+
console.log("字符串比较:" + Object.equals("a", "A"));
25+
console.log("整型比较:" + Object.equals(1, 1));
26+
console.log("整型比较:" + Object.equals(1, 1.0));
27+
console.log("浮点型比较:" + Object.equals(1.0, 1.0));
28+
console.log("整型和数字字符串比较:" + Object.equals(1, "1"));
29+
console.log("数组比较:" + Object.equals([1], [1]));
30+
console.log("数组比较:" + Object.equals([1, true], [1, 1]));
31+
console.log("对象比较:" + Object.equals(window, window));
32+
console.log("对象比较:" + Object.equals(source, target));
33+
console.log("对象比较:" + Object.equals(source, nSource1));
34+
console.log("对象比较:" + Object.equals(source, nSource2));
35+
</script>
36+
</body>
37+
</html>

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"name": "@buession/prototype",
33
"alias": "prototype",
4-
"version": "v2.1.0",
4+
"version": "v2.2.0",
55
"description": "A native object extension framework for Javascript.",
6-
"homepage": "https://buession.github.io/buession-prototype/",
6+
"homepage": "https://prototype.buession.com/",
77
"author": {
88
"name": "yong.teng",
99
"email": "webmaster@buession.com"

src/object.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,31 @@ interface ObjectConstructor {
179179
*/
180180
isUndefinedOrNull(obj: any): boolean;
181181

182+
/**
183+
* 判断两个对象是否相等
184+
*
185+
* @param obj1 一个对象
186+
* @param obj2 用于和 obj1 比较的对象
187+
* @return 当两个对象相等时,返回 true;否则,返回 false
188+
*/
189+
equals(obj1: any, obj2: any): boolean;
190+
182191
/**
183192
* 克隆对象
184193
*
185194
* @param obj 任意对象
186195
* @return 新对象实例
187196
*/
188197
clone(obj: any): any;
198+
199+
/**
200+
* 克隆对象,但需要删除指定属性
201+
*
202+
* @param obj 任意对象
203+
* @param fields 需要删除的属性
204+
* @return 新对象实例
205+
*/
206+
omit<T extends object, K extends keyof T> (obj: T, ...fields: K[]): Omit<T, K>;
189207
}
190208

191209
declare var Object: ObjectConstructor;
@@ -449,3 +467,56 @@ Object.clone = function(obj: any): any {
449467

450468
return obj;
451469
}
470+
471+
/**
472+
* 判断两个对象是否相等
473+
*
474+
* @param obj1 一个对象
475+
* @param obj2 用于和 obj1 比较的对象
476+
* @return 当两个对象相等时,返回 true;否则,返回 false
477+
*/
478+
Object.equals = function(obj1: any, obj2: any): boolean {
479+
if (obj1 === obj2) {
480+
return true;
481+
} else if (!(obj1 instanceof Object) || !(obj2 instanceof Object)) {
482+
return false;
483+
} else if (obj1.constructor !== obj2.constructor) {
484+
return false;
485+
} else if (Object.isArray(obj1) && Object.isArray(obj2) && obj1.length === obj2.length) {
486+
for (let i = 0; i < obj1.length; i++) {
487+
if (Object.equals(obj1[i], obj2[i]) === false) {
488+
return false
489+
}
490+
}
491+
}else if (Object.isObject(obj1) && Object.isObject(obj2) && Object.keys(obj1).length === Object.keys(obj2).length) {
492+
for (const key in obj1) {
493+
if (obj1.hasOwnProperty.call(key)) {
494+
if (Object.equals(obj1[key], obj2[key]) === false) {
495+
return false
496+
}
497+
}
498+
}
499+
} else {
500+
return false;
501+
}
502+
503+
return true;
504+
}
505+
506+
/**
507+
* 克隆对象,但需要删除指定属性
508+
*
509+
* @param obj 任意对象
510+
* @param fields 需要删除的属性
511+
* @return 新对象实例
512+
*/
513+
Object.omit = function<T extends object, K extends keyof T>(obj: T, ...fields: K[]): Omit<T, K> {
514+
const result = Object.assign({}, obj);
515+
516+
for (let i = 0; i < fields.length; i++) {
517+
const key = fields[i];
518+
delete result[key];
519+
}
520+
521+
return result;
522+
}

0 commit comments

Comments
 (0)