@cleverbrush/deep 0.0.1 → 0.0.4

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 ADDED
@@ -0,0 +1,61 @@
1
+ # Deep operations 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
@@ -11,8 +11,8 @@ const deepEqual = function (p1, p2) {
11
11
  const arraysAreIdentical = function (a1, a2) {
12
12
  if (a1.length !== a2.length)
13
13
  return false;
14
- const c1 = a1.map(t => t).sort();
15
- const c2 = a2.map(t => t).sort();
14
+ const c1 = a1.map((t) => t).sort();
15
+ const c2 = a2.map((t) => t).sort();
16
16
  for (let i = 0; i < c1.length; i++) {
17
17
  if (!compare(c1[i], c2[i]))
18
18
  return false;
@@ -27,7 +27,8 @@ const deepEqual = function (p1, p2) {
27
27
  return false;
28
28
  if (isBothNaN(o1, o2))
29
29
  return true;
30
- if ((Array.isArray(o1) && !Array.isArray(o2)) || (!Array.isArray(o1) && Array.isArray(o2))) {
30
+ if ((Array.isArray(o1) && !Array.isArray(o2)) ||
31
+ (!Array.isArray(o1) && Array.isArray(o2))) {
31
32
  return false;
32
33
  }
33
34
  if (Array.isArray(o1) && Array.isArray(o2)) {
@@ -38,8 +39,8 @@ const deepEqual = function (p1, p2) {
38
39
  return false;
39
40
  }
40
41
  cache.set(o1, true);
41
- let keys1 = Object.keys(o1);
42
- let keys2 = Object.keys(o2);
42
+ const keys1 = Object.keys(o1);
43
+ const keys2 = Object.keys(o2);
43
44
  if (keys1.length != keys2.length)
44
45
  return false;
45
46
  keys1.sort();
@@ -48,8 +49,8 @@ const deepEqual = function (p1, p2) {
48
49
  if (keys1[i] !== keys2[i]) {
49
50
  return false;
50
51
  }
51
- let v1 = o1[keys1[i]];
52
- let v2 = o2[keys1[i]];
52
+ const v1 = o1[keys1[i]];
53
+ const v2 = o2[keys1[i]];
53
54
  if (!compare(v1, v2))
54
55
  return false;
55
56
  }
@@ -14,7 +14,9 @@ const deepExtend = function () {
14
14
  const keys = Object.keys(o2);
15
15
  for (let i = 0; i < keys.length; i++) {
16
16
  if (o2.hasOwnProperty(keys[i])) {
17
- if ((!o1.hasOwnProperty(keys[i])) || !(typeof o1[keys[i]] === 'object' && typeof o2[keys[i]] === 'object')) {
17
+ if (!o1.hasOwnProperty(keys[i]) ||
18
+ !(typeof o1[keys[i]] === 'object' &&
19
+ typeof o2[keys[i]] === 'object')) {
18
20
  o1[keys[i]] = o2[keys[i]];
19
21
  }
20
22
  else {
package/dist/index.d.ts CHANGED
@@ -1,3 +1,8 @@
1
1
  import deepEqual from './deepEqual';
2
2
  import deepExtend from './deepExtend';
3
3
  export { deepEqual, deepExtend };
4
+ declare const _default: {
5
+ deepEqual: (p1: any, p2: any) => boolean;
6
+ deepExtend: (...args: any) => any;
7
+ };
8
+ export default _default;
package/dist/index.js CHANGED
@@ -8,3 +8,7 @@ const deepEqual_1 = __importDefault(require("./deepEqual"));
8
8
  exports.deepEqual = deepEqual_1.default;
9
9
  const deepExtend_1 = __importDefault(require("./deepExtend"));
10
10
  exports.deepExtend = deepExtend_1.default;
11
+ exports.default = {
12
+ deepEqual: deepEqual_1.default,
13
+ deepExtend: deepExtend_1.default
14
+ };
package/package.json CHANGED
@@ -1,7 +1,6 @@
1
1
  {
2
2
  "name": "@cleverbrush/deep",
3
- "version": "0.0.1",
4
- "type": "module",
3
+ "version": "0.0.4",
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
- let keys1 = Object.keys(o1);
44
- let keys2 = Object.keys(o2);
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
- let v1 = o1[keys1[i]];
55
- let v2 = o2[keys1[i]];
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/src/index.ts CHANGED
@@ -2,3 +2,8 @@ import deepEqual from './deepEqual';
2
2
  import deepExtend from './deepExtend';
3
3
 
4
4
  export { deepEqual, deepExtend };
5
+
6
+ export default {
7
+ deepEqual,
8
+ deepExtend
9
+ };
package/tsconfig.json CHANGED
@@ -3,10 +3,11 @@
3
3
  "rootDir": "./src",
4
4
  "outDir": "./dist",
5
5
  "declaration": true,
6
- "target": "ES2020",
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": {