Skip to content

Commit

Permalink
Remove deprecated methods and improve Class definition (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornstar authored Jun 26, 2024
1 parent 92699e1 commit 8cbcaee
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 35 deletions.
30 changes: 3 additions & 27 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,10 @@ const is = Object.assign(
boundFunction: isBoundFunction,
buffer: isBuffer,
class: isClass,
/** @deprecated Renamed to `class`. */
class_: isClass,
dataView: isDataView,
date: isDate,
detect,
directInstanceOf: isDirectInstanceOf,
/** @deprecated Renamed to `htmlElement` */
domElement: isHtmlElement,
emptyArray: isEmptyArray,
emptyMap: isEmptyMap,
emptyObject: isEmptyObject,
Expand All @@ -258,8 +254,6 @@ const is = Object.assign(
float64Array: isFloat64Array,
formData: isFormData,
function: isFunction,
/** @deprecated Renamed to `function`. */
function_: isFunction,
generator: isGenerator,
generatorFunction: isGeneratorFunction,
htmlElement: isHtmlElement,
Expand All @@ -282,8 +276,6 @@ const is = Object.assign(
nonEmptyString: isNonEmptyString,
nonEmptyStringAndNotWhitespace: isNonEmptyStringAndNotWhitespace,
null: isNull,
/** @deprecated Renamed to `null`. */
null_: isNull,
nullOrUndefined: isNullOrUndefined,
number: isNumber,
numericString: isNumericString,
Expand Down Expand Up @@ -403,7 +395,7 @@ export function isBuffer(value: unknown): value is Buffer {
return (value as any)?.constructor?.isBuffer?.(value) ?? false;
}

export function isClass(value: unknown): value is Class {
export function isClass<T = unknown>(value: unknown): value is Class<T> {
return isFunction(value) && value.toString().startsWith('class ');
}

Expand Down Expand Up @@ -832,17 +824,9 @@ type Assert = {
bigint: (value: unknown, message?: string) => asserts value is bigint;
// eslint-disable-next-line @typescript-eslint/ban-types
function: (value: unknown, message?: string) => asserts value is Function;
/** @deprecated Renamed to `function`. */
// eslint-disable-next-line @typescript-eslint/ban-types
function_: (value: unknown, message?: string) => asserts value is Function;
// eslint-disable-next-line @typescript-eslint/ban-types
null: (value: unknown, message?: string) => asserts value is null;
/** @deprecated Renamed to `null`. */
// eslint-disable-next-line @typescript-eslint/ban-types
null_: (value: unknown, message?: string) => asserts value is null;
class: (value: unknown, message?: string) => asserts value is Class;
/** @deprecated Renamed to `class`. */
class_: (value: unknown, message?: string) => asserts value is Class;
class: <T = unknown>(value: unknown, message?: string) => asserts value is Class<T>;
boolean: (value: unknown, message?: string) => asserts value is boolean;
symbol: (value: unknown, message?: string) => asserts value is symbol;
numericString: (value: unknown, message?: string) => asserts value is `${number}`;
Expand Down Expand Up @@ -902,8 +886,6 @@ type Assert = {
typedArray: (value: unknown, message?: string) => asserts value is TypedArray;
arrayLike: <T = unknown>(value: unknown, message?: string) => asserts value is ArrayLike<T>;
tupleLike: <T extends Array<TypeGuard<unknown>>>(value: unknown, guards: [...T], message?: string) => asserts value is ResolveTypesOfTypeGuardsTuple<T>;
/** @deprecated Renamed to `htmlElement` */
domElement: (value: unknown, message?: string) => asserts value is HTMLElement;
htmlElement: (value: unknown, message?: string) => asserts value is HTMLElement;
observable: (value: unknown, message?: string) => asserts value is ObservableLike;
nodeStream: (value: unknown, message?: string) => asserts value is NodeStream;
Expand Down Expand Up @@ -958,11 +940,9 @@ export const assert: Assert = {
boundFunction: assertBoundFunction,
buffer: assertBuffer,
class: assertClass,
class_: assertClass,
dataView: assertDataView,
date: assertDate,
directInstanceOf: assertDirectInstanceOf,
domElement: assertHtmlElement,
emptyArray: assertEmptyArray,
emptyMap: assertEmptyMap,
emptyObject: assertEmptyObject,
Expand All @@ -977,7 +957,6 @@ export const assert: Assert = {
float64Array: assertFloat64Array,
formData: assertFormData,
function: assertFunction,
function_: assertFunction,
generator: assertGenerator,
generatorFunction: assertGeneratorFunction,
htmlElement: assertHtmlElement,
Expand All @@ -1000,7 +979,6 @@ export const assert: Assert = {
nonEmptyString: assertNonEmptyString,
nonEmptyStringAndNotWhitespace: assertNonEmptyStringAndNotWhitespace,
null: assertNull,
null_: assertNull,
nullOrUndefined: assertNullOrUndefined,
number: assertNumber,
numericString: assertNumericString,
Expand Down Expand Up @@ -1056,8 +1034,6 @@ const methodTypeMap = {
isDataView: 'DataView',
isDate: 'Date',
isDirectInstanceOf: 'T',
/** @deprecated */
isDomElement: 'HTMLElement',
isEmptyArray: 'empty array',
isEmptyMap: 'empty map',
isEmptyObject: 'empty object',
Expand Down Expand Up @@ -1247,7 +1223,7 @@ export function assertBuffer(value: unknown, message?: string): asserts value is
}
}

export function assertClass(value: unknown, message?: string): asserts value is Class {
export function assertClass<T>(value: unknown, message?: string): asserts value is Class<T> {
if (!isClass(value)) {
throw new TypeError(message ?? typeErrorMessage('Class', value));
}
Expand Down
6 changes: 2 additions & 4 deletions source/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ export type Primitive =
/**
Matches a [`class` constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes).
*/
/// type Constructor<T, Arguments extends unknown[] = any[]> = new(...arguments_: Arguments) => T;
type Constructor<T, Arguments extends unknown[] = any[]> = new(...arguments_: Arguments) => T;

/**
Matches a [`class`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes).
*/
// TODO: Use the below in the next major version.
// export type Class<T, Arguments extends unknown[] = any[]> = Constructor<T, Arguments> & {prototype: T};
export type Class<T = unknown, Arguments extends any[] = any[]> = new (...arguments_: Arguments) => T;
export type Class<T, Arguments extends unknown[] = any[]> = Constructor<T, Arguments> & {prototype: T};

/**
Matches any [typed array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray), like `Uint8Array` or `Float64Array`.
Expand Down
8 changes: 4 additions & 4 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const types = new Map<string, Test>([
}],
['null', {
is: is.null,
assert: assert.null_,
assert: assert.null,
fixtures: [
null,
],
Expand Down Expand Up @@ -161,7 +161,7 @@ const types = new Map<string, Test>([
}],
['function', {
is: is.function,
assert: assert.function_,
assert: assert.function,
fixtures: [
function foo() {}, // eslint-disable-line func-names
function () {},
Expand Down Expand Up @@ -840,7 +840,7 @@ test('is.asyncFunction', t => {
t.true(is.function(fixture().then));

t.notThrows(() => {
assert.function_(fixture().then);
assert.function(fixture().then);
});
}
});
Expand Down Expand Up @@ -1370,7 +1370,7 @@ test('is.class', t => {
t.true(is.class(classDeclaration));

t.notThrows(() => {
assert.class_(classDeclaration);
assert.class(classDeclaration);
});
}
});
Expand Down

0 comments on commit 8cbcaee

Please sign in to comment.