All files / expect / _utils.ts

93.00% Branches 93/100
100.00% Functions 13/13
92.34% Lines 205/222
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
 
 
 
 
 
x105
 
x105
x194
x194
x194
x194
x194
x2
x2
x194
x17
x17
x194
x194
 
x105
x13
x1
x13
x12
x12
x13
 
 
x105
x109
x109
 
x105
x13
x13
 
x173
x173
x173
 
x167
x167
x167
x167
x167
x167
x167
x167
 
x167
 
x36
x36
x36
x36
x36
 
x36
x36
 
x54
x54
x54
 
 
 
 
 
x54
x54
x54
 
x54
 
 
x6
 
 
 
 
 
 
x6
 
 
x105
 
x256
 
x256
x256
x256
x256
 
x256
x256
x256
x256
x256
x256
x256
x256
x237
x237
x101
x1
x1
x18
x101
 
 
 
 
x2
x1
x1
x2
x17
x17
 
 
x17
x17
x17
x17
x28
x28
x28
 
 
 
 
x17
x17
x17
x17
 
x101
x11
x1
x11
x7
x7
x17
x4
x4
x10
x10
x10
x10
x3
x3
x10
 
x4
x1
x1
x1
x4
x17
 
x7
x7
x7
x10
x3
x3
x5
x5
x5
x5
x5
x5
x1
x1
x2
x2
x2
x2
 
 
x2
x2
x1
x1
x1
x1
 
x1
 
 
 
x2
 
x1
x1
x1
x1
x1
x5
 
x3
x3
x3
x3
x11
 
x6
 
x101
x15
x15
x15
x15
x15
x2
x2
x15
x101
x1
x1
 
x3
x3
x101
x1
x1
 
 
x2
x2
x2
x256
 
 
x105
x105
x105
x105
 
x33
x33
 
 
x33
x33
 
x87
x113
x77
x77
 
 
x36
 
x36
x54
 
 
 
 
 
x18
x54
x54
x54
x54
x54
x54
 
x54
x54
x54
x36
x36
 
x36
x33
 
x33
x33







I

























































I




I






I
















































































































I






























































I




I






















// Copyright 2018-2026 the Deno authors. MIT license.
// Copyright (c) Meta Platforms, Inc. and affiliates. All rights reserved. MIT license.

import type { EqualOptions, EqualOptionUtil } from "./_types.ts";
import type { Tester } from "./_types.ts";
import { equal } from "./_equal.ts";

export function buildEqualOptions(options: EqualOptionUtil): EqualOptions {
  const { customMessage, customTesters = [], strictCheck } = options ?? {};
  const ret: EqualOptions = {
    customTesters,
  };
  if (customMessage !== undefined) {
    ret.msg = customMessage;
  }
  if (strictCheck !== undefined) {
    ret.strictCheck = strictCheck;
  }
  return ret;
}

export function isPromiseLike(value: unknown): value is PromiseLike<unknown> {
  if (value == null) {
    return false;
  } else {
    return typeof ((value as Record<string, unknown>).then) === "function";
  }
}

// deno-lint-ignore no-explicit-any
export function hasIterator(object: any) {
  return !!(object != null && object[Symbol.iterator]);
}

export function isA<T>(typeName: string, value: unknown): value is T {
  return Object.prototype.toString.apply(value) === `[object ${typeName}]`;
}

function isObject(a: unknown) {
  return a !== null && typeof a === "object";
}

function isObjectWithKeys(a: unknown) {
  return (
    isObject(a) &&
    !(a instanceof Error) &&
    !Array.isArray(a) &&
    !(a instanceof Date) &&
    !(a instanceof Set) &&
    !(a instanceof Map)
  );
}

function getObjectKeys(object: object): Array<string | symbol> {
  return [
    ...Object.keys(object),
    ...Object.getOwnPropertySymbols(object).filter(
      (s) => Object.getOwnPropertyDescriptor(object, s)?.enumerable,
    ),
  ];
}

function hasPropertyInObject(object: object, key: string | symbol): boolean {
  const shouldTerminate = !object || typeof object !== "object" ||
    object === Object.prototype;

  if (shouldTerminate) {
    return false;
  }

  return (
    Object.prototype.hasOwnProperty.call(object, key) ||
    hasPropertyInObject(Object.getPrototypeOf(object), key)
  );
}

// deno-lint-ignore no-explicit-any
function entries(obj: any) {
  if (!isObject(obj)) return [];

  return Object.getOwnPropertySymbols(obj)
    .filter((key) => key !== Symbol.iterator)
    .map((key) => [key, obj[key as keyof typeof obj]])
    .concat(Object.entries(obj));
}

// Ported from https://github.com/jestjs/jest/blob/442c7f692e3a92f14a2fb56c1737b26fc663a0ef/packages/expect-utils/src/utils.ts#L173
export function iterableEquality(
  // deno-lint-ignore no-explicit-any
  a: any,
  // deno-lint-ignore no-explicit-any
  b: any,
  customTesters: Tester[] = [],
  aStack: unknown[] = [],
  bStack: unknown[] = [],
): boolean | undefined {
  if (
    typeof a !== "object" ||
    typeof b !== "object" ||
    Array.isArray(a) ||
    Array.isArray(b) ||
    !hasIterator(a) ||
    !hasIterator(b)
  ) {
    return undefined;
  }
  if (a.constructor !== b.constructor) {
    return false;
  }
  let length = aStack.length;
  while (length--) {
    // Linear search. Performance is inversely proportional to the number of
    // unique nested structures.
    // circular references at same depth are equal
    // circular reference is not equal to non-circular one
    if (aStack[length] === a) {
      return bStack[length] === b;
    }
  }
  aStack.push(a);
  bStack.push(b);

  // deno-lint-ignore no-explicit-any
  const iterableEqualityWithStack = (a: any, b: any) =>
    iterableEquality(
      a,
      b,
      [...filteredCustomTesters],
      [...aStack],
      [...bStack],
    );

  // Replace any instance of iterableEquality with the new
  // iterableEqualityWithStack so we can do circular detection
  const filteredCustomTesters: Tester[] = [
    ...customTesters.filter((t) => t !== iterableEquality),
    iterableEqualityWithStack,
  ];

  if (a.size !== undefined) {
    if (a.size !== b.size) {
      return false;
    } else if (isA<Set<unknown>>("Set", a)) {
      let allFound = true;
      for (const aValue of a) {
        if (!b.has(aValue)) {
          let has = false;
          for (const bValue of b) {
            const isEqual = equal(aValue, bValue, {
              customTesters: filteredCustomTesters,
            });
            if (isEqual === true) {
              has = true;
            }
          }

          if (has === false) {
            allFound = false;
            break;
          }
        }
      }
      // Remove the first value from the stack of traversed values.
      aStack.pop();
      bStack.pop();
      return allFound;
    } else if (isA<Map<unknown, unknown>>("Map", a)) {
      let allFound = true;
      for (const aEntry of a) {
        if (
          !b.has(aEntry[0]) ||
          !equal(aEntry[1], b.get(aEntry[0]), {
            customTesters: filteredCustomTesters,
          })
        ) {
          let has = false;
          for (const bEntry of b) {
            const matchedKey = equal(
              aEntry[0],
              bEntry[0],
              { customTesters: filteredCustomTesters },
            );

            let matchedValue = false;
            if (matchedKey === true) {
              matchedValue = equal(
                aEntry[1],
                bEntry[1],
                { customTesters: filteredCustomTesters },
              );
            }
            if (matchedValue === true) {
              has = true;
            }
          }

          if (has === false) {
            allFound = false;
            break;
          }
        }
      }
      // Remove the first value from the stack of traversed values.
      aStack.pop();
      bStack.pop();
      return allFound;
    }
  }

  const bIterator = b[Symbol.iterator]();

  for (const aValue of a) {
    const nextB = bIterator.next();
    if (
      nextB.done ||
      !equal(aValue, nextB.value, { customTesters: filteredCustomTesters })
    ) {
      return false;
    }
  }
  if (!bIterator.next().done) {
    return false;
  }

  const aEntries = entries(a);
  const bEntries = entries(b);
  if (!equal(aEntries, bEntries)) {
    return false;
  }

  // Remove the first value from the stack of traversed values.
  aStack.pop();
  bStack.pop();
  return true;
}

// Ported from https://github.com/jestjs/jest/blob/442c7f692e3a92f14a2fb56c1737b26fc663a0ef/packages/expect-utils/src/utils.ts#L341
export function subsetEquality(
  object: unknown,
  subset: unknown,
  customTesters: Tester[] = [],
): boolean | undefined {
  const filteredCustomTesters = customTesters.filter((t) =>
    t !== subsetEquality
  );

  const subsetEqualityWithContext =
    (seenReferences: WeakMap<object, boolean> = new WeakMap()) =>
    // deno-lint-ignore no-explicit-any
    (object: any, subset: any): boolean | undefined => {
      if (!isObjectWithKeys(subset)) {
        return undefined;
      }

      if (seenReferences.has(subset)) return undefined;
      seenReferences.set(subset, true);

      const matchResult = getObjectKeys(subset).every((key) => {
        if (isObjectWithKeys(subset[key])) {
          if (seenReferences.has(subset[key])) {
            return equal(object[key], subset[key], {
              customTesters: filteredCustomTesters,
            });
          }
        }
        const result = object != null &&
          hasPropertyInObject(object, key) &&
          equal(object[key], subset[key], {
            customTesters: [
              ...filteredCustomTesters,
              subsetEqualityWithContext(seenReferences),
            ],
          });
        seenReferences.delete(subset[key]);
        return result;
      });
      seenReferences.delete(subset);

      return matchResult;
    };

  return subsetEqualityWithContext()(object, subset);
}