@deepkit/core
Advanced tools
Comparing version 1.0.1-alpha.39 to 1.0.1-alpha.42
@@ -89,3 +89,3 @@ /** | ||
* Returns a human readable string representation from the given value. | ||
*/ | ||
*/ | ||
export declare function stringifyValueWithType(value: any): string; | ||
@@ -120,2 +120,8 @@ /** | ||
/** | ||
* Returns true if given obj is a async function. | ||
* | ||
* @public | ||
*/ | ||
export declare function isAsyncFunction(obj: any): obj is (...args: any[]) => Promise<any>; | ||
/** | ||
* Returns true if given obj is a promise like object. | ||
@@ -122,0 +128,0 @@ * |
@@ -15,3 +15,4 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.inDebugMode = exports.isPrototypeOfBase = exports.isConstructable = exports.getObjectKeysSize = exports.humanBytes = exports.deletePathValue = exports.setPathValue = exports.getPathValue = exports.time = exports.collectForMicrotask = exports.mergeStack = exports.createStack = exports.mergePromiseStack = exports.asyncOperation = exports.appendObject = exports.prependObjectKeys = exports.average = exports.arrayRemoveItem = exports.arrayClear = exports.last = exports.first = exports.lastKey = exports.firstKey = exports.size = exports.empty = exports.copy = exports.sleep = exports.indexOf = exports.arrayHasItem = exports.isString = exports.isNumber = exports.isSet = exports.isUndefined = exports.isNull = exports.isArray = exports.isObject = exports.isClass = exports.isPromise = exports.isFunction = exports.prettyPrintObject = exports.changeClass = exports.stringifyValueWithType = exports.isClassInstance = exports.getClassTypeFromInstance = exports.isPlainObject = exports.typeOf = exports.applyDefaults = exports.getClassPropertyName = exports.getClassName = exports.CustomError = void 0; | ||
exports.isPrototypeOfBase = exports.isConstructable = exports.getObjectKeysSize = exports.humanBytes = exports.deletePathValue = exports.setPathValue = exports.getPathValue = exports.time = exports.collectForMicrotask = exports.mergeStack = exports.createStack = exports.mergePromiseStack = exports.asyncOperation = exports.appendObject = exports.prependObjectKeys = exports.average = exports.arrayRemoveItem = exports.arrayClear = exports.last = exports.first = exports.lastKey = exports.firstKey = exports.size = exports.empty = exports.copy = exports.sleep = exports.indexOf = exports.arrayHasItem = exports.isString = exports.isNumber = exports.isSet = exports.isUndefined = exports.isNull = exports.isArray = exports.isObject = exports.isClass = exports.isPromise = exports.isAsyncFunction = exports.isFunction = exports.prettyPrintObject = exports.changeClass = exports.stringifyValueWithType = exports.isClassInstance = exports.getClassTypeFromInstance = exports.isPlainObject = exports.typeOf = exports.applyDefaults = exports.getClassPropertyName = exports.getClassName = exports.CustomError = void 0; | ||
exports.inDebugMode = void 0; | ||
const dot_prop_1 = __importDefault(require("dot-prop")); | ||
@@ -124,3 +125,3 @@ const iterators_1 = require("./iterators"); | ||
* Returns a human readable string representation from the given value. | ||
*/ | ||
*/ | ||
function stringifyValueWithType(value) { | ||
@@ -187,3 +188,14 @@ if ('string' === typeof value) | ||
exports.isFunction = isFunction; | ||
const AsyncFunction = (async () => { | ||
}).constructor; | ||
/** | ||
* Returns true if given obj is a async function. | ||
* | ||
* @public | ||
*/ | ||
function isAsyncFunction(obj) { | ||
return obj instanceof AsyncFunction; | ||
} | ||
exports.isAsyncFunction = isAsyncFunction; | ||
/** | ||
* Returns true if given obj is a promise like object. | ||
@@ -197,4 +209,4 @@ * | ||
function isPromise(obj) { | ||
return obj !== null && typeof obj === "object" && typeof obj.then === "function" | ||
&& typeof obj.catch === "function" && typeof obj.finally === "function"; | ||
return obj !== null && typeof obj === 'object' && typeof obj.then === 'function' | ||
&& typeof obj.catch === 'function' && typeof obj.finally === 'function'; | ||
} | ||
@@ -480,5 +492,4 @@ exports.isPromise = isPromise; | ||
async function asyncOperation(executor) { | ||
let error, async; | ||
try { | ||
async = await new Promise(async (resolve, reject) => { | ||
return await new Promise(async (resolve, reject) => { | ||
try { | ||
@@ -492,10 +503,6 @@ await executor(resolve, reject); | ||
} | ||
catch (e) { | ||
error = e; | ||
} | ||
if (error) { | ||
catch (error) { | ||
mergeStack(error, createStack()); | ||
throw error; | ||
} | ||
return async; | ||
} | ||
@@ -502,0 +509,0 @@ exports.asyncOperation = asyncOperation; |
@@ -29,4 +29,6 @@ "use strict"; | ||
globals_1.expect(core_1.isObject(undefined)).toBe(false); | ||
globals_1.expect(core_1.isObject(() => { })).toBe(false); | ||
globals_1.expect(core_1.isObject(function () { })).toBe(false); | ||
globals_1.expect(core_1.isObject(() => { | ||
})).toBe(false); | ||
globals_1.expect(core_1.isObject(function () { | ||
})).toBe(false); | ||
globals_1.expect(core_1.isObject(1)).toBe(false); | ||
@@ -44,4 +46,6 @@ globals_1.expect(core_1.isObject('1')).toBe(false); | ||
globals_1.expect(core_1.isPromise(undefined)).toBe(false); | ||
globals_1.expect(core_1.isPromise(() => { })).toBe(false); | ||
globals_1.expect(core_1.isPromise(function () { })).toBe(false); | ||
globals_1.expect(core_1.isPromise(() => { | ||
})).toBe(false); | ||
globals_1.expect(core_1.isPromise(function () { | ||
})).toBe(false); | ||
globals_1.expect(core_1.isPromise(1)).toBe(false); | ||
@@ -51,3 +55,4 @@ globals_1.expect(core_1.isPromise('1')).toBe(false); | ||
} | ||
const foo2 = () => { }; | ||
const foo2 = () => { | ||
}; | ||
globals_1.expect(core_1.isPromise(foo1())).toBe(false); | ||
@@ -65,3 +70,4 @@ globals_1.expect(core_1.isPromise(foo2())).toBe(false); | ||
globals_1.expect(core_1.isObject(await foo4())).toBe(false); | ||
globals_1.expect(core_1.isObject((async () => { })())).toBe(true); | ||
globals_1.expect(core_1.isObject((async () => { | ||
})())).toBe(true); | ||
}); | ||
@@ -80,7 +86,32 @@ globals_1.test('helper isFunction', () => { | ||
globals_1.expect(core_1.isFunction(core_1.isFunction)).toBe(true); | ||
globals_1.expect(core_1.isFunction(() => { })).toBe(true); | ||
globals_1.expect(core_1.isFunction(async () => { })).toBe(true); | ||
globals_1.expect(core_1.isFunction(function () { })).toBe(true); | ||
globals_1.expect(core_1.isFunction(async function () { })).toBe(true); | ||
globals_1.expect(core_1.isFunction(() => { | ||
})).toBe(true); | ||
globals_1.expect(core_1.isFunction(async () => { | ||
})).toBe(true); | ||
globals_1.expect(core_1.isFunction(function () { | ||
})).toBe(true); | ||
globals_1.expect(core_1.isFunction(async function () { | ||
})).toBe(true); | ||
}); | ||
globals_1.test('helper isAsyncFunction', () => { | ||
globals_1.expect(core_1.isAsyncFunction([])).toBe(false); | ||
globals_1.expect(core_1.isAsyncFunction(false)).toBe(false); | ||
globals_1.expect(core_1.isAsyncFunction(true)).toBe(false); | ||
globals_1.expect(core_1.isAsyncFunction(null)).toBe(false); | ||
globals_1.expect(core_1.isAsyncFunction(undefined)).toBe(false); | ||
globals_1.expect(core_1.isAsyncFunction(1)).toBe(false); | ||
globals_1.expect(core_1.isAsyncFunction('1')).toBe(false); | ||
globals_1.expect(core_1.isAsyncFunction({})).toBe(false); | ||
globals_1.expect(core_1.isAsyncFunction(new Date())).toBe(false); | ||
globals_1.expect(core_1.isAsyncFunction(new SimpleClass('asd'))).toBe(false); | ||
globals_1.expect(core_1.isAsyncFunction(core_1.isFunction)).toBe(false); | ||
globals_1.expect(core_1.isAsyncFunction(() => { | ||
})).toBe(false); | ||
globals_1.expect(core_1.isAsyncFunction(async () => { | ||
})).toBe(true); | ||
globals_1.expect(core_1.isAsyncFunction(function () { | ||
})).toBe(false); | ||
globals_1.expect(core_1.isAsyncFunction(async function () { | ||
})).toBe(true); | ||
}); | ||
globals_1.test('helper isClass', () => { | ||
@@ -98,6 +129,10 @@ globals_1.expect(core_1.isClass([])).toBe(false); | ||
globals_1.expect(core_1.isClass(core_1.isFunction)).toBe(false); | ||
globals_1.expect(core_1.isClass(() => { })).toBe(false); | ||
globals_1.expect(core_1.isClass(async () => { })).toBe(false); | ||
globals_1.expect(core_1.isClass(function () { })).toBe(false); | ||
globals_1.expect(core_1.isClass(async function () { })).toBe(false); | ||
globals_1.expect(core_1.isClass(() => { | ||
})).toBe(false); | ||
globals_1.expect(core_1.isClass(async () => { | ||
})).toBe(false); | ||
globals_1.expect(core_1.isClass(function () { | ||
})).toBe(false); | ||
globals_1.expect(core_1.isClass(async function () { | ||
})).toBe(false); | ||
globals_1.expect(core_1.isClass(SimpleClass)).toBe(true); | ||
@@ -113,4 +148,6 @@ }); | ||
globals_1.expect(core_1.isPlainObject('1')).toBe(false); | ||
globals_1.expect(core_1.isPlainObject(() => { })).toBe(false); | ||
globals_1.expect(core_1.isPlainObject(function () { })).toBe(false); | ||
globals_1.expect(core_1.isPlainObject(() => { | ||
})).toBe(false); | ||
globals_1.expect(core_1.isPlainObject(function () { | ||
})).toBe(false); | ||
globals_1.expect(core_1.isPlainObject(new Date())).toBe(false); | ||
@@ -193,3 +230,3 @@ globals_1.expect(core_1.isPlainObject(new SimpleClass('asd'))).toBe(false); | ||
}); | ||
globals_1.test('asyncOperation', async () => { | ||
globals_1.test('asyncOperation maintain error stack trace', async () => { | ||
class MyError extends Error { | ||
@@ -200,2 +237,24 @@ } | ||
async function doIt() { | ||
await core_1.asyncOperation((resolve, reject) => { | ||
setTimeout(() => { | ||
reject(new MyError('MyError1')); | ||
}); | ||
}); | ||
} | ||
await doIt(); | ||
} | ||
catch (error) { | ||
fetched = true; | ||
globals_1.expect(error).toBeInstanceOf(MyError); | ||
globals_1.expect(error.stack).toContain('MyError1'); | ||
globals_1.expect(error.stack).toContain('doIt'); | ||
} | ||
globals_1.expect(fetched).toBe(true); | ||
}); | ||
globals_1.test('asyncOperation catches async errors', async () => { | ||
class MyError extends Error { | ||
} | ||
let fetched = false; | ||
try { | ||
async function doIt() { | ||
await core_1.asyncOperation(async (resolve) => { | ||
@@ -271,10 +330,20 @@ await core_1.sleep(0.2); | ||
}.bind(undefined))).toBe(true); | ||
globals_1.expect(core_1.isConstructable(function () { })).toBe(true); | ||
globals_1.expect(core_1.isConstructable(function () { }.bind(undefined))).toBe(true); | ||
globals_1.expect(core_1.isConstructable(() => { })).toBe(false); | ||
globals_1.expect(core_1.isConstructable((() => { }).bind(undefined))).toBe(false); | ||
globals_1.expect(core_1.isConstructable(async () => { })).toBe(false); | ||
globals_1.expect(core_1.isConstructable(async function () { })).toBe(false); | ||
globals_1.expect(core_1.isConstructable(function* () { })).toBe(false); | ||
globals_1.expect(core_1.isConstructable({ foo() { } }.foo)).toBe(false); | ||
globals_1.expect(core_1.isConstructable(function () { | ||
})).toBe(true); | ||
globals_1.expect(core_1.isConstructable(function () { | ||
}.bind(undefined))).toBe(true); | ||
globals_1.expect(core_1.isConstructable(() => { | ||
})).toBe(false); | ||
globals_1.expect(core_1.isConstructable((() => { | ||
}).bind(undefined))).toBe(false); | ||
globals_1.expect(core_1.isConstructable(async () => { | ||
})).toBe(false); | ||
globals_1.expect(core_1.isConstructable(async function () { | ||
})).toBe(false); | ||
globals_1.expect(core_1.isConstructable(function* () { | ||
})).toBe(false); | ||
globals_1.expect(core_1.isConstructable({ | ||
foo() { | ||
} | ||
}.foo)).toBe(false); | ||
globals_1.expect(core_1.isConstructable(URL)).toBe(true); | ||
@@ -309,3 +378,4 @@ }); | ||
globals_1.expect(core_1.stringifyValueWithType(true)).toBe(`Boolean(true)`); | ||
globals_1.expect(core_1.stringifyValueWithType(function Peter() { })).toBe(`Function Peter`); | ||
globals_1.expect(core_1.stringifyValueWithType(function Peter() { | ||
})).toBe(`Function Peter`); | ||
}); | ||
@@ -312,0 +382,0 @@ globals_1.test('getClassTypeFromInstance', async () => { |
@@ -89,3 +89,3 @@ /** | ||
* Returns a human readable string representation from the given value. | ||
*/ | ||
*/ | ||
export declare function stringifyValueWithType(value: any): string; | ||
@@ -120,2 +120,8 @@ /** | ||
/** | ||
* Returns true if given obj is a async function. | ||
* | ||
* @public | ||
*/ | ||
export declare function isAsyncFunction(obj: any): obj is (...args: any[]) => Promise<any>; | ||
/** | ||
* Returns true if given obj is a promise like object. | ||
@@ -122,0 +128,0 @@ * |
@@ -109,3 +109,3 @@ /* | ||
* Returns a human readable string representation from the given value. | ||
*/ | ||
*/ | ||
export function stringifyValueWithType(value) { | ||
@@ -168,3 +168,13 @@ if ('string' === typeof value) | ||
} | ||
const AsyncFunction = (async () => { | ||
}).constructor; | ||
/** | ||
* Returns true if given obj is a async function. | ||
* | ||
* @public | ||
*/ | ||
export function isAsyncFunction(obj) { | ||
return obj instanceof AsyncFunction; | ||
} | ||
/** | ||
* Returns true if given obj is a promise like object. | ||
@@ -178,4 +188,4 @@ * | ||
export function isPromise(obj) { | ||
return obj !== null && typeof obj === "object" && typeof obj.then === "function" | ||
&& typeof obj.catch === "function" && typeof obj.finally === "function"; | ||
return obj !== null && typeof obj === 'object' && typeof obj.then === 'function' | ||
&& typeof obj.catch === 'function' && typeof obj.finally === 'function'; | ||
} | ||
@@ -437,5 +447,4 @@ /** | ||
export async function asyncOperation(executor) { | ||
let error, async; | ||
try { | ||
async = await new Promise(async (resolve, reject) => { | ||
return await new Promise(async (resolve, reject) => { | ||
try { | ||
@@ -449,10 +458,6 @@ await executor(resolve, reject); | ||
} | ||
catch (e) { | ||
error = e; | ||
} | ||
if (error) { | ||
catch (error) { | ||
mergeStack(error, createStack()); | ||
throw error; | ||
} | ||
return async; | ||
} | ||
@@ -459,0 +464,0 @@ /** |
import { expect, test } from '@jest/globals'; | ||
import { asyncOperation, changeClass, collectForMicrotask, getClassName, getClassTypeFromInstance, getObjectKeysSize, getPathValue, isArray, isClass, isClassInstance, isConstructable, isFunction, isObject, isPlainObject, isPromise, isPrototypeOfBase, isUndefined, setPathValue, sleep, stringifyValueWithType } from '../src/core'; | ||
import { asyncOperation, changeClass, collectForMicrotask, getClassName, getClassTypeFromInstance, getObjectKeysSize, getPathValue, isArray, isAsyncFunction, isClass, isClassInstance, isConstructable, isFunction, isObject, isPlainObject, isPromise, isPrototypeOfBase, isUndefined, setPathValue, sleep, stringifyValueWithType } from '../src/core'; | ||
class SimpleClass { | ||
@@ -27,4 +27,6 @@ constructor(name) { | ||
expect(isObject(undefined)).toBe(false); | ||
expect(isObject(() => { })).toBe(false); | ||
expect(isObject(function () { })).toBe(false); | ||
expect(isObject(() => { | ||
})).toBe(false); | ||
expect(isObject(function () { | ||
})).toBe(false); | ||
expect(isObject(1)).toBe(false); | ||
@@ -42,4 +44,6 @@ expect(isObject('1')).toBe(false); | ||
expect(isPromise(undefined)).toBe(false); | ||
expect(isPromise(() => { })).toBe(false); | ||
expect(isPromise(function () { })).toBe(false); | ||
expect(isPromise(() => { | ||
})).toBe(false); | ||
expect(isPromise(function () { | ||
})).toBe(false); | ||
expect(isPromise(1)).toBe(false); | ||
@@ -49,3 +53,4 @@ expect(isPromise('1')).toBe(false); | ||
} | ||
const foo2 = () => { }; | ||
const foo2 = () => { | ||
}; | ||
expect(isPromise(foo1())).toBe(false); | ||
@@ -63,3 +68,4 @@ expect(isPromise(foo2())).toBe(false); | ||
expect(isObject(await foo4())).toBe(false); | ||
expect(isObject((async () => { })())).toBe(true); | ||
expect(isObject((async () => { | ||
})())).toBe(true); | ||
}); | ||
@@ -78,7 +84,32 @@ test('helper isFunction', () => { | ||
expect(isFunction(isFunction)).toBe(true); | ||
expect(isFunction(() => { })).toBe(true); | ||
expect(isFunction(async () => { })).toBe(true); | ||
expect(isFunction(function () { })).toBe(true); | ||
expect(isFunction(async function () { })).toBe(true); | ||
expect(isFunction(() => { | ||
})).toBe(true); | ||
expect(isFunction(async () => { | ||
})).toBe(true); | ||
expect(isFunction(function () { | ||
})).toBe(true); | ||
expect(isFunction(async function () { | ||
})).toBe(true); | ||
}); | ||
test('helper isAsyncFunction', () => { | ||
expect(isAsyncFunction([])).toBe(false); | ||
expect(isAsyncFunction(false)).toBe(false); | ||
expect(isAsyncFunction(true)).toBe(false); | ||
expect(isAsyncFunction(null)).toBe(false); | ||
expect(isAsyncFunction(undefined)).toBe(false); | ||
expect(isAsyncFunction(1)).toBe(false); | ||
expect(isAsyncFunction('1')).toBe(false); | ||
expect(isAsyncFunction({})).toBe(false); | ||
expect(isAsyncFunction(new Date())).toBe(false); | ||
expect(isAsyncFunction(new SimpleClass('asd'))).toBe(false); | ||
expect(isAsyncFunction(isFunction)).toBe(false); | ||
expect(isAsyncFunction(() => { | ||
})).toBe(false); | ||
expect(isAsyncFunction(async () => { | ||
})).toBe(true); | ||
expect(isAsyncFunction(function () { | ||
})).toBe(false); | ||
expect(isAsyncFunction(async function () { | ||
})).toBe(true); | ||
}); | ||
test('helper isClass', () => { | ||
@@ -96,6 +127,10 @@ expect(isClass([])).toBe(false); | ||
expect(isClass(isFunction)).toBe(false); | ||
expect(isClass(() => { })).toBe(false); | ||
expect(isClass(async () => { })).toBe(false); | ||
expect(isClass(function () { })).toBe(false); | ||
expect(isClass(async function () { })).toBe(false); | ||
expect(isClass(() => { | ||
})).toBe(false); | ||
expect(isClass(async () => { | ||
})).toBe(false); | ||
expect(isClass(function () { | ||
})).toBe(false); | ||
expect(isClass(async function () { | ||
})).toBe(false); | ||
expect(isClass(SimpleClass)).toBe(true); | ||
@@ -111,4 +146,6 @@ }); | ||
expect(isPlainObject('1')).toBe(false); | ||
expect(isPlainObject(() => { })).toBe(false); | ||
expect(isPlainObject(function () { })).toBe(false); | ||
expect(isPlainObject(() => { | ||
})).toBe(false); | ||
expect(isPlainObject(function () { | ||
})).toBe(false); | ||
expect(isPlainObject(new Date())).toBe(false); | ||
@@ -191,3 +228,3 @@ expect(isPlainObject(new SimpleClass('asd'))).toBe(false); | ||
}); | ||
test('asyncOperation', async () => { | ||
test('asyncOperation maintain error stack trace', async () => { | ||
class MyError extends Error { | ||
@@ -198,2 +235,24 @@ } | ||
async function doIt() { | ||
await asyncOperation((resolve, reject) => { | ||
setTimeout(() => { | ||
reject(new MyError('MyError1')); | ||
}); | ||
}); | ||
} | ||
await doIt(); | ||
} | ||
catch (error) { | ||
fetched = true; | ||
expect(error).toBeInstanceOf(MyError); | ||
expect(error.stack).toContain('MyError1'); | ||
expect(error.stack).toContain('doIt'); | ||
} | ||
expect(fetched).toBe(true); | ||
}); | ||
test('asyncOperation catches async errors', async () => { | ||
class MyError extends Error { | ||
} | ||
let fetched = false; | ||
try { | ||
async function doIt() { | ||
await asyncOperation(async (resolve) => { | ||
@@ -269,10 +328,20 @@ await sleep(0.2); | ||
}.bind(undefined))).toBe(true); | ||
expect(isConstructable(function () { })).toBe(true); | ||
expect(isConstructable(function () { }.bind(undefined))).toBe(true); | ||
expect(isConstructable(() => { })).toBe(false); | ||
expect(isConstructable((() => { }).bind(undefined))).toBe(false); | ||
expect(isConstructable(async () => { })).toBe(false); | ||
expect(isConstructable(async function () { })).toBe(false); | ||
expect(isConstructable(function* () { })).toBe(false); | ||
expect(isConstructable({ foo() { } }.foo)).toBe(false); | ||
expect(isConstructable(function () { | ||
})).toBe(true); | ||
expect(isConstructable(function () { | ||
}.bind(undefined))).toBe(true); | ||
expect(isConstructable(() => { | ||
})).toBe(false); | ||
expect(isConstructable((() => { | ||
}).bind(undefined))).toBe(false); | ||
expect(isConstructable(async () => { | ||
})).toBe(false); | ||
expect(isConstructable(async function () { | ||
})).toBe(false); | ||
expect(isConstructable(function* () { | ||
})).toBe(false); | ||
expect(isConstructable({ | ||
foo() { | ||
} | ||
}.foo)).toBe(false); | ||
expect(isConstructable(URL)).toBe(true); | ||
@@ -307,3 +376,4 @@ }); | ||
expect(stringifyValueWithType(true)).toBe(`Boolean(true)`); | ||
expect(stringifyValueWithType(function Peter() { })).toBe(`Function Peter`); | ||
expect(stringifyValueWithType(function Peter() { | ||
})).toBe(`Function Peter`); | ||
}); | ||
@@ -310,0 +380,0 @@ test('getClassTypeFromInstance', async () => { |
{ | ||
"name": "@deepkit/core", | ||
"version": "1.0.1-alpha.39", | ||
"version": "1.0.1-alpha.42", | ||
"description": "Deepkit core library", | ||
@@ -40,3 +40,3 @@ "type": "commonjs", | ||
}, | ||
"gitHead": "21b4849669752cc18aa13094594a08f240042e69" | ||
"gitHead": "8973e01e1596c0b5dac4b61ff2325a89e82e1ec0" | ||
} |
@@ -29,2 +29,3 @@ /* | ||
public stack?: string; | ||
constructor(public message: string = '') { | ||
@@ -146,3 +147,3 @@ super(message); | ||
* Returns a human readable string representation from the given value. | ||
*/ | ||
*/ | ||
export function stringifyValueWithType(value: any): string { | ||
@@ -203,3 +204,15 @@ if ('string' === typeof value) return `String(${value})`; | ||
const AsyncFunction = (async () => { | ||
}).constructor; | ||
/** | ||
* Returns true if given obj is a async function. | ||
* | ||
* @public | ||
*/ | ||
export function isAsyncFunction(obj: any): obj is (...args: any[]) => Promise<any> { | ||
return obj instanceof AsyncFunction; | ||
} | ||
/** | ||
* Returns true if given obj is a promise like object. | ||
@@ -213,4 +226,4 @@ * | ||
export function isPromise<T>(obj: any | Promise<T>): obj is Promise<T> { | ||
return obj !== null && typeof obj === "object" && typeof obj.then === "function" | ||
&& typeof obj.catch === "function" && typeof obj.finally === "function"; | ||
return obj !== null && typeof obj === 'object' && typeof obj.then === 'function' | ||
&& typeof obj.catch === 'function' && typeof obj.finally === 'function'; | ||
} | ||
@@ -499,5 +512,4 @@ | ||
export async function asyncOperation<T>(executor: (resolve: (value: T) => void, reject: (error: any) => void) => void | Promise<void>): Promise<T> { | ||
let error: any, async: any; | ||
try { | ||
async = await new Promise<T>(async (resolve, reject) => { | ||
return await new Promise<T>(async (resolve, reject) => { | ||
try { | ||
@@ -508,11 +520,7 @@ await executor(resolve, reject); | ||
} | ||
}) | ||
} catch (e) { | ||
error = e; | ||
} | ||
if (error) { | ||
}); | ||
} catch (error) { | ||
mergeStack(error, createStack()); | ||
throw error; | ||
} | ||
return async; | ||
} | ||
@@ -536,3 +544,3 @@ | ||
export function createStack(removeCallee: boolean = true): string { | ||
if (Error.stackTraceLimit === 10) Error.stackTraceLimit = 100 | ||
if (Error.stackTraceLimit === 10) Error.stackTraceLimit = 100; | ||
let stack = new Error().stack || ''; | ||
@@ -539,0 +547,0 @@ |
@@ -11,2 +11,3 @@ import { expect, test } from '@jest/globals'; | ||
isArray, | ||
isAsyncFunction, | ||
isClass, | ||
@@ -27,3 +28,4 @@ isClassInstance, | ||
class SimpleClass { | ||
constructor(public name: string) { } | ||
constructor(public name: string) { | ||
} | ||
} | ||
@@ -33,6 +35,8 @@ | ||
class User { | ||
constructor(public readonly name: string) { } | ||
constructor(public readonly name: string) { | ||
} | ||
} | ||
class MyError extends Error { } | ||
class MyError extends Error { | ||
} | ||
@@ -52,4 +56,6 @@ expect(getClassName(new User('peter'))).toBe('User'); | ||
expect(isObject(undefined)).toBe(false); | ||
expect(isObject(() => { })).toBe(false); | ||
expect(isObject(function () { })).toBe(false); | ||
expect(isObject(() => { | ||
})).toBe(false); | ||
expect(isObject(function () { | ||
})).toBe(false); | ||
expect(isObject(1)).toBe(false); | ||
@@ -69,4 +75,6 @@ expect(isObject('1')).toBe(false); | ||
expect(isPromise(undefined)).toBe(false); | ||
expect(isPromise(() => { })).toBe(false); | ||
expect(isPromise(function () { })).toBe(false); | ||
expect(isPromise(() => { | ||
})).toBe(false); | ||
expect(isPromise(function () { | ||
})).toBe(false); | ||
expect(isPromise(1)).toBe(false); | ||
@@ -78,3 +86,4 @@ expect(isPromise('1')).toBe(false); | ||
const foo2 = () => { }; | ||
const foo2 = () => { | ||
}; | ||
expect(isPromise(foo1())).toBe(false); | ||
@@ -85,6 +94,7 @@ expect(isPromise(foo2())).toBe(false); | ||
} | ||
function foo4() { | ||
return new Promise((resolve) => { | ||
resolve(1); | ||
}) | ||
}); | ||
} | ||
@@ -95,3 +105,4 @@ | ||
expect(isObject(await foo4())).toBe(false); | ||
expect(isObject((async () => { })())).toBe(true); | ||
expect(isObject((async () => { | ||
})())).toBe(true); | ||
}); | ||
@@ -112,8 +123,35 @@ | ||
expect(isFunction(isFunction)).toBe(true); | ||
expect(isFunction(() => { })).toBe(true); | ||
expect(isFunction(async () => { })).toBe(true); | ||
expect(isFunction(function () { })).toBe(true); | ||
expect(isFunction(async function () { })).toBe(true); | ||
expect(isFunction(() => { | ||
})).toBe(true); | ||
expect(isFunction(async () => { | ||
})).toBe(true); | ||
expect(isFunction(function () { | ||
})).toBe(true); | ||
expect(isFunction(async function () { | ||
})).toBe(true); | ||
}); | ||
test('helper isAsyncFunction', () => { | ||
expect(isAsyncFunction([])).toBe(false); | ||
expect(isAsyncFunction(false)).toBe(false); | ||
expect(isAsyncFunction(true)).toBe(false); | ||
expect(isAsyncFunction(null)).toBe(false); | ||
expect(isAsyncFunction(undefined)).toBe(false); | ||
expect(isAsyncFunction(1)).toBe(false); | ||
expect(isAsyncFunction('1')).toBe(false); | ||
expect(isAsyncFunction({})).toBe(false); | ||
expect(isAsyncFunction(new Date())).toBe(false); | ||
expect(isAsyncFunction(new SimpleClass('asd'))).toBe(false); | ||
expect(isAsyncFunction(isFunction)).toBe(false); | ||
expect(isAsyncFunction(() => { | ||
})).toBe(false); | ||
expect(isAsyncFunction(async () => { | ||
})).toBe(true); | ||
expect(isAsyncFunction(function () { | ||
})).toBe(false); | ||
expect(isAsyncFunction(async function () { | ||
})).toBe(true); | ||
}); | ||
test('helper isClass', () => { | ||
@@ -131,6 +169,10 @@ expect(isClass([])).toBe(false); | ||
expect(isClass(isFunction)).toBe(false); | ||
expect(isClass(() => { })).toBe(false); | ||
expect(isClass(async () => { })).toBe(false); | ||
expect(isClass(function () { })).toBe(false); | ||
expect(isClass(async function () { })).toBe(false); | ||
expect(isClass(() => { | ||
})).toBe(false); | ||
expect(isClass(async () => { | ||
})).toBe(false); | ||
expect(isClass(function () { | ||
})).toBe(false); | ||
expect(isClass(async function () { | ||
})).toBe(false); | ||
@@ -148,4 +190,6 @@ expect(isClass(SimpleClass)).toBe(true); | ||
expect(isPlainObject('1')).toBe(false); | ||
expect(isPlainObject(() => { })).toBe(false); | ||
expect(isPlainObject(function () { })).toBe(false); | ||
expect(isPlainObject(() => { | ||
})).toBe(false); | ||
expect(isPlainObject(function () { | ||
})).toBe(false); | ||
@@ -157,2 +201,3 @@ expect(isPlainObject(new Date())).toBe(false); | ||
} | ||
expect(isPlainObject(new O)).toBe(false); | ||
@@ -201,4 +246,3 @@ | ||
expect(getPathValue({ | ||
}, 'bla', 'another')).toBe('another'); | ||
expect(getPathValue({}, 'bla', 'another')).toBe('another'); | ||
@@ -250,4 +294,5 @@ }); | ||
test('asyncOperation', async () => { | ||
class MyError extends Error { } | ||
test('asyncOperation maintain error stack trace', async () => { | ||
class MyError extends Error { | ||
} | ||
@@ -257,2 +302,26 @@ let fetched = false; | ||
async function doIt() { | ||
await asyncOperation((resolve, reject) => { | ||
setTimeout(() => { | ||
reject(new MyError('MyError1')); | ||
}); | ||
}); | ||
} | ||
await doIt(); | ||
} catch (error) { | ||
fetched = true; | ||
expect(error).toBeInstanceOf(MyError); | ||
expect(error.stack).toContain('MyError1'); | ||
expect(error.stack).toContain('doIt'); | ||
} | ||
expect(fetched).toBe(true); | ||
}); | ||
test('asyncOperation catches async errors', async () => { | ||
class MyError extends Error { | ||
} | ||
let fetched = false; | ||
try { | ||
async function doIt() { | ||
await asyncOperation(async (resolve) => { | ||
@@ -263,2 +332,3 @@ await sleep(0.2); | ||
} | ||
await doIt(); | ||
@@ -280,2 +350,3 @@ } catch (error) { | ||
await sleep(0.2); | ||
async function doIt2() { | ||
@@ -285,3 +356,3 @@ await asyncOperation(async (resolve) => { | ||
throw new Error('MyError2'); | ||
}) | ||
}); | ||
}; | ||
@@ -291,2 +362,3 @@ await doIt2(); | ||
} | ||
await doIt1(); | ||
@@ -311,10 +383,17 @@ } catch (error) { | ||
test('isPrototypeOfBase', () => { | ||
class Base { } | ||
class Base { | ||
} | ||
class Child1 extends Base { } | ||
class Child2 extends Base { } | ||
class Child1 extends Base { | ||
} | ||
class Child1_1 extends Child1 { } | ||
class Child1_1_1 extends Child1_1 { } | ||
class Child2 extends Base { | ||
} | ||
class Child1_1 extends Child1 { | ||
} | ||
class Child1_1_1 extends Child1_1 { | ||
} | ||
expect(isPrototypeOfBase(Base, Base)).toBe(true); | ||
@@ -330,13 +409,25 @@ expect(isPrototypeOfBase(Child1, Base)).toBe(true); | ||
test('isConstructable', () => { | ||
expect(isConstructable(class { })).toBe(true) | ||
expect(isConstructable(class { }.bind(undefined))).toBe(true) | ||
expect(isConstructable(function () { })).toBe(true) | ||
expect(isConstructable(function () { }.bind(undefined))).toBe(true) | ||
expect(isConstructable(() => { })).toBe(false) | ||
expect(isConstructable((() => { }).bind(undefined))).toBe(false) | ||
expect(isConstructable(async () => { })).toBe(false) | ||
expect(isConstructable(async function () { })).toBe(false) | ||
expect(isConstructable(function* () { })).toBe(false) | ||
expect(isConstructable({ foo() { } }.foo)).toBe(false) | ||
expect(isConstructable(URL)).toBe(true) | ||
expect(isConstructable(class { | ||
})).toBe(true); | ||
expect(isConstructable(class { | ||
}.bind(undefined))).toBe(true); | ||
expect(isConstructable(function () { | ||
})).toBe(true); | ||
expect(isConstructable(function () { | ||
}.bind(undefined))).toBe(true); | ||
expect(isConstructable(() => { | ||
})).toBe(false); | ||
expect(isConstructable((() => { | ||
}).bind(undefined))).toBe(false); | ||
expect(isConstructable(async () => { | ||
})).toBe(false); | ||
expect(isConstructable(async function () { | ||
})).toBe(false); | ||
expect(isConstructable(function* () { | ||
})).toBe(false); | ||
expect(isConstructable({ | ||
foo() { | ||
} | ||
}.foo)).toBe(false); | ||
expect(isConstructable(URL)).toBe(true); | ||
}); | ||
@@ -365,13 +456,19 @@ | ||
test('stringifyValueWithType', async () => { | ||
class Peter {id = 1} | ||
class Peter { | ||
id = 1; | ||
} | ||
expect(stringifyValueWithType(new Peter)).toBe(`Peter {id: Number(1)}`); | ||
expect(stringifyValueWithType({id: 1})).toBe(`Object {id: Number(1)}`); | ||
expect(stringifyValueWithType({ id: 1 })).toBe(`Object {id: Number(1)}`); | ||
expect(stringifyValueWithType('foo')).toBe(`String(foo)`); | ||
expect(stringifyValueWithType(2)).toBe(`Number(2)`); | ||
expect(stringifyValueWithType(true)).toBe(`Boolean(true)`); | ||
expect(stringifyValueWithType(function Peter() {})).toBe(`Function Peter`); | ||
expect(stringifyValueWithType(function Peter() { | ||
})).toBe(`Function Peter`); | ||
}); | ||
test('getClassTypeFromInstance', async () => { | ||
class Peter {} | ||
class Peter { | ||
} | ||
expect(getClassTypeFromInstance(new Peter)).toBe(Peter); | ||
@@ -385,3 +482,5 @@ expect(() => getClassTypeFromInstance({})).toThrow('Value is not a class instance'); | ||
test('isClassInstance', async () => { | ||
class Peter {} | ||
class Peter { | ||
} | ||
expect(isClassInstance(new Peter)).toBe(true); | ||
@@ -400,4 +499,5 @@ expect(isClassInstance({})).toBe(false); | ||
class Base {} | ||
class Base { | ||
} | ||
class Model2 extends Base { | ||
@@ -404,0 +504,0 @@ id: number = 0; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
475490
8586