@cleverbrush/deep 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +61 -0
- package/dist/deepEqual.js +4 -4
- package/package.json +2 -2
- package/src/deepEqual.ts +5 -5
- package/tsconfig.json +3 -2
package/README.md
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# Deep operation on objects
|
|
2
|
+
|
|
3
|
+
This is yet another library which provides operations over JavaScript objects, like deep compare and deep merge.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
npm install @cleverbrush/deep
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
const deep = require('@cleverbrush/deep');
|
|
12
|
+
|
|
13
|
+
or for ES Module
|
|
14
|
+
|
|
15
|
+
import deep from '@cleverbrush/deep';
|
|
16
|
+
// or import functions individually
|
|
17
|
+
import { deepEqual, deepExtend } from '@cleverbrush/deep';
|
|
18
|
+
|
|
19
|
+
## Functions
|
|
20
|
+
|
|
21
|
+
### deepEqual
|
|
22
|
+
|
|
23
|
+
Takes two objects as parameters, compares it recursively and returns `true` if they are equal or `false` in other case.
|
|
24
|
+
|
|
25
|
+
import { deepEqual } from '@cleverbrush/deep';
|
|
26
|
+
|
|
27
|
+
const equal1 = deepEqual({ a: {b : 1} }, { a: {b: 1} });
|
|
28
|
+
// equal1 === true
|
|
29
|
+
|
|
30
|
+
const equal2 = deepEqual({ a: {b : 1} }, { a: {b: 20} });
|
|
31
|
+
// equal2 === false
|
|
32
|
+
|
|
33
|
+
const equal3 = deepEqual({ a: {b : 1, c: 2} }, { a: {b: 20} });
|
|
34
|
+
// equal3 === false
|
|
35
|
+
|
|
36
|
+
### deepExtend
|
|
37
|
+
|
|
38
|
+
Works similary to `Object.extend`, but respects the deep structure of objects.
|
|
39
|
+
|
|
40
|
+
import { deepExtend } from '@cleverbrush/deep';
|
|
41
|
+
|
|
42
|
+
deepExtend({}, {
|
|
43
|
+
a: 'something,
|
|
44
|
+
name: {
|
|
45
|
+
first: 'Ivan'
|
|
46
|
+
}
|
|
47
|
+
}, {
|
|
48
|
+
name: {
|
|
49
|
+
last: 'Ivanov'
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
will return the following object:
|
|
54
|
+
|
|
55
|
+
{
|
|
56
|
+
a: 'something',
|
|
57
|
+
name: {
|
|
58
|
+
first: 'Ivan',
|
|
59
|
+
last: 'Ivanov'
|
|
60
|
+
}
|
|
61
|
+
}
|
package/dist/deepEqual.js
CHANGED
|
@@ -39,8 +39,8 @@ const deepEqual = function (p1, p2) {
|
|
|
39
39
|
return false;
|
|
40
40
|
}
|
|
41
41
|
cache.set(o1, true);
|
|
42
|
-
|
|
43
|
-
|
|
42
|
+
const keys1 = Object.keys(o1);
|
|
43
|
+
const keys2 = Object.keys(o2);
|
|
44
44
|
if (keys1.length != keys2.length)
|
|
45
45
|
return false;
|
|
46
46
|
keys1.sort();
|
|
@@ -49,8 +49,8 @@ const deepEqual = function (p1, p2) {
|
|
|
49
49
|
if (keys1[i] !== keys2[i]) {
|
|
50
50
|
return false;
|
|
51
51
|
}
|
|
52
|
-
|
|
53
|
-
|
|
52
|
+
const v1 = o1[keys1[i]];
|
|
53
|
+
const v2 = o2[keys1[i]];
|
|
54
54
|
if (!compare(v1, v2))
|
|
55
55
|
return false;
|
|
56
56
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cleverbrush/deep",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"type": "module",
|
|
3
|
+
"version": "0.0.3",
|
|
5
4
|
"keywords": [
|
|
6
5
|
"deep operations on objects",
|
|
7
6
|
"deep equal",
|
|
@@ -10,6 +9,7 @@
|
|
|
10
9
|
"author": "Andrew Zolotukhin <andrew_zol@cleverbrush.com>",
|
|
11
10
|
"license": "BSD",
|
|
12
11
|
"main": "./dist/index.js",
|
|
12
|
+
"readme": "https://github.com/cleverbrush/libs/tree/master/libs/deep#readme",
|
|
13
13
|
"scripts": {
|
|
14
14
|
"watch": "tsc --watch",
|
|
15
15
|
"build": "tsc"
|
package/src/deepEqual.ts
CHANGED
|
@@ -2,7 +2,7 @@ const isBothNaN = function (v1, v2) {
|
|
|
2
2
|
return Number.isNaN(v1) && Number.isNaN(v2);
|
|
3
3
|
};
|
|
4
4
|
|
|
5
|
-
const deepEqual = function (p1, p2) {
|
|
5
|
+
const deepEqual = function (p1, p2): boolean {
|
|
6
6
|
if (arguments.length !== 2) return false;
|
|
7
7
|
const cache = new Map();
|
|
8
8
|
|
|
@@ -40,8 +40,8 @@ const deepEqual = function (p1, p2) {
|
|
|
40
40
|
|
|
41
41
|
cache.set(o1, true);
|
|
42
42
|
|
|
43
|
-
|
|
44
|
-
|
|
43
|
+
const keys1 = Object.keys(o1);
|
|
44
|
+
const keys2 = Object.keys(o2);
|
|
45
45
|
if (keys1.length != keys2.length) return false;
|
|
46
46
|
|
|
47
47
|
keys1.sort();
|
|
@@ -51,8 +51,8 @@ const deepEqual = function (p1, p2) {
|
|
|
51
51
|
return false;
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
-
|
|
55
|
-
|
|
54
|
+
const v1 = o1[keys1[i]];
|
|
55
|
+
const v2 = o2[keys1[i]];
|
|
56
56
|
if (!compare(v1, v2)) return false;
|
|
57
57
|
}
|
|
58
58
|
return true;
|
package/tsconfig.json
CHANGED
|
@@ -3,10 +3,11 @@
|
|
|
3
3
|
"rootDir": "./src",
|
|
4
4
|
"outDir": "./dist",
|
|
5
5
|
"declaration": true,
|
|
6
|
-
"target": "
|
|
6
|
+
"target": "esnext",
|
|
7
7
|
"module": "CommonJS",
|
|
8
8
|
"esModuleInterop": true,
|
|
9
|
-
"moduleResolution": "Node"
|
|
9
|
+
"moduleResolution": "Node",
|
|
10
|
+
"allowSyntheticDefaultImports": true
|
|
10
11
|
},
|
|
11
12
|
"files": ["./src/index.ts"],
|
|
12
13
|
"watchOptions": {
|