Skip to content

Commit

Permalink
refactor codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
eryue0220 committed Feb 28, 2024
1 parent 6fa54a9 commit e2c364f
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 83 deletions.
2 changes: 1 addition & 1 deletion expect/_matchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
79 changes: 79 additions & 0 deletions expect/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,82 @@ export function isPromiseLike(value: unknown): value is PromiseLike<unknown> {
return typeof ((value as Record<string, unknown>).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<string, unknown> {
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<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";
}

// 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)];
}
94 changes: 12 additions & 82 deletions expect/_jest_utils.ts → expect/iterable_equality.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown> {
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<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";
}

// 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
Expand Down Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions expect/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,4 @@
*/
export { expect } from "./expect.ts";
export { fn } from "./fn.ts";
export { iterableEquality } from "./iterable_equality.ts";

0 comments on commit e2c364f

Please sign in to comment.