Skip to content

Commit 0b4a6ec

Browse files
authored
refactor(@jest/expect-utils): move immutable utils to a separate file (#13357)
1 parent d75433d commit 0b4a6ec

File tree

3 files changed

+81
-69
lines changed

3 files changed

+81
-69
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
*/
8+
9+
// SENTINEL constants are from https://github.com/immutable-js/immutable-js/tree/main/src/predicates
10+
const IS_KEYED_SENTINEL = '@@__IMMUTABLE_KEYED__@@';
11+
const IS_SET_SENTINEL = '@@__IMMUTABLE_SET__@@';
12+
const IS_LIST_SENTINEL = '@@__IMMUTABLE_LIST__@@';
13+
const IS_ORDERED_SENTINEL = '@@__IMMUTABLE_ORDERED__@@';
14+
const IS_RECORD_SYMBOL = '@@__IMMUTABLE_RECORD__@@';
15+
16+
function isObjectLiteral(source: unknown): source is Record<string, unknown> {
17+
return source != null && typeof source === 'object' && !Array.isArray(source);
18+
}
19+
20+
export function isImmutableUnorderedKeyed(source: unknown): boolean {
21+
return Boolean(
22+
source &&
23+
isObjectLiteral(source) &&
24+
source[IS_KEYED_SENTINEL] &&
25+
!source[IS_ORDERED_SENTINEL],
26+
);
27+
}
28+
29+
export function isImmutableUnorderedSet(source: unknown): boolean {
30+
return Boolean(
31+
source &&
32+
isObjectLiteral(source) &&
33+
source[IS_SET_SENTINEL] &&
34+
!source[IS_ORDERED_SENTINEL],
35+
);
36+
}
37+
38+
export function isImmutableList(source: unknown): boolean {
39+
return Boolean(source && isObjectLiteral(source) && source[IS_LIST_SENTINEL]);
40+
}
41+
42+
export function isImmutableOrderedKeyed(source: unknown): boolean {
43+
return Boolean(
44+
source &&
45+
isObjectLiteral(source) &&
46+
source[IS_KEYED_SENTINEL] &&
47+
source[IS_ORDERED_SENTINEL],
48+
);
49+
}
50+
51+
export function isImmutableOrderedSet(source: unknown): boolean {
52+
return Boolean(
53+
source &&
54+
isObjectLiteral(source) &&
55+
source[IS_SET_SENTINEL] &&
56+
source[IS_ORDERED_SENTINEL],
57+
);
58+
}
59+
60+
export function isImmutableRecord(source: unknown): boolean {
61+
return Boolean(source && isObjectLiteral(source) && source[IS_RECORD_SYMBOL]);
62+
}

packages/expect-utils/src/jasmineUtils.ts

+17-66
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2222
2323
*/
2424

25-
/* eslint-disable */
26-
2725
import type {Tester} from './types';
2826

2927
export type EqualsFunction = (
@@ -44,8 +42,8 @@ function isAsymmetric(obj: any) {
4442
}
4543

4644
function asymmetricMatch(a: any, b: any) {
47-
var asymmetricA = isAsymmetric(a),
48-
asymmetricB = isAsymmetric(b);
45+
const asymmetricA = isAsymmetric(a);
46+
const asymmetricB = isAsymmetric(b);
4947

5048
if (asymmetricA && asymmetricB) {
5149
return undefined;
@@ -70,15 +68,15 @@ function eq(
7068
customTesters: Array<Tester>,
7169
strictCheck: boolean | undefined,
7270
): boolean {
73-
var result = true;
71+
let result = true;
7472

75-
var asymmetricResult = asymmetricMatch(a, b);
73+
const asymmetricResult = asymmetricMatch(a, b);
7674
if (asymmetricResult !== undefined) {
7775
return asymmetricResult;
7876
}
7977

80-
for (var i = 0; i < customTesters.length; i++) {
81-
var customTesterResult = customTesters[i](a, b);
78+
for (let i = 0; i < customTesters.length; i++) {
79+
const customTesterResult = customTesters[i](a, b);
8280
if (customTesterResult !== undefined) {
8381
return customTesterResult;
8482
}
@@ -95,7 +93,7 @@ function eq(
9593
if (a === null || b === null) {
9694
return a === b;
9795
}
98-
var className = Object.prototype.toString.call(a);
96+
const className = Object.prototype.toString.call(a);
9997
if (className != Object.prototype.toString.call(b)) {
10098
return false;
10199
}
@@ -132,7 +130,7 @@ function eq(
132130
}
133131

134132
// Used to detect circular references.
135-
var length = aStack.length;
133+
let length = aStack.length;
136134
while (length--) {
137135
// Linear search. Performance is inversely proportional to the number of
138136
// unique nested structures.
@@ -154,19 +152,19 @@ function eq(
154152
}
155153

156154
// Deep compare objects.
157-
var aKeys = keys(a, hasKey),
158-
key;
155+
const aKeys = keys(a, hasKey);
156+
let key;
159157

160-
var bKeys = keys(b, hasKey);
158+
const bKeys = keys(b, hasKey);
161159
// Add keys corresponding to asymmetric matchers if they miss in non strict check mode
162160
if (!strictCheck) {
163-
for (var index = 0; index !== bKeys.length; ++index) {
161+
for (let index = 0; index !== bKeys.length; ++index) {
164162
key = bKeys[index];
165163
if ((isAsymmetric(b[key]) || b[key] === undefined) && !hasKey(a, key)) {
166164
aKeys.push(key);
167165
}
168166
}
169-
for (var index = 0; index !== aKeys.length; ++index) {
167+
for (let index = 0; index !== aKeys.length; ++index) {
170168
key = aKeys[index];
171169
if ((isAsymmetric(a[key]) || a[key] === undefined) && !hasKey(b, key)) {
172170
bKeys.push(key);
@@ -175,7 +173,7 @@ function eq(
175173
}
176174

177175
// Ensure that both objects contain the same number of properties before comparing deep equality.
178-
var size = aKeys.length;
176+
let size = aKeys.length;
179177
if (bKeys.length !== size) {
180178
return false;
181179
}
@@ -205,8 +203,8 @@ function eq(
205203
}
206204

207205
function keys(obj: object, hasKey: (obj: object, key: string) => boolean) {
208-
var keys = [];
209-
for (var key in obj) {
206+
const keys = [];
207+
for (const key in obj) {
210208
if (hasKey(obj, key)) {
211209
keys.push(key);
212210
}
@@ -225,7 +223,7 @@ function hasKey(obj: any, key: string) {
225223
}
226224

227225
export function isA<T>(typeName: string, value: unknown): value is T {
228-
return Object.prototype.toString.apply(value) === '[object ' + typeName + ']';
226+
return Object.prototype.toString.apply(value) === `[object ${typeName}]`;
229227
}
230228

231229
function isDomNode(obj: any): boolean {
@@ -237,50 +235,3 @@ function isDomNode(obj: any): boolean {
237235
typeof obj.isEqualNode === 'function'
238236
);
239237
}
240-
241-
// SENTINEL constants are from https://github.com/immutable-js/immutable-js/tree/main/src/predicates
242-
const IS_KEYED_SENTINEL = '@@__IMMUTABLE_KEYED__@@';
243-
const IS_SET_SENTINEL = '@@__IMMUTABLE_SET__@@';
244-
const IS_LIST_SENTINEL = '@@__IMMUTABLE_LIST__@@';
245-
const IS_ORDERED_SENTINEL = '@@__IMMUTABLE_ORDERED__@@';
246-
const IS_RECORD_SYMBOL = '@@__IMMUTABLE_RECORD__@@';
247-
248-
export function isImmutableUnorderedKeyed(maybeKeyed: any) {
249-
return !!(
250-
maybeKeyed &&
251-
maybeKeyed[IS_KEYED_SENTINEL] &&
252-
!maybeKeyed[IS_ORDERED_SENTINEL]
253-
);
254-
}
255-
256-
export function isImmutableUnorderedSet(maybeSet: any) {
257-
return !!(
258-
maybeSet &&
259-
maybeSet[IS_SET_SENTINEL] &&
260-
!maybeSet[IS_ORDERED_SENTINEL]
261-
);
262-
}
263-
264-
export function isImmutableList(maybeList: any) {
265-
return !!(maybeList && maybeList[IS_LIST_SENTINEL]);
266-
}
267-
268-
export function isImmutableOrderedKeyed(maybeKeyed: any) {
269-
return !!(
270-
maybeKeyed &&
271-
maybeKeyed[IS_KEYED_SENTINEL] &&
272-
maybeKeyed[IS_ORDERED_SENTINEL]
273-
);
274-
}
275-
276-
export function isImmutableOrderedSet(maybeSet: any) {
277-
return !!(
278-
maybeSet &&
279-
maybeSet[IS_SET_SENTINEL] &&
280-
maybeSet[IS_ORDERED_SENTINEL]
281-
);
282-
}
283-
284-
export function isImmutableRecord(maybeSet: any) {
285-
return !!(maybeSet && maybeSet[IS_RECORD_SYMBOL]);
286-
}

packages/expect-utils/src/utils.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@
88

99
import {isPrimitive} from 'jest-get-type';
1010
import {
11-
equals,
12-
isA,
1311
isImmutableList,
1412
isImmutableOrderedKeyed,
1513
isImmutableOrderedSet,
1614
isImmutableRecord,
1715
isImmutableUnorderedKeyed,
1816
isImmutableUnorderedSet,
19-
} from './jasmineUtils';
17+
} from './immutableUtils';
18+
import {equals, isA} from './jasmineUtils';
2019

2120
type GetPath = {
2221
hasEndProp?: boolean;

0 commit comments

Comments
 (0)