diff --git a/expect/_matchers.ts b/expect/_matchers.ts index 84a43c6def13..561ce28f6aed 100644 --- a/expect/_matchers.ts +++ b/expect/_matchers.ts @@ -16,7 +16,7 @@ import { equal } from "./_equal.ts"; import { format } from "./_format.ts"; import { getMockCalls } from "./_mock_util.ts"; import { inspectArg, inspectArgs } from "./_inspect_args.ts"; -import { iterableEquality } from "./_jest_utils.ts"; +import { iterableEquality } from "./iterable_equality.ts"; import { AnyConstructor, MatcherContext, MatchResult } from "./_types.ts"; import { buildEqualOptions } from "./_utils.ts"; diff --git a/expect/_utils.ts b/expect/_utils.ts index 36ad0117be08..56c29b4bdfaf 100644 --- a/expect/_utils.ts +++ b/expect/_utils.ts @@ -18,3 +18,82 @@ export function isPromiseLike(value: unknown): value is PromiseLike { return typeof ((value as Record).then) === "function"; } } + +// SENTINEL constants are from https://github.com/facebook/immutable-js +const IS_KEYED_SENTINEL = "@@__IMMUTABLE_KEYED__@@"; +const IS_SET_SENTINEL = "@@__IMMUTABLE_SET__@@"; +const IS_LIST_SENTINEL = "@@__IMMUTABLE_LIST__@@"; +const IS_ORDERED_SENTINEL = "@@__IMMUTABLE_ORDERED__@@"; +const IS_RECORD_SYMBOL = "@@__IMMUTABLE_RECORD__@@"; + +function isObjectLiteral(source: unknown): source is Record { + return source != null && typeof source === "object" && !Array.isArray(source); +} + +export function isImmutableUnorderedKeyed(source: unknown): boolean { + return Boolean( + source && + isObjectLiteral(source) && + source[IS_KEYED_SENTINEL] && + !source[IS_ORDERED_SENTINEL], + ); +} + +export function isImmutableUnorderedSet(source: unknown): boolean { + return Boolean( + source && + isObjectLiteral(source) && + source[IS_SET_SENTINEL] && + !source[IS_ORDERED_SENTINEL], + ); +} + +export function isImmutableList(source: unknown): boolean { + return Boolean(source && isObjectLiteral(source) && source[IS_LIST_SENTINEL]); +} + +export function isImmutableOrderedKeyed(source: unknown): boolean { + return Boolean( + source && + isObjectLiteral(source) && + source[IS_KEYED_SENTINEL] && + source[IS_ORDERED_SENTINEL], + ); +} + +export function isImmutableOrderedSet(source: unknown): boolean { + return Boolean( + source && + isObjectLiteral(source) && + source[IS_SET_SENTINEL] && + source[IS_ORDERED_SENTINEL], + ); +} + +export function isImmutableRecord(source: unknown): boolean { + return Boolean(source && isObjectLiteral(source) && source[IS_RECORD_SYMBOL]); +} + +// deno-lint-ignore no-explicit-any +export function hasIterator(object: any) { + return !!(object != null && object[Symbol.iterator]); +} + +export function isA(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"; +} + +// deno-lint-ignore no-explicit-any +export function entries(obj: any) { + if (!isObject(obj)) return []; + + const symbolProperties = Object.getOwnPropertySymbols(obj) + .filter((key) => key !== Symbol.iterator) + .map((key) => [key, obj[key]]); + + return [...symbolProperties, ...Object.entries(obj)]; +} diff --git a/expect/_jest_utils.ts b/expect/iterable_equality.ts similarity index 63% rename from expect/_jest_utils.ts rename to expect/iterable_equality.ts index 3ec5037560a1..a48aa9744004 100644 --- a/expect/_jest_utils.ts +++ b/expect/iterable_equality.ts @@ -2,87 +2,17 @@ import { equal } from "./_equal.ts"; import { Tester } from "./_types.ts"; - -const IteratorSymbol = Symbol.iterator; - -// SENTINEL constants are from https://github.com/facebook/immutable-js -const IS_KEYED_SENTINEL = "@@__IMMUTABLE_KEYED__@@"; -const IS_SET_SENTINEL = "@@__IMMUTABLE_SET__@@"; -const IS_LIST_SENTINEL = "@@__IMMUTABLE_LIST__@@"; -const IS_ORDERED_SENTINEL = "@@__IMMUTABLE_ORDERED__@@"; -const IS_RECORD_SYMBOL = "@@__IMMUTABLE_RECORD__@@"; - -function isObjectLiteral(source: unknown): source is Record { - return source != null && typeof source === "object" && !Array.isArray(source); -} - -function isImmutableUnorderedKeyed(source: unknown): boolean { - return Boolean( - source && - isObjectLiteral(source) && - source[IS_KEYED_SENTINEL] && - !source[IS_ORDERED_SENTINEL], - ); -} - -function isImmutableUnorderedSet(source: unknown): boolean { - return Boolean( - source && - isObjectLiteral(source) && - source[IS_SET_SENTINEL] && - !source[IS_ORDERED_SENTINEL], - ); -} - -function isImmutableList(source: unknown): boolean { - return Boolean(source && isObjectLiteral(source) && source[IS_LIST_SENTINEL]); -} - -function isImmutableOrderedKeyed(source: unknown): boolean { - return Boolean( - source && - isObjectLiteral(source) && - source[IS_KEYED_SENTINEL] && - source[IS_ORDERED_SENTINEL], - ); -} - -function isImmutableOrderedSet(source: unknown): boolean { - return Boolean( - source && - isObjectLiteral(source) && - source[IS_SET_SENTINEL] && - source[IS_ORDERED_SENTINEL], - ); -} - -function isImmutableRecord(source: unknown): boolean { - return Boolean(source && isObjectLiteral(source) && source[IS_RECORD_SYMBOL]); -} - -// deno-lint-ignore no-explicit-any -function hasIterator(object: any) { - return !!(object != null && object[IteratorSymbol]); -} - -function isA(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"; -} - -// deno-lint-ignore no-explicit-any -function entries(obj: any) { - if (!isObject(obj)) return []; - - const symbolProperties = Object.getOwnPropertySymbols(obj) - .filter((key) => key !== Symbol.iterator) - .map((key) => [key, obj[key]]); - - return [...symbolProperties, ...Object.entries(obj)]; -} +import { + entries, + hasIterator, + isA, + isImmutableUnorderedSet, + isImmutableUnorderedKeyed, + isImmutableList, + isImmutableOrderedKeyed, + isImmutableOrderedSet, + isImmutableRecord, +} from "./_utils.ts"; export function iterableEquality( // deno-lint-ignore no-explicit-any @@ -209,7 +139,7 @@ export function iterableEquality( } } - const bIterator = b[IteratorSymbol](); + const bIterator = b[Symbol.iterator](); for (const aValue of a) { const nextB = bIterator.next(); diff --git a/expect/mod.ts b/expect/mod.ts index b79709940a6f..a47ac68eaff5 100644 --- a/expect/mod.ts +++ b/expect/mod.ts @@ -85,3 +85,4 @@ */ export { expect } from "./expect.ts"; export { fn } from "./fn.ts"; +export { iterableEquality } from "./iterable_equality.ts";