@tsdotnet/type
Advanced tools
Comparing version 1.0.4 to 1.0.5
@@ -13,8 +13,8 @@ /*! | ||
* Returns true if the target matches the type (instanceof). | ||
* @param target | ||
* @param instance | ||
* @param type | ||
* @returns {T|null} | ||
*/ | ||
function is(target, type) { | ||
return target instanceof type; | ||
function is(instance, type) { | ||
return instance instanceof type; | ||
} | ||
@@ -25,8 +25,8 @@ type_1.is = is; | ||
* Otherwise returns the target as the type. | ||
* @param target | ||
* @param instance | ||
* @param type | ||
* @returns {T|null} | ||
*/ | ||
function as(target, type) { | ||
return target instanceof type ? target : null; | ||
function as(instance, type) { | ||
return instance instanceof type ? instance : null; | ||
} | ||
@@ -49,3 +49,3 @@ type_1.as = as; | ||
function isBoolean(value) { | ||
return typeof value === "boolean" /* Boolean */; | ||
return typeof value === 'boolean'; | ||
} | ||
@@ -60,3 +60,3 @@ type_1.isBoolean = isBoolean; | ||
function isNumber(value, ignoreNaN = false) { | ||
return typeof value === "number" /* Number */ && (!ignoreNaN || !isNaN(value)); | ||
return typeof value === 'number' && (!ignoreNaN || !isNaN(value)); | ||
} | ||
@@ -70,3 +70,3 @@ type_1.isNumber = isNumber; | ||
function isTrueNaN(value) { | ||
return typeof value === "number" /* Number */ && isNaN(value); | ||
return typeof value === 'number' && isNaN(value); | ||
} | ||
@@ -80,3 +80,3 @@ type_1.isTrueNaN = isTrueNaN; | ||
function isString(value) { | ||
return typeof value === "string" /* String */; | ||
return typeof value === 'string'; | ||
} | ||
@@ -112,3 +112,3 @@ type_1.isString = isString; | ||
function isPrimitiveOrSymbol(value, allowUndefined = false) { | ||
return typeof value === "symbol" /* Symbol */ ? true : isPrimitive(value, allowUndefined); | ||
return typeof value === "symbol" /* Symbol */ || isPrimitive(value, allowUndefined); | ||
} | ||
@@ -138,3 +138,3 @@ type_1.isPrimitiveOrSymbol = isPrimitiveOrSymbol; | ||
function isFunction(value) { | ||
return typeof value === "function" /* Function */; | ||
return typeof value === 'function'; | ||
} | ||
@@ -152,3 +152,3 @@ type_1.isFunction = isFunction; | ||
function numberOrNaN(value) { | ||
return isNaN(value) ? NaN : value; | ||
return typeof value === 'number' ? value : NaN; | ||
} | ||
@@ -179,13 +179,14 @@ type_1.numberOrNaN = numberOrNaN; | ||
function hasMemberOfType(instance, property, type) { | ||
return hasMember(instance, property) && typeof instance[property] === type; | ||
return hasMember(instance, property) | ||
&& typeof instance[property] === type; | ||
} | ||
type_1.hasMemberOfType = hasMemberOfType; | ||
/** | ||
* Tests to see if an object has a function of the provide name. | ||
* Tests to see if an object has a function of the specified name. | ||
* @param instance | ||
* @param {string} name | ||
* @returns {instance is T} | ||
* @param {K} name | ||
* @return {instance is {[P in K]: Function} & T} | ||
*/ | ||
function hasMethod(instance, name) { | ||
return hasMemberOfType(instance, name, "function" /* Function */); | ||
return hasMemberOfType(instance, name, 'function'); | ||
} | ||
@@ -226,4 +227,4 @@ type_1.hasMethod = hasMethod; | ||
* Returns null if unable to convert to iterable. | ||
* @param {Iterable<T> | ArrayLike<T>} instance | ||
* @return {Iterable<T>} | ||
* @param {Iterable | ArrayLike} instance | ||
* @return {Iterable} | ||
*/ | ||
@@ -230,0 +231,0 @@ function asIterable(instance) { |
@@ -8,3 +8,3 @@ /*! | ||
export type Primitive = P; | ||
export type PropertyKey = string | number | symbol; | ||
export type PropertyKey = symbol | keyof any; | ||
export const enum Value { | ||
@@ -23,15 +23,15 @@ Boolean = "boolean", | ||
* Returns true if the target matches the type (instanceof). | ||
* @param target | ||
* @param instance | ||
* @param type | ||
* @returns {T|null} | ||
*/ | ||
export function is<T extends object>(target: object, type: new (...params: any[]) => T): target is T; | ||
export function is<T extends object>(instance: object, type: new (...params: any[]) => T): instance is T; | ||
/** | ||
* Returns null if the target does not match the type (instanceof). | ||
* Otherwise returns the target as the type. | ||
* @param target | ||
* @param instance | ||
* @param type | ||
* @returns {T|null} | ||
*/ | ||
export function as<T>(target: object, type: new (...params: any[]) => T): T | null; | ||
export function as<T>(instance: object, type: new (...params: any[]) => T): T | null; | ||
/** | ||
@@ -42,3 +42,3 @@ * Returns true if the value parameter is null or undefined. | ||
*/ | ||
export function isNullOrUndefined(value: any): value is null | undefined; | ||
export function isNullOrUndefined(value: unknown): value is null | undefined; | ||
/** | ||
@@ -49,3 +49,3 @@ * Returns true if the value parameter is a boolean. | ||
*/ | ||
export function isBoolean(value: any): value is boolean; | ||
export function isBoolean(value: unknown): value is boolean; | ||
/** | ||
@@ -57,3 +57,3 @@ * Returns true if the value parameter is a number. | ||
*/ | ||
export function isNumber(value: any, ignoreNaN?: boolean): value is number; | ||
export function isNumber(value: unknown, ignoreNaN?: boolean): value is number; | ||
/** | ||
@@ -64,3 +64,3 @@ * Returns true if is a number and is NaN. | ||
*/ | ||
export function isTrueNaN(value: any): value is number; | ||
export function isTrueNaN(value: unknown): value is number; | ||
/** | ||
@@ -71,3 +71,3 @@ * Returns true if the value parameter is a string. | ||
*/ | ||
export function isString(value: any): value is string; | ||
export function isString(value: unknown): value is string; | ||
/** | ||
@@ -78,3 +78,3 @@ * Returns true if the value is a boolean, string, number, or null. | ||
*/ | ||
export function isPrimitive(value: any): value is NullablePrimitive; | ||
export function isPrimitive(value: unknown): value is NullablePrimitive; | ||
/** | ||
@@ -86,3 +86,3 @@ * Returns true if the value is a boolean, string, number, null, or undefined. | ||
*/ | ||
export function isPrimitive(value: any, allowUndefined: false): value is NullablePrimitive; | ||
export function isPrimitive(value: unknown, allowUndefined: false): value is NullablePrimitive; | ||
/** | ||
@@ -94,3 +94,3 @@ * Returns true if the value is a boolean, string, number, null, or undefined. | ||
*/ | ||
export function isPrimitive(value: any, allowUndefined: boolean): value is NullablePrimitive | undefined; | ||
export function isPrimitive(value: unknown, allowUndefined: boolean): value is NullablePrimitive | undefined; | ||
/** | ||
@@ -101,3 +101,3 @@ * For detecting if the value can be used as a key. | ||
*/ | ||
export function isPrimitiveOrSymbol(value: any): value is NullablePrimitive | symbol; | ||
export function isPrimitiveOrSymbol(value: unknown): value is NullablePrimitive | symbol; | ||
/** | ||
@@ -109,3 +109,3 @@ * For detecting if the value can be used as a key. | ||
*/ | ||
export function isPrimitiveOrSymbol(value: any, allowUndefined: false): value is NullablePrimitive | symbol; | ||
export function isPrimitiveOrSymbol(value: unknown, allowUndefined: false): value is NullablePrimitive | symbol; | ||
/** | ||
@@ -117,3 +117,3 @@ * For detecting if the value can be used as a key. | ||
*/ | ||
export function isPrimitiveOrSymbol(value: any, allowUndefined: boolean): value is NullablePrimitive | symbol | undefined; | ||
export function isPrimitiveOrSymbol(value: unknown, allowUndefined: boolean): value is NullablePrimitive | symbol | undefined; | ||
/** | ||
@@ -124,3 +124,3 @@ * Returns true if the value is a string, number, or symbol. | ||
*/ | ||
export function isPropertyKey(value: any): value is PropertyKey; | ||
export function isPropertyKey(value: unknown): value is PropertyKey; | ||
/** | ||
@@ -131,3 +131,3 @@ * Returns true if the value parameter is a function. | ||
*/ | ||
export function isFunction(value: any): value is (...params: any[]) => any; | ||
export function isFunction(value: unknown): value is (...params: any[]) => unknown; | ||
/** | ||
@@ -138,3 +138,3 @@ * Returns true if the value parameter is an object. | ||
*/ | ||
export function isObject(value: any): value is object; | ||
export function isObject(value: unknown): value is object; | ||
/** | ||
@@ -146,4 +146,4 @@ * Returns true if the value parameter is an object. | ||
*/ | ||
export function isObject(value: any, allowNull: false): value is object; | ||
export function isObject(value: any, allowNull: boolean): value is object | null; | ||
export function isObject(value: unknown, allowNull: false): value is object; | ||
export function isObject(value: unknown, allowNull: boolean): value is object | null; | ||
/** | ||
@@ -154,3 +154,3 @@ * Guarantees a number value or NaN instead. | ||
*/ | ||
export function numberOrNaN(value: any): number; | ||
export function numberOrNaN(value: unknown): number; | ||
/** | ||
@@ -164,19 +164,65 @@ * Will detect if a member exists (using 'in'). | ||
*/ | ||
export function hasMember(instance: any, property: PropertyKey, verify?: boolean): boolean; | ||
export function hasMember<T = unknown, K extends PropertyKey = keyof T>(instance: unknown, property: K, verify?: boolean): instance is { | ||
[P in K]: unknown; | ||
} & T; | ||
/** | ||
* Returns true if the member matches the type. | ||
* Returns true if the member is a string. | ||
* @param instance | ||
* @param property | ||
* @param type | ||
* @returns {boolean} | ||
* @param {K} property | ||
* @param {"string"} type | ||
* @return {instance is {[P in K]: string} & T} | ||
*/ | ||
export function hasMemberOfType<T>(instance: any, property: PropertyKey, type: Literal): instance is T; | ||
export function hasMemberOfType<T = unknown, K extends PropertyKey = keyof T>(instance: unknown, property: K, type: 'string'): instance is { | ||
[P in K]: string; | ||
} & T; | ||
/** | ||
* Tests to see if an object has a function of the provide name. | ||
* Returns true if the member is a number. | ||
* @param instance | ||
* @param {string} name | ||
* @returns {instance is T} | ||
* @param {K} property | ||
* @param {"number"} type | ||
* @return {instance is {[P in K]: number} & T} | ||
*/ | ||
export function hasMethod<T>(instance: any, name: PropertyKey): instance is T; | ||
export function hasMemberOfType<T = unknown, K extends PropertyKey = keyof T>(instance: unknown, property: K, type: 'number'): instance is { | ||
[P in K]: number; | ||
} & T; | ||
/** | ||
* Returns true if the member is a boolean. | ||
* @param instance | ||
* @param {K} property | ||
* @param {"boolean"} type | ||
* @return {instance is {[P in K]: boolean} & T} | ||
*/ | ||
export function hasMemberOfType<T = unknown, K extends PropertyKey = keyof T>(instance: unknown, property: K, type: 'boolean'): instance is { | ||
[P in K]: boolean; | ||
} & T; | ||
/** | ||
* Returns true if the member is a object. | ||
* @param instance | ||
* @param {K} property | ||
* @param {"object"} type | ||
* @return {instance is {[P in K]: object} & T} | ||
*/ | ||
export function hasMemberOfType<T = unknown, K extends PropertyKey = keyof T>(instance: unknown, property: K, type: 'object'): instance is { | ||
[P in K]: object; | ||
} & T; | ||
/** | ||
* Returns true if the member is a Function. | ||
* @param instance | ||
* @param {K} property | ||
* @param {"function"} type | ||
* @return {instance is {[P in K]: Function} & T} | ||
*/ | ||
export function hasMemberOfType<T = unknown, K extends PropertyKey = keyof T>(instance: unknown, property: K, type: 'function'): instance is { | ||
[P in K]: Function; | ||
} & T; | ||
/** | ||
* Tests to see if an object has a function of the specified name. | ||
* @param instance | ||
* @param {K} name | ||
* @return {instance is {[P in K]: Function} & T} | ||
*/ | ||
export function hasMethod<T = unknown, K extends PropertyKey = keyof T>(instance: unknown, name: K): instance is { | ||
[P in K]: (...params: any[]) => unknown; | ||
} & T; | ||
/** | ||
* Checks to see if object is an array or something with length property that isn't a function. | ||
@@ -186,3 +232,3 @@ * @param instance | ||
*/ | ||
export function isArrayLike<T>(instance: any): instance is ArrayLikeWritable<T>; | ||
export function isArrayLike<T = unknown>(instance: unknown): instance is ArrayLikeWritable<T>; | ||
/** | ||
@@ -193,3 +239,3 @@ * Checks to see if [Symbol.iterator] is a function. | ||
*/ | ||
export function isIterable<T>(instance: any): instance is Iterable<T>; | ||
export function isIterable<T = unknown>(instance: unknown): instance is Iterable<T>; | ||
/** | ||
@@ -208,5 +254,5 @@ * Ensures an object is iterable if possible. | ||
*/ | ||
export function asIterable<T = unknown>(instance: any): Iterable<T> | null; | ||
export function asIterable<T = unknown>(instance: unknown): Iterable<T> | null; | ||
export {}; | ||
} | ||
export default type; |
@@ -16,8 +16,8 @@ "use strict"; | ||
* Returns true if the target matches the type (instanceof). | ||
* @param target | ||
* @param instance | ||
* @param type | ||
* @returns {T|null} | ||
*/ | ||
function is(target, type) { | ||
return target instanceof type; | ||
function is(instance, type) { | ||
return instance instanceof type; | ||
} | ||
@@ -28,8 +28,8 @@ type_1.is = is; | ||
* Otherwise returns the target as the type. | ||
* @param target | ||
* @param instance | ||
* @param type | ||
* @returns {T|null} | ||
*/ | ||
function as(target, type) { | ||
return target instanceof type ? target : null; | ||
function as(instance, type) { | ||
return instance instanceof type ? instance : null; | ||
} | ||
@@ -52,3 +52,3 @@ type_1.as = as; | ||
function isBoolean(value) { | ||
return typeof value === "boolean" /* Boolean */; | ||
return typeof value === 'boolean'; | ||
} | ||
@@ -63,3 +63,3 @@ type_1.isBoolean = isBoolean; | ||
function isNumber(value, ignoreNaN = false) { | ||
return typeof value === "number" /* Number */ && (!ignoreNaN || !isNaN(value)); | ||
return typeof value === 'number' && (!ignoreNaN || !isNaN(value)); | ||
} | ||
@@ -73,3 +73,3 @@ type_1.isNumber = isNumber; | ||
function isTrueNaN(value) { | ||
return typeof value === "number" /* Number */ && isNaN(value); | ||
return typeof value === 'number' && isNaN(value); | ||
} | ||
@@ -83,3 +83,3 @@ type_1.isTrueNaN = isTrueNaN; | ||
function isString(value) { | ||
return typeof value === "string" /* String */; | ||
return typeof value === 'string'; | ||
} | ||
@@ -115,3 +115,3 @@ type_1.isString = isString; | ||
function isPrimitiveOrSymbol(value, allowUndefined = false) { | ||
return typeof value === "symbol" /* Symbol */ ? true : isPrimitive(value, allowUndefined); | ||
return typeof value === "symbol" /* Symbol */ || isPrimitive(value, allowUndefined); | ||
} | ||
@@ -141,3 +141,3 @@ type_1.isPrimitiveOrSymbol = isPrimitiveOrSymbol; | ||
function isFunction(value) { | ||
return typeof value === "function" /* Function */; | ||
return typeof value === 'function'; | ||
} | ||
@@ -155,3 +155,3 @@ type_1.isFunction = isFunction; | ||
function numberOrNaN(value) { | ||
return isNaN(value) ? NaN : value; | ||
return typeof value === 'number' ? value : NaN; | ||
} | ||
@@ -182,13 +182,14 @@ type_1.numberOrNaN = numberOrNaN; | ||
function hasMemberOfType(instance, property, type) { | ||
return hasMember(instance, property) && typeof instance[property] === type; | ||
return hasMember(instance, property) | ||
&& typeof instance[property] === type; | ||
} | ||
type_1.hasMemberOfType = hasMemberOfType; | ||
/** | ||
* Tests to see if an object has a function of the provide name. | ||
* Tests to see if an object has a function of the specified name. | ||
* @param instance | ||
* @param {string} name | ||
* @returns {instance is T} | ||
* @param {K} name | ||
* @return {instance is {[P in K]: Function} & T} | ||
*/ | ||
function hasMethod(instance, name) { | ||
return hasMemberOfType(instance, name, "function" /* Function */); | ||
return hasMemberOfType(instance, name, 'function'); | ||
} | ||
@@ -229,4 +230,4 @@ type_1.hasMethod = hasMethod; | ||
* Returns null if unable to convert to iterable. | ||
* @param {Iterable<T> | ArrayLike<T>} instance | ||
* @return {Iterable<T>} | ||
* @param {Iterable | ArrayLike} instance | ||
* @return {Iterable} | ||
*/ | ||
@@ -233,0 +234,0 @@ function asIterable(instance) { |
{ | ||
"name": "@tsdotnet/type", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "A set of useful utility functions for JavaScript run-time type checking and inspection.", | ||
@@ -5,0 +5,0 @@ "author": "electricessence", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
36303
721