1
+ import { is , isObject } from "@coven/predicates" ;
1
2
import type { Just } from "@coven/types" ;
3
+ import { always } from "@coven/utils" ;
2
4
import type { CurriedComparison } from "./CurriedComparison.ts" ;
3
5
import type { Difference } from "./Difference.ts" ;
4
6
import { compareObjects } from "./compareObjects.ts" ;
5
- import { isObject } from "./isObject.ts" ;
6
7
import { valueToDifference } from "./valueToDifference.ts" ;
7
8
9
+ // deno-lint-ignore no-boolean-literal-for-arguments
10
+ const alwaysFalse = always ( false ) ;
11
+
8
12
/**
9
13
* Function to compare a `left` and a `right` value, by doing a deep comparison
10
14
* and yielding the differences found with a descriptive object.
@@ -28,6 +32,8 @@ export const compare = (left: unknown): CurriedComparison<unknown> => {
28
32
const valuesToDifferenceLeft = valueToDifference ( left ) ;
29
33
const leftIsObject = isObject ( left ) ;
30
34
const compareObjectsLeft = leftIsObject ? compareObjects ( left ) : undefined ;
35
+ const isLeft = is ( left ) ;
36
+ const isLeftConstructor = leftIsObject ? is ( left . constructor ) : alwaysFalse ;
31
37
32
38
return leftIsObject
33
39
/**
@@ -37,13 +43,12 @@ export const compare = (left: unknown): CurriedComparison<unknown> => {
37
43
* @yields Differences.
38
44
*/
39
45
? function * ( right ) : Generator < Difference > {
40
- Object . is ( left , right )
41
- ? undefined
42
- : isObject ( right ) && left . constructor === right . constructor
43
- ? yield * (
44
- compareObjectsLeft as Just < typeof compareObjectsLeft >
45
- ) ( right )
46
- : yield valuesToDifferenceLeft ( right ) ;
46
+ isLeft ( right ) ? undefined : yield * (
47
+ isObject ( right ) &&
48
+ isLeftConstructor ( ( right as object ) . constructor )
49
+ ? compareObjectsLeft as Just < typeof compareObjectsLeft >
50
+ : valuesToDifferenceLeft
51
+ ) ( right ) ;
47
52
}
48
53
/**
49
54
* Curried {@link compare} with `left` set in context.
@@ -52,8 +57,8 @@ export const compare = (left: unknown): CurriedComparison<unknown> => {
52
57
* @yields Differences.
53
58
*/
54
59
: function * ( right ) : Generator < Difference > {
55
- Object . is ( left , right ) ? undefined : (
56
- yield valuesToDifferenceLeft ( right )
60
+ isLeft ( right ) ? undefined : (
61
+ yield * valuesToDifferenceLeft ( right )
57
62
) ;
58
63
} ;
59
64
} ;
0 commit comments