@cleverbrush/deep 1.0.0-beta.5 → 1.0.0-beta.6

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.
@@ -1,2 +1,4 @@
1
- declare const deepEqual: (p1: any, p2: any) => boolean;
1
+ declare const deepEqual: (p1: any, p2: any, options?: {
2
+ disregardArrayOrder?: boolean;
3
+ }) => boolean;
2
4
  export default deepEqual;
package/dist/deepEqual.js CHANGED
@@ -1,18 +1,35 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ const hashObject_js_1 = require("./hashObject.js");
3
4
  const isBothNaN = function (v1, v2) {
4
5
  return Number.isNaN(v1) && Number.isNaN(v2);
5
6
  };
6
- const deepEqual = function (p1, p2) {
7
- if (arguments.length !== 2)
8
- return false;
7
+ const deepEqual = function (p1, p2, options) {
9
8
  const cache = new Map();
10
9
  const compare = function (o1, o2) {
11
10
  const arraysAreIdentical = function (a1, a2) {
12
11
  if (a1.length !== a2.length)
13
12
  return false;
14
- const c1 = a1.map((t) => t).sort();
15
- const c2 = a2.map((t) => t).sort();
13
+ if (a1.length === 0 && a2.length === 0)
14
+ return true;
15
+ const c1 = options?.disregardArrayOrder
16
+ ? a1
17
+ .map((t) => t)
18
+ .sort((l, r) => {
19
+ const hash1 = (0, hashObject_js_1.HashObject)(l);
20
+ const hash2 = (0, hashObject_js_1.HashObject)(r);
21
+ return hash1 < hash2 ? 1 : -1;
22
+ })
23
+ : a1.map((t) => t);
24
+ const c2 = options?.disregardArrayOrder
25
+ ? a2
26
+ .map((t) => t)
27
+ .sort((l, r) => {
28
+ const hash1 = (0, hashObject_js_1.HashObject)(l);
29
+ const hash2 = (0, hashObject_js_1.HashObject)(r);
30
+ return hash1 < hash2 ? 1 : -1;
31
+ })
32
+ : a2.map((t) => t);
16
33
  for (let i = 0; i < c1.length; i++) {
17
34
  if (!compare(c1[i], c2[i]))
18
35
  return false;
@@ -39,6 +56,11 @@ const deepEqual = function (p1, p2) {
39
56
  return false;
40
57
  }
41
58
  cache.set(o1, true);
59
+ if (o1 instanceof Date && o2 instanceof Date) {
60
+ return (!isNaN(o1) &&
61
+ !isNaN(o2) &&
62
+ o1.getTime() === o2.getTime());
63
+ }
42
64
  const keys1 = Object.keys(o1);
43
65
  const keys2 = Object.keys(o2);
44
66
  if (keys1.length != keys2.length)
@@ -1,10 +1,10 @@
1
- export declare type CommonProps<T1, T2> = {
1
+ export type CommonProps<T1, T2> = {
2
2
  [k in keyof T1 & keyof T2]: T1[k] extends never ? never : T2[k] extends never ? never : T2[k];
3
3
  };
4
- export declare type PropsInFirstOnly<T1, T2> = Omit<T1, keyof T2>;
5
- export declare type MergeTwo<T1, T2> = PropsInFirstOnly<T1, T2> & PropsInFirstOnly<T2, T1> & {
4
+ export type PropsInFirstOnly<T1, T2> = Omit<T1, keyof T2>;
5
+ export type MergeTwo<T1, T2> = PropsInFirstOnly<T1, T2> & PropsInFirstOnly<T2, T1> & {
6
6
  [k in keyof CommonProps<T1, T2>]: T1[k] extends Record<string, unknown> ? T2[k] extends Record<string, unknown> ? MergeTwo<T1[k], T2[k]> : T2[k] : T2[k];
7
7
  };
8
- export declare type Merge<T extends unknown[]> = T['length'] extends 3 ? MergeTwo<T[0], MergeTwo<T[1], T[2]>> : T['length'] extends 2 ? MergeTwo<T[0], T[1]> : T['length'] extends 1 ? T[0] : T extends [...infer K, infer PL, infer L] ? Merge<[Merge<[...K]>, MergeTwo<PL, L>]> : never;
8
+ export type Merge<T extends unknown[]> = T['length'] extends 3 ? MergeTwo<T[0], MergeTwo<T[1], T[2]>> : T['length'] extends 2 ? MergeTwo<T[0], T[1]> : T['length'] extends 1 ? T[0] : T extends [...infer K, infer PL, infer L] ? Merge<[Merge<[...K]>, MergeTwo<PL, L>]> : never;
9
9
  declare const deepExtend: <T extends unknown[]>(...args: T) => Merge<T>;
10
10
  export default deepExtend;
@@ -0,0 +1 @@
1
+ export declare function HashObject(unkType: any, exclude?: any): any;
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HashObject = void 0;
4
+ const stringHash = (string, noType) => {
5
+ let hashString = string;
6
+ if (!noType) {
7
+ hashString = `string${string}`;
8
+ }
9
+ let hash = 0;
10
+ for (let i = 0; i < hashString.length; i++) {
11
+ const character = hashString.charCodeAt(i);
12
+ hash = (hash << 5) - hash + character;
13
+ hash = hash & hash; // Convert to 32bit integer
14
+ }
15
+ return hash;
16
+ };
17
+ function objectHash(obj, exclude) {
18
+ if (exclude.indexOf(obj) > -1) {
19
+ return undefined;
20
+ }
21
+ let hash = '';
22
+ const keys = Object.keys(obj).sort();
23
+ for (let index = 0; index < keys.length; index += 1) {
24
+ const key = keys[index];
25
+ const keyHash = hashFunc(key);
26
+ const attrHash = hashFunc(obj[key], exclude);
27
+ exclude.push(obj[key]);
28
+ hash += stringHash(`object${keyHash}${attrHash}`, true);
29
+ }
30
+ return stringHash(hash, true);
31
+ }
32
+ function HashObject(unkType, exclude) {
33
+ let ex = exclude;
34
+ if (ex === undefined) {
35
+ ex = [];
36
+ }
37
+ if (!isNaN(unkType) && typeof unkType !== 'string') {
38
+ return unkType;
39
+ }
40
+ switch (typeof unkType) {
41
+ case 'object':
42
+ return objectHash(unkType, ex);
43
+ default:
44
+ return stringHash(String(unkType));
45
+ }
46
+ }
47
+ exports.HashObject = HashObject;
48
+ const hashFunc = HashObject;
package/dist/index.d.ts CHANGED
@@ -2,7 +2,9 @@ import deepEqual from './deepEqual';
2
2
  import deepExtend, { Merge } from './deepExtend';
3
3
  export { deepEqual, deepExtend, Merge };
4
4
  declare const _default: {
5
- deepEqual: (p1: any, p2: any) => boolean;
5
+ deepEqual: (p1: any, p2: any, options?: {
6
+ disregardArrayOrder?: boolean;
7
+ }) => boolean;
6
8
  deepExtend: <T extends unknown[]>(...args: T) => Merge<T>;
7
9
  };
8
10
  export default _default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cleverbrush/deep",
3
- "version": "1.0.0-beta.5",
3
+ "version": "1.0.0-beta.6",
4
4
  "keywords": [
5
5
  "deep operations on objects",
6
6
  "deep equal",
@@ -0,0 +1,69 @@
1
+ import deepEqual from './deepEqual.js';
2
+
3
+ test('deepEqual - 1', () => {
4
+ expect(deepEqual('a', 'a')).toEqual(true);
5
+ });
6
+
7
+ test('deepEqual - 2', () => {
8
+ expect(deepEqual('a', 'b')).toEqual(false);
9
+ });
10
+
11
+ test('deepEqual - 3', () => {
12
+ expect(deepEqual({}, 'a')).toEqual(false);
13
+ });
14
+
15
+ test('deepEqual - 4', () => {
16
+ expect(deepEqual({ a: 10 }, { a: 10 })).toEqual(true);
17
+ });
18
+
19
+ test('deepEqual - 5', () => {
20
+ expect(deepEqual([], [])).toEqual(true);
21
+ });
22
+
23
+ test('deepEqual - 6', () => {
24
+ expect(deepEqual([10], [])).toEqual(false);
25
+ });
26
+
27
+ test('deepEqual - 7', () => {
28
+ expect(deepEqual([10], [10])).toEqual(true);
29
+ });
30
+
31
+ test('deepEqual - 8', () => {
32
+ expect(deepEqual([10], [10, { b: 20 }])).toEqual(false);
33
+ });
34
+
35
+ test('deepEqual - 9', () => {
36
+ expect(deepEqual([{ b: 20 }, 10], [10, { b: 20 }])).toEqual(false);
37
+ });
38
+
39
+ test('deepEqual - 10', () => {
40
+ expect(
41
+ deepEqual([{ b: 20 }, 10], [10, { b: 20 }], {
42
+ disregardArrayOrder: true
43
+ })
44
+ ).toEqual(true);
45
+ });
46
+
47
+ test('deepEqual - 11', () => {
48
+ expect(
49
+ deepEqual(
50
+ [{ b: 20, c: { d: 'a', e: new Date(2022, 10, 1) } }, 10],
51
+ [10, { b: 20 }],
52
+ {
53
+ disregardArrayOrder: true
54
+ }
55
+ )
56
+ ).toEqual(false);
57
+ });
58
+
59
+ test('deepEqual - 12', () => {
60
+ expect(
61
+ deepEqual(
62
+ [{ b: 20, c: { d: 'a', e: new Date(2022, 10, 1) } }, 10],
63
+ [10, { b: 20, c: { e: new Date(2022, 10, 1), d: 'a' } }],
64
+ {
65
+ disregardArrayOrder: true
66
+ }
67
+ )
68
+ ).toEqual(true);
69
+ });
package/src/deepEqual.ts CHANGED
@@ -1,16 +1,39 @@
1
+ import { HashObject } from './hashObject.js';
2
+
1
3
  const isBothNaN = function (v1, v2) {
2
4
  return Number.isNaN(v1) && Number.isNaN(v2);
3
5
  };
4
6
 
5
- const deepEqual = function (p1, p2): boolean {
6
- if (arguments.length !== 2) return false;
7
+ const deepEqual = function (
8
+ p1,
9
+ p2,
10
+ options?: { disregardArrayOrder?: boolean }
11
+ ): boolean {
7
12
  const cache = new Map();
8
13
 
9
14
  const compare = function (o1, o2) {
10
15
  const arraysAreIdentical = function (a1, a2) {
11
16
  if (a1.length !== a2.length) return false;
12
- const c1 = a1.map((t) => t).sort();
13
- const c2 = a2.map((t) => t).sort();
17
+ if (a1.length === 0 && a2.length === 0) return true;
18
+
19
+ const c1 = options?.disregardArrayOrder
20
+ ? a1
21
+ .map((t) => t)
22
+ .sort((l, r) => {
23
+ const hash1 = HashObject(l);
24
+ const hash2 = HashObject(r);
25
+ return hash1 < hash2 ? 1 : -1;
26
+ })
27
+ : a1.map((t) => t);
28
+ const c2 = options?.disregardArrayOrder
29
+ ? a2
30
+ .map((t) => t)
31
+ .sort((l, r) => {
32
+ const hash1 = HashObject(l);
33
+ const hash2 = HashObject(r);
34
+ return hash1 < hash2 ? 1 : -1;
35
+ })
36
+ : a2.map((t) => t);
14
37
 
15
38
  for (let i = 0; i < c1.length; i++) {
16
39
  if (!compare(c1[i], c2[i])) return false;
@@ -40,6 +63,14 @@ const deepEqual = function (p1, p2): boolean {
40
63
 
41
64
  cache.set(o1, true);
42
65
 
66
+ if (o1 instanceof Date && o2 instanceof Date) {
67
+ return (
68
+ !isNaN(o1 as any) &&
69
+ !isNaN(o2 as any) &&
70
+ o1.getTime() === o2.getTime()
71
+ );
72
+ }
73
+
43
74
  const keys1 = Object.keys(o1);
44
75
  const keys2 = Object.keys(o2);
45
76
  if (keys1.length != keys2.length) return false;
@@ -0,0 +1,47 @@
1
+ const stringHash = (string, noType?) => {
2
+ let hashString = string;
3
+ if (!noType) {
4
+ hashString = `string${string}`;
5
+ }
6
+ let hash = 0;
7
+ for (let i = 0; i < hashString.length; i++) {
8
+ const character = hashString.charCodeAt(i);
9
+ hash = (hash << 5) - hash + character;
10
+ hash = hash & hash; // Convert to 32bit integer
11
+ }
12
+ return hash;
13
+ };
14
+
15
+ function objectHash(obj, exclude) {
16
+ if (exclude.indexOf(obj) > -1) {
17
+ return undefined;
18
+ }
19
+ let hash = '';
20
+ const keys = Object.keys(obj).sort();
21
+ for (let index = 0; index < keys.length; index += 1) {
22
+ const key = keys[index];
23
+ const keyHash = hashFunc(key);
24
+ const attrHash = hashFunc(obj[key], exclude);
25
+ exclude.push(obj[key]);
26
+ hash += stringHash(`object${keyHash}${attrHash}`, true);
27
+ }
28
+ return stringHash(hash, true);
29
+ }
30
+
31
+ export function HashObject(unkType, exclude?) {
32
+ let ex = exclude;
33
+ if (ex === undefined) {
34
+ ex = [];
35
+ }
36
+ if (!isNaN(unkType) && typeof unkType !== 'string') {
37
+ return unkType;
38
+ }
39
+ switch (typeof unkType) {
40
+ case 'object':
41
+ return objectHash(unkType, ex);
42
+ default:
43
+ return stringHash(String(unkType));
44
+ }
45
+ }
46
+
47
+ const hashFunc = HashObject;