diff options
Diffstat (limited to 'std/node/_util')
-rw-r--r-- | std/node/_util/_util_callbackify.ts | 125 | ||||
-rw-r--r-- | std/node/_util/_util_callbackify_test.ts | 390 | ||||
-rw-r--r-- | std/node/_util/_util_promisify.ts | 125 | ||||
-rw-r--r-- | std/node/_util/_util_promisify_test.ts | 236 | ||||
-rw-r--r-- | std/node/_util/_util_types.ts | 233 | ||||
-rw-r--r-- | std/node/_util/_util_types_test.ts | 509 |
6 files changed, 0 insertions, 1618 deletions
diff --git a/std/node/_util/_util_callbackify.ts b/std/node/_util/_util_callbackify.ts deleted file mode 100644 index e0b862b0e..000000000 --- a/std/node/_util/_util_callbackify.ts +++ /dev/null @@ -1,125 +0,0 @@ -// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -// -// Adapted from Node.js. Copyright Joyent, Inc. and other Node contributors. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to permit -// persons to whom the Software is furnished to do so, subject to the -// following conditions: -// -// The above copyright notice and this permission notice shall be included -// in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN -// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR -// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE -// USE OR OTHER DEALINGS IN THE SOFTWARE. - -// These are simplified versions of the "real" errors in Node. -class NodeFalsyValueRejectionError extends Error { - public reason: unknown; - public code = "ERR_FALSY_VALUE_REJECTION"; - constructor(reason: unknown) { - super("Promise was rejected with falsy value"); - this.reason = reason; - } -} -class NodeInvalidArgTypeError extends TypeError { - public code = "ERR_INVALID_ARG_TYPE"; - constructor(argumentName: string) { - super(`The ${argumentName} argument must be of type function.`); - } -} - -type Callback<ResultT> = - | ((err: Error) => void) - | ((err: null, result: ResultT) => void); - -function callbackify<ResultT>( - fn: () => PromiseLike<ResultT>, -): (callback: Callback<ResultT>) => void; -function callbackify<ArgT, ResultT>( - fn: (arg: ArgT) => PromiseLike<ResultT>, -): (arg: ArgT, callback: Callback<ResultT>) => void; -function callbackify<Arg1T, Arg2T, ResultT>( - fn: (arg1: Arg1T, arg2: Arg2T) => PromiseLike<ResultT>, -): (arg1: Arg1T, arg2: Arg2T, callback: Callback<ResultT>) => void; -function callbackify<Arg1T, Arg2T, Arg3T, ResultT>( - fn: (arg1: Arg1T, arg2: Arg2T, arg3: Arg3T) => PromiseLike<ResultT>, -): (arg1: Arg1T, arg2: Arg2T, arg3: Arg3T, callback: Callback<ResultT>) => void; -function callbackify<Arg1T, Arg2T, Arg3T, Arg4T, ResultT>( - fn: ( - arg1: Arg1T, - arg2: Arg2T, - arg3: Arg3T, - arg4: Arg4T, - ) => PromiseLike<ResultT>, -): ( - arg1: Arg1T, - arg2: Arg2T, - arg3: Arg3T, - arg4: Arg4T, - callback: Callback<ResultT>, -) => void; -function callbackify<Arg1T, Arg2T, Arg3T, Arg4T, Arg5T, ResultT>( - fn: ( - arg1: Arg1T, - arg2: Arg2T, - arg3: Arg3T, - arg4: Arg4T, - arg5: Arg5T, - ) => PromiseLike<ResultT>, -): ( - arg1: Arg1T, - arg2: Arg2T, - arg3: Arg3T, - arg4: Arg4T, - arg5: Arg5T, - callback: Callback<ResultT>, -) => void; - -// deno-lint-ignore no-explicit-any -function callbackify(original: any): any { - if (typeof original !== "function") { - throw new NodeInvalidArgTypeError('"original"'); - } - - const callbackified = function (this: unknown, ...args: unknown[]): void { - const maybeCb = args.pop(); - if (typeof maybeCb !== "function") { - throw new NodeInvalidArgTypeError("last"); - } - const cb = (...args: unknown[]): void => { - maybeCb.apply(this, args); - }; - original.apply(this, args).then( - (ret: unknown) => { - queueMicrotask(cb.bind(this, null, ret)); - }, - (rej: unknown) => { - rej = rej || new NodeFalsyValueRejectionError(rej); - queueMicrotask(cb.bind(this, rej)); - }, - ); - }; - - const descriptors = Object.getOwnPropertyDescriptors(original); - // It is possible to manipulate a functions `length` or `name` property. This - // guards against the manipulation. - if (typeof descriptors.length.value === "number") { - descriptors.length.value++; - } - if (typeof descriptors.name.value === "string") { - descriptors.name.value += "Callbackified"; - } - Object.defineProperties(callbackified, descriptors); - return callbackified; -} - -export { callbackify }; diff --git a/std/node/_util/_util_callbackify_test.ts b/std/node/_util/_util_callbackify_test.ts deleted file mode 100644 index 9e5281409..000000000 --- a/std/node/_util/_util_callbackify_test.ts +++ /dev/null @@ -1,390 +0,0 @@ -// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -// -// Adapted from Node.js. Copyright Joyent, Inc. and other Node contributors. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to permit -// persons to whom the Software is furnished to do so, subject to the -// following conditions: -// -// The above copyright notice and this permission notice shall be included -// in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN -// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR -// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE -// USE OR OTHER DEALINGS IN THE SOFTWARE. -import { assert, assertStrictEquals } from "../../testing/asserts.ts"; -import { callbackify } from "./_util_callbackify.ts"; - -const values = [ - "hello world", - null, - undefined, - false, - 0, - {}, - { key: "value" }, - Symbol("I am a symbol"), - function ok(): void {}, - ["array", "with", 4, "values"], - new Error("boo"), -]; - -class TestQueue { - #waitingPromise: Promise<void>; - #resolve?: () => void; - #reject?: (err: unknown) => void; - #queueSize = 0; - - constructor() { - this.#waitingPromise = new Promise((resolve, reject) => { - this.#resolve = resolve; - this.#reject = reject; - }); - } - - enqueue(fn: (done: () => void) => void): void { - this.#queueSize++; - try { - fn(() => { - this.#queueSize--; - if (this.#queueSize === 0) { - assert( - this.#resolve, - "Test setup error; async queue is missing #resolve", - ); - this.#resolve(); - } - }); - } catch (err) { - assert(this.#reject, "Test setup error; async queue is missing #reject"); - this.#reject(err); - } - } - - waitForCompletion(): Promise<void> { - return this.#waitingPromise; - } -} - -Deno.test( - "callbackify passes the resolution value as the second argument to the callback", - async () => { - const testQueue = new TestQueue(); - - for (const value of values) { - // deno-lint-ignore require-await - const asyncFn = async (): Promise<typeof value> => { - return value; - }; - const cbAsyncFn = callbackify(asyncFn); - testQueue.enqueue((done) => { - cbAsyncFn((err: unknown, ret: unknown) => { - assertStrictEquals(err, null); - assertStrictEquals(ret, value); - done(); - }); - }); - - const promiseFn = (): Promise<typeof value> => { - return Promise.resolve(value); - }; - const cbPromiseFn = callbackify(promiseFn); - testQueue.enqueue((done) => { - cbPromiseFn((err: unknown, ret: unknown) => { - assertStrictEquals(err, null); - assertStrictEquals(ret, value); - done(); - }); - }); - - // deno-lint-ignore no-explicit-any - const thenableFn = (): PromiseLike<any> => { - return { - // deno-lint-ignore no-explicit-any - then(onfulfilled): PromiseLike<any> { - assert(onfulfilled); - onfulfilled(value); - return this; - }, - }; - }; - const cbThenableFn = callbackify(thenableFn); - testQueue.enqueue((done) => { - cbThenableFn((err: unknown, ret: unknown) => { - assertStrictEquals(err, null); - assertStrictEquals(ret, value); - done(); - }); - }); - } - - await testQueue.waitForCompletion(); - }, -); - -Deno.test( - "callbackify passes the rejection value as the first argument to the callback", - async () => { - const testQueue = new TestQueue(); - - for (const value of values) { - // deno-lint-ignore require-await - const asyncFn = async (): Promise<never> => { - return Promise.reject(value); - }; - const cbAsyncFn = callbackify(asyncFn); - assertStrictEquals(cbAsyncFn.length, 1); - assertStrictEquals(cbAsyncFn.name, "asyncFnCallbackified"); - testQueue.enqueue((done) => { - cbAsyncFn((err: unknown, ret: unknown) => { - assertStrictEquals(ret, undefined); - if (err instanceof Error) { - if ("reason" in err) { - assert(!value); - assertStrictEquals( - // deno-lint-ignore no-explicit-any - (err as any).code, - "ERR_FALSY_VALUE_REJECTION", - ); - // deno-lint-ignore no-explicit-any - assertStrictEquals((err as any).reason, value); - } else { - assertStrictEquals(String(value).endsWith(err.message), true); - } - } else { - assertStrictEquals(err, value); - } - done(); - }); - }); - - const promiseFn = (): Promise<never> => { - return Promise.reject(value); - }; - const obj = {}; - Object.defineProperty(promiseFn, "name", { - value: obj, - writable: false, - enumerable: false, - configurable: true, - }); - - const cbPromiseFn = callbackify(promiseFn); - assertStrictEquals(promiseFn.name, obj); - testQueue.enqueue((done) => { - cbPromiseFn((err: unknown, ret: unknown) => { - assertStrictEquals(ret, undefined); - if (err instanceof Error) { - if ("reason" in err) { - assert(!value); - assertStrictEquals( - // deno-lint-ignore no-explicit-any - (err as any).code, - "ERR_FALSY_VALUE_REJECTION", - ); - // deno-lint-ignore no-explicit-any - assertStrictEquals((err as any).reason, value); - } else { - assertStrictEquals(String(value).endsWith(err.message), true); - } - } else { - assertStrictEquals(err, value); - } - done(); - }); - }); - - const thenableFn = (): PromiseLike<never> => { - return { - then(onfulfilled, onrejected): PromiseLike<never> { - assert(onrejected); - onrejected(value); - return this; - }, - }; - }; - - const cbThenableFn = callbackify(thenableFn); - testQueue.enqueue((done) => { - cbThenableFn((err: unknown, ret: unknown) => { - assertStrictEquals(ret, undefined); - if (err instanceof Error) { - if ("reason" in err) { - assert(!value); - assertStrictEquals( - // deno-lint-ignore no-explicit-any - (err as any).code, - "ERR_FALSY_VALUE_REJECTION", - ); - // deno-lint-ignore no-explicit-any - assertStrictEquals((err as any).reason, value); - } else { - assertStrictEquals(String(value).endsWith(err.message), true); - } - } else { - assertStrictEquals(err, value); - } - done(); - }); - }); - } - - await testQueue.waitForCompletion(); - }, -); - -Deno.test("callbackify passes arguments to the original", async () => { - const testQueue = new TestQueue(); - - for (const value of values) { - // deno-lint-ignore require-await - const asyncFn = async (arg: typeof value): Promise<typeof value> => { - assertStrictEquals(arg, value); - return arg; - }; - - const cbAsyncFn = callbackify(asyncFn); - assertStrictEquals(cbAsyncFn.length, 2); - assert(Object.getPrototypeOf(cbAsyncFn) !== Object.getPrototypeOf(asyncFn)); - assertStrictEquals(Object.getPrototypeOf(cbAsyncFn), Function.prototype); - testQueue.enqueue((done) => { - cbAsyncFn(value, (err: unknown, ret: unknown) => { - assertStrictEquals(err, null); - assertStrictEquals(ret, value); - done(); - }); - }); - - const promiseFn = <T>(arg: typeof value): Promise<typeof value> => { - assertStrictEquals(arg, value); - return Promise.resolve(arg); - }; - const obj = {}; - Object.defineProperty(promiseFn, "length", { - value: obj, - writable: false, - enumerable: false, - configurable: true, - }); - - const cbPromiseFn = callbackify(promiseFn); - assertStrictEquals(promiseFn.length, obj); - testQueue.enqueue((done) => { - cbPromiseFn(value, (err: unknown, ret: unknown) => { - assertStrictEquals(err, null); - assertStrictEquals(ret, value); - done(); - }); - }); - } - - await testQueue.waitForCompletion(); -}); - -Deno.test("callbackify preserves the `this` binding", async () => { - const testQueue = new TestQueue(); - - for (const value of values) { - const objectWithSyncFunction = { - fn(this: unknown, arg: typeof value): Promise<typeof value> { - assertStrictEquals(this, objectWithSyncFunction); - return Promise.resolve(arg); - }, - }; - const cbSyncFunction = callbackify(objectWithSyncFunction.fn); - testQueue.enqueue((done) => { - cbSyncFunction.call(objectWithSyncFunction, value, function ( - this: unknown, - err: unknown, - ret: unknown, - ) { - assertStrictEquals(err, null); - assertStrictEquals(ret, value); - assertStrictEquals(this, objectWithSyncFunction); - done(); - }); - }); - - const objectWithAsyncFunction = { - // deno-lint-ignore require-await - async fn(this: unknown, arg: typeof value): Promise<typeof value> { - assertStrictEquals(this, objectWithAsyncFunction); - return arg; - }, - }; - const cbAsyncFunction = callbackify(objectWithAsyncFunction.fn); - testQueue.enqueue((done) => { - cbAsyncFunction.call(objectWithAsyncFunction, value, function ( - this: unknown, - err: unknown, - ret: unknown, - ) { - assertStrictEquals(err, null); - assertStrictEquals(ret, value); - assertStrictEquals(this, objectWithAsyncFunction); - done(); - }); - }); - } - - await testQueue.waitForCompletion(); -}); - -Deno.test("callbackify throws with non-function inputs", () => { - ["foo", null, undefined, false, 0, {}, Symbol(), []].forEach((value) => { - try { - // deno-lint-ignore no-explicit-any - callbackify(value as any); - throw Error("We should never reach this error"); - } catch (err) { - assert(err instanceof TypeError); - // deno-lint-ignore no-explicit-any - assertStrictEquals((err as any).code, "ERR_INVALID_ARG_TYPE"); - assertStrictEquals(err.name, "TypeError"); - assertStrictEquals( - err.message, - 'The "original" argument must be of type function.', - ); - } - }); -}); - -Deno.test( - "callbackify returns a function that throws if the last argument is not a function", - () => { - // deno-lint-ignore require-await - async function asyncFn(): Promise<number> { - return 42; - } - - // deno-lint-ignore no-explicit-any - const cb = callbackify(asyncFn) as any; - const args: unknown[] = []; - - ["foo", null, undefined, false, 0, {}, Symbol(), []].forEach((value) => { - args.push(value); - - try { - cb(...args); - throw Error("We should never reach this error"); - } catch (err) { - assert(err instanceof TypeError); - // deno-lint-ignore no-explicit-any - assertStrictEquals((err as any).code, "ERR_INVALID_ARG_TYPE"); - assertStrictEquals(err.name, "TypeError"); - assertStrictEquals( - err.message, - "The last argument must be of type function.", - ); - } - }); - }, -); diff --git a/std/node/_util/_util_promisify.ts b/std/node/_util/_util_promisify.ts deleted file mode 100644 index 6692677ec..000000000 --- a/std/node/_util/_util_promisify.ts +++ /dev/null @@ -1,125 +0,0 @@ -// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -// -// Adapted from Node.js. Copyright Joyent, Inc. and other Node contributors. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to permit -// persons to whom the Software is furnished to do so, subject to the -// following conditions: -// -// The above copyright notice and this permission notice shall be included -// in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN -// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR -// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE -// USE OR OTHER DEALINGS IN THE SOFTWARE. - -// Hack: work around the following TypeScript error: -// error: TS2345 [ERROR]: Argument of type 'typeof kCustomPromisifiedSymbol' -// is not assignable to parameter of type 'typeof kCustomPromisifiedSymbol'. -// assertStrictEquals(kCustomPromisifiedSymbol, promisify.custom); -// ~~~~~~~~~~~~~~~~ -declare const _CustomPromisifiedSymbol: unique symbol; -declare const _CustomPromisifyArgsSymbol: unique symbol; -declare let Symbol: SymbolConstructor; -interface SymbolConstructor { - for(key: "nodejs.util.promisify.custom"): typeof _CustomPromisifiedSymbol; - for( - key: "nodejs.util.promisify.customArgs", - ): typeof _CustomPromisifyArgsSymbol; -} -// End hack. - -// In addition to being accessible through util.promisify.custom, -// this symbol is registered globally and can be accessed in any environment as -// Symbol.for('nodejs.util.promisify.custom'). -const kCustomPromisifiedSymbol = Symbol.for("nodejs.util.promisify.custom"); -// This is an internal Node symbol used by functions returning multiple -// arguments, e.g. ['bytesRead', 'buffer'] for fs.read(). -const kCustomPromisifyArgsSymbol = Symbol.for( - "nodejs.util.promisify.customArgs", -); - -class NodeInvalidArgTypeError extends TypeError { - public code = "ERR_INVALID_ARG_TYPE"; - constructor(argumentName: string, type: string, received: unknown) { - super( - `The "${argumentName}" argument must be of type ${type}. Received ${typeof received}`, - ); - } -} - -export function promisify( - // deno-lint-ignore no-explicit-any - original: (...args: any[]) => void, - // deno-lint-ignore no-explicit-any -): (...args: any[]) => Promise<any> { - if (typeof original !== "function") { - throw new NodeInvalidArgTypeError("original", "Function", original); - } - // deno-lint-ignore no-explicit-any - if ((original as any)[kCustomPromisifiedSymbol]) { - // deno-lint-ignore no-explicit-any - const fn = (original as any)[kCustomPromisifiedSymbol]; - if (typeof fn !== "function") { - throw new NodeInvalidArgTypeError( - "util.promisify.custom", - "Function", - fn, - ); - } - return Object.defineProperty(fn, kCustomPromisifiedSymbol, { - value: fn, - enumerable: false, - writable: false, - configurable: true, - }); - } - - // Names to create an object from in case the callback receives multiple - // arguments, e.g. ['bytesRead', 'buffer'] for fs.read. - // deno-lint-ignore no-explicit-any - const argumentNames = (original as any)[kCustomPromisifyArgsSymbol]; - // deno-lint-ignore no-explicit-any - function fn(this: any, ...args: unknown[]): Promise<unknown> { - return new Promise((resolve, reject) => { - original.call(this, ...args, (err: Error, ...values: unknown[]) => { - if (err) { - return reject(err); - } - if (argumentNames !== undefined && values.length > 1) { - const obj = {}; - for (let i = 0; i < argumentNames.length; i++) { - // deno-lint-ignore no-explicit-any - (obj as any)[argumentNames[i]] = values[i]; - } - resolve(obj); - } else { - resolve(values[0]); - } - }); - }); - } - - Object.setPrototypeOf(fn, Object.getPrototypeOf(original)); - - Object.defineProperty(fn, kCustomPromisifiedSymbol, { - value: fn, - enumerable: false, - writable: false, - configurable: true, - }); - return Object.defineProperties( - fn, - Object.getOwnPropertyDescriptors(original), - ); -} - -promisify.custom = kCustomPromisifiedSymbol; diff --git a/std/node/_util/_util_promisify_test.ts b/std/node/_util/_util_promisify_test.ts deleted file mode 100644 index 39f7de75c..000000000 --- a/std/node/_util/_util_promisify_test.ts +++ /dev/null @@ -1,236 +0,0 @@ -// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -// -// Adapted from Node.js. Copyright Joyent, Inc. and other Node contributors. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to permit -// persons to whom the Software is furnished to do so, subject to the -// following conditions: -// -// The above copyright notice and this permission notice shall be included -// in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN -// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR -// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE -// USE OR OTHER DEALINGS IN THE SOFTWARE. -import { - assert, - assertEquals, - assertStrictEquals, - assertThrowsAsync, -} from "../../testing/asserts.ts"; -import { promisify } from "./_util_promisify.ts"; -import * as fs from "../fs.ts"; - -// deno-lint-ignore no-explicit-any -type VoidFunction = (...args: any[]) => void; - -const readFile = promisify(fs.readFile); -const customPromisifyArgs = Symbol.for("nodejs.util.promisify.customArgs"); - -Deno.test( - "Errors should reject the promise", - async function testPromiseRejection() { - await assertThrowsAsync(() => readFile("/dontexist"), Deno.errors.NotFound); - }, -); - -Deno.test("Promisify.custom", async function testPromisifyCustom() { - function fn(): void {} - - function promisifedFn(): void {} - // @ts-expect-error TypeScript (as of 3.7) does not support indexing namespaces by symbol - fn[promisify.custom] = promisifedFn; - - const promisifiedFnA = promisify(fn); - const promisifiedFnB = promisify(promisifiedFnA); - assertStrictEquals(promisifiedFnA, promisifedFn); - assertStrictEquals(promisifiedFnB, promisifedFn); - - await promisifiedFnA; - await promisifiedFnB; -}); - -Deno.test("promiisfy.custom symbol", function testPromisifyCustomSymbol() { - function fn(): void {} - - function promisifiedFn(): void {} - - // util.promisify.custom is a shared symbol which can be accessed - // as `Symbol.for("nodejs.util.promisify.custom")`. - const kCustomPromisifiedSymbol = Symbol.for("nodejs.util.promisify.custom"); - // @ts-expect-error TypeScript (as of 3.7) does not support indexing namespaces by symbol - fn[kCustomPromisifiedSymbol] = promisifiedFn; - - assertStrictEquals(kCustomPromisifiedSymbol, promisify.custom); - assertStrictEquals(promisify(fn), promisifiedFn); - assertStrictEquals(promisify(promisify(fn)), promisifiedFn); -}); - -Deno.test("Invalid argument should throw", function testThrowInvalidArgument() { - function fn(): void {} - // @ts-expect-error TypeScript (as of 3.7) does not support indexing namespaces by symbol - fn[promisify.custom] = 42; - try { - promisify(fn); - } catch (e) { - assertStrictEquals(e.code, "ERR_INVALID_ARG_TYPE"); - assert(e instanceof TypeError); - } -}); - -Deno.test("Custom promisify args", async function testPromisifyCustomArgs() { - const firstValue = 5; - const secondValue = 17; - - function fn(callback: VoidFunction): void { - callback(null, firstValue, secondValue); - } - - // @ts-expect-error TypeScript (as of 3.7) does not support indexing namespaces by symbol - fn[customPromisifyArgs] = ["first", "second"]; - - const obj = await promisify(fn)(); - assertEquals(obj, { first: firstValue, second: secondValue }); -}); - -Deno.test( - "Multiple callback args without custom promisify args", - async function testPromisifyWithoutCustomArgs() { - function fn(callback: VoidFunction): void { - callback(null, "foo", "bar"); - } - const value = await promisify(fn)(); - assertStrictEquals(value, "foo"); - }, -); - -Deno.test( - "Undefined resolved value", - async function testPromisifyWithUndefinedResolvedValue() { - function fn(callback: VoidFunction): void { - callback(null); - } - const value = await promisify(fn)(); - assertStrictEquals(value, undefined); - }, -); - -Deno.test( - "Undefined resolved value II", - async function testPromisifyWithUndefinedResolvedValueII() { - function fn(callback: VoidFunction): void { - callback(); - } - const value = await promisify(fn)(); - assertStrictEquals(value, undefined); - }, -); - -Deno.test( - "Resolved value: number", - async function testPromisifyWithNumberResolvedValue() { - function fn(err: Error | null, val: number, callback: VoidFunction): void { - callback(err, val); - } - const value = await promisify(fn)(null, 42); - assertStrictEquals(value, 42); - }, -); - -Deno.test( - "Rejected value", - async function testPromisifyWithNumberRejectedValue() { - function fn(err: Error | null, val: null, callback: VoidFunction): void { - callback(err, val); - } - await assertThrowsAsync( - () => promisify(fn)(new Error("oops"), null), - Error, - "oops", - ); - }, -); - -Deno.test("Rejected value", async function testPromisifyWithAsObjectMethod() { - const o: { fn?: VoidFunction } = {}; - const fn = promisify(function (this: unknown, cb: VoidFunction): void { - cb(null, this === o); - }); - - o.fn = fn; - - const val = await o.fn(); - assert(val); -}); - -Deno.test( - "Multiple callback", - async function testPromisifyWithMultipleCallback() { - const err = new Error( - "Should not have called the callback with the error.", - ); - const stack = err.stack; - - const fn = promisify(function (cb: VoidFunction): void { - cb(null); - cb(err); - }); - - await fn(); - await Promise.resolve(); - return assertStrictEquals(stack, err.stack); - }, -); - -Deno.test("Promisify a promise", function testPromisifyPromise() { - function c(): void {} - const a = promisify(function (): void {}); - const b = promisify(a); - assert(c !== a); - assertStrictEquals(a, b); -}); - -Deno.test("Test error", async function testInvalidArguments() { - let errToThrow; - - const thrower = promisify(function ( - a: number, - b: number, - c: number, - cb: VoidFunction, - ): void { - errToThrow = new Error(`${a}-${b}-${c}-${cb}`); - throw errToThrow; - }); - - try { - await thrower(1, 2, 3); - throw new Error(`should've failed`); - } catch (e) { - assertStrictEquals(e, errToThrow); - } -}); - -Deno.test("Test invalid arguments", function testInvalidArguments() { - [undefined, null, true, 0, "str", {}, [], Symbol()].forEach((input) => { - try { - // @ts-expect-error TypeScript - promisify(input); - } catch (e) { - assertStrictEquals(e.code, "ERR_INVALID_ARG_TYPE"); - assert(e instanceof TypeError); - assertEquals( - e.message, - `The "original" argument must be of type Function. Received ${typeof input}`, - ); - } - }); -}); diff --git a/std/node/_util/_util_types.ts b/std/node/_util/_util_types.ts deleted file mode 100644 index f64f5377b..000000000 --- a/std/node/_util/_util_types.ts +++ /dev/null @@ -1,233 +0,0 @@ -// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -// -// Adapted from Node.js. Copyright Joyent, Inc. and other Node contributors. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to permit -// persons to whom the Software is furnished to do so, subject to the -// following conditions: -// -// The above copyright notice and this permission notice shall be included -// in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN -// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR -// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE -// USE OR OTHER DEALINGS IN THE SOFTWARE. - -const _toString = Object.prototype.toString; - -const _isObjectLike = (value: unknown): boolean => - value !== null && typeof value === "object"; - -const _isFunctionLike = (value: unknown): boolean => - value !== null && typeof value === "function"; - -export function isAnyArrayBuffer(value: unknown): boolean { - return ( - _isObjectLike(value) && - (_toString.call(value) === "[object ArrayBuffer]" || - _toString.call(value) === "[object SharedArrayBuffer]") - ); -} - -export function isArrayBufferView(value: unknown): boolean { - return ArrayBuffer.isView(value); -} - -export function isArgumentsObject(value: unknown): boolean { - return _isObjectLike(value) && _toString.call(value) === "[object Arguments]"; -} - -export function isArrayBuffer(value: unknown): boolean { - return ( - _isObjectLike(value) && _toString.call(value) === "[object ArrayBuffer]" - ); -} - -export function isAsyncFunction(value: unknown): boolean { - return ( - _isFunctionLike(value) && _toString.call(value) === "[object AsyncFunction]" - ); -} - -export function isBigInt64Array(value: unknown): boolean { - return ( - _isObjectLike(value) && _toString.call(value) === "[object BigInt64Array]" - ); -} - -export function isBigUint64Array(value: unknown): boolean { - return ( - _isObjectLike(value) && _toString.call(value) === "[object BigUint64Array]" - ); -} - -export function isBooleanObject(value: unknown): boolean { - return _isObjectLike(value) && _toString.call(value) === "[object Boolean]"; -} - -export function isBoxedPrimitive(value: unknown): boolean { - return ( - isBooleanObject(value) || - isStringObject(value) || - isNumberObject(value) || - isSymbolObject(value) || - isBigIntObject(value) - ); -} - -export function isDataView(value: unknown): boolean { - return _isObjectLike(value) && _toString.call(value) === "[object DataView]"; -} - -export function isDate(value: unknown): boolean { - return _isObjectLike(value) && _toString.call(value) === "[object Date]"; -} - -// isExternal: Not implemented - -export function isFloat32Array(value: unknown): boolean { - return ( - _isObjectLike(value) && _toString.call(value) === "[object Float32Array]" - ); -} - -export function isFloat64Array(value: unknown): boolean { - return ( - _isObjectLike(value) && _toString.call(value) === "[object Float64Array]" - ); -} - -export function isGeneratorFunction(value: unknown): boolean { - return ( - _isFunctionLike(value) && - _toString.call(value) === "[object GeneratorFunction]" - ); -} - -export function isGeneratorObject(value: unknown): boolean { - return _isObjectLike(value) && _toString.call(value) === "[object Generator]"; -} - -export function isInt8Array(value: unknown): boolean { - return _isObjectLike(value) && _toString.call(value) === "[object Int8Array]"; -} - -export function isInt16Array(value: unknown): boolean { - return ( - _isObjectLike(value) && _toString.call(value) === "[object Int16Array]" - ); -} - -export function isInt32Array(value: unknown): boolean { - return ( - _isObjectLike(value) && _toString.call(value) === "[object Int32Array]" - ); -} - -export function isMap(value: unknown): boolean { - return _isObjectLike(value) && _toString.call(value) === "[object Map]"; -} - -export function isMapIterator(value: unknown): boolean { - return ( - _isObjectLike(value) && _toString.call(value) === "[object Map Iterator]" - ); -} - -export function isModuleNamespaceObject(value: unknown): boolean { - return _isObjectLike(value) && _toString.call(value) === "[object Module]"; -} - -export function isNativeError(value: unknown): boolean { - return _isObjectLike(value) && _toString.call(value) === "[object Error]"; -} - -export function isNumberObject(value: unknown): boolean { - return _isObjectLike(value) && _toString.call(value) === "[object Number]"; -} - -export function isBigIntObject(value: unknown): boolean { - return _isObjectLike(value) && _toString.call(value) === "[object BigInt]"; -} - -export function isPromise(value: unknown): boolean { - return _isObjectLike(value) && _toString.call(value) === "[object Promise]"; -} - -export function isRegExp(value: unknown): boolean { - return _isObjectLike(value) && _toString.call(value) === "[object RegExp]"; -} - -export function isSet(value: unknown): boolean { - return _isObjectLike(value) && _toString.call(value) === "[object Set]"; -} - -export function isSetIterator(value: unknown): boolean { - return ( - _isObjectLike(value) && _toString.call(value) === "[object Set Iterator]" - ); -} - -export function isSharedArrayBuffer(value: unknown): boolean { - return ( - _isObjectLike(value) && - _toString.call(value) === "[object SharedArrayBuffer]" - ); -} - -export function isStringObject(value: unknown): boolean { - return _isObjectLike(value) && _toString.call(value) === "[object String]"; -} - -export function isSymbolObject(value: unknown): boolean { - return _isObjectLike(value) && _toString.call(value) === "[object Symbol]"; -} - -// Adapted from Lodash -export function isTypedArray(value: unknown): boolean { - /** Used to match `toStringTag` values of typed arrays. */ - const reTypedTag = - /^\[object (?:Float(?:32|64)|(?:Int|Uint)(?:8|16|32)|Uint8Clamped)Array\]$/; - return _isObjectLike(value) && reTypedTag.test(_toString.call(value)); -} - -export function isUint8Array(value: unknown): boolean { - return ( - _isObjectLike(value) && _toString.call(value) === "[object Uint8Array]" - ); -} - -export function isUint8ClampedArray(value: unknown): boolean { - return ( - _isObjectLike(value) && - _toString.call(value) === "[object Uint8ClampedArray]" - ); -} - -export function isUint16Array(value: unknown): boolean { - return ( - _isObjectLike(value) && _toString.call(value) === "[object Uint16Array]" - ); -} - -export function isUint32Array(value: unknown): boolean { - return ( - _isObjectLike(value) && _toString.call(value) === "[object Uint32Array]" - ); -} - -export function isWeakMap(value: unknown): boolean { - return _isObjectLike(value) && _toString.call(value) === "[object WeakMap]"; -} - -export function isWeakSet(value: unknown): boolean { - return _isObjectLike(value) && _toString.call(value) === "[object WeakSet]"; -} diff --git a/std/node/_util/_util_types_test.ts b/std/node/_util/_util_types_test.ts deleted file mode 100644 index 763969964..000000000 --- a/std/node/_util/_util_types_test.ts +++ /dev/null @@ -1,509 +0,0 @@ -// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -// -// Adapted from Node.js. Copyright Joyent, Inc. and other Node contributors. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to permit -// persons to whom the Software is furnished to do so, subject to the -// following conditions: -// -// The above copyright notice and this permission notice shall be included -// in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN -// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR -// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE -// USE OR OTHER DEALINGS IN THE SOFTWARE. -import { assertStrictEquals } from "../../testing/asserts.ts"; -import { - isAnyArrayBuffer, - isArgumentsObject, - isArrayBuffer, - isArrayBufferView, - isAsyncFunction, - isBigInt64Array, - isBigIntObject, - isBigUint64Array, - isBooleanObject, - isBoxedPrimitive, - isDataView, - isDate, - isFloat32Array, - isFloat64Array, - isGeneratorFunction, - isGeneratorObject, - isInt16Array, - isInt32Array, - isInt8Array, - isMap, - isMapIterator, - isModuleNamespaceObject, - isNativeError, - isNumberObject, - isPromise, - isRegExp, - isSet, - isSetIterator, - isSharedArrayBuffer, - isStringObject, - isSymbolObject, - isTypedArray, - isUint16Array, - isUint32Array, - isUint8Array, - isUint8ClampedArray, - isWeakMap, - isWeakSet, -} from "./_util_types.ts"; - -// Used to test isModuleNamespaceObject -import * as testModuleNamespaceOpbject from "./_util_types.ts"; - -// isAnyArrayBuffer -Deno.test("Should return true for valid ArrayBuffer types", () => { - assertStrictEquals(isAnyArrayBuffer(new ArrayBuffer(0)), true); - assertStrictEquals(isAnyArrayBuffer(new SharedArrayBuffer(0)), true); -}); - -Deno.test("Should return false for invalid ArrayBuffer types", () => { - assertStrictEquals(isAnyArrayBuffer({}), false); - assertStrictEquals(isAnyArrayBuffer([]), false); - assertStrictEquals(isAnyArrayBuffer(new Error()), false); -}); - -// isArrayBufferView -Deno.test("Should return true for valid ArrayBufferView types", () => { - assertStrictEquals(isArrayBufferView(new Int8Array(0)), true); - assertStrictEquals(isArrayBufferView(new Uint8Array(0)), true); - assertStrictEquals(isArrayBufferView(new Uint8ClampedArray(0)), true); - assertStrictEquals(isArrayBufferView(new Int16Array(0)), true); - assertStrictEquals(isArrayBufferView(new Uint16Array(0)), true); - assertStrictEquals(isArrayBufferView(new Int32Array(0)), true); - assertStrictEquals(isArrayBufferView(new Uint32Array(0)), true); - assertStrictEquals(isArrayBufferView(new Float32Array(0)), true); - assertStrictEquals(isArrayBufferView(new Float64Array(0)), true); - assertStrictEquals(isArrayBufferView(new DataView(new ArrayBuffer(0))), true); -}); - -Deno.test("Should return false for invalid ArrayBufferView types", () => { - assertStrictEquals(isArrayBufferView({}), false); - assertStrictEquals(isArrayBufferView([]), false); - assertStrictEquals(isArrayBufferView(new Error()), false); - assertStrictEquals(isArrayBufferView(new ArrayBuffer(0)), false); -}); - -// isArgumentsObject -// Note: not testable in TS - -Deno.test("Should return false for invalid Argument types", () => { - assertStrictEquals(isArgumentsObject({}), false); - assertStrictEquals(isArgumentsObject([]), false); - assertStrictEquals(isArgumentsObject(new Error()), false); -}); - -// isArrayBuffer -Deno.test("Should return true for valid ArrayBuffer types", () => { - assertStrictEquals(isArrayBuffer(new ArrayBuffer(0)), true); -}); - -Deno.test("Should return false for invalid ArrayBuffer types", () => { - assertStrictEquals(isArrayBuffer(new SharedArrayBuffer(0)), false); - assertStrictEquals(isArrayBuffer({}), false); - assertStrictEquals(isArrayBuffer([]), false); - assertStrictEquals(isArrayBuffer(new Error()), false); -}); - -// isAsyncFunction -Deno.test("Should return true for valid async function types", () => { - const asyncFunction = async (): Promise<void> => {}; - assertStrictEquals(isAsyncFunction(asyncFunction), true); -}); - -Deno.test("Should return false for invalid async function types", () => { - const syncFunction = (): void => {}; - assertStrictEquals(isAsyncFunction(syncFunction), false); - assertStrictEquals(isAsyncFunction({}), false); - assertStrictEquals(isAsyncFunction([]), false); - assertStrictEquals(isAsyncFunction(new Error()), false); -}); - -// isBigInt64Array -Deno.test("Should return true for valid BigInt64Array types", () => { - assertStrictEquals(isBigInt64Array(new BigInt64Array()), true); -}); - -Deno.test("Should return false for invalid BigInt64Array types", () => { - assertStrictEquals(isBigInt64Array(new BigUint64Array()), false); - assertStrictEquals(isBigInt64Array(new Float32Array()), false); - assertStrictEquals(isBigInt64Array(new Int32Array()), false); -}); - -// isBigUint64Array -Deno.test("Should return true for valid isBigUint64Array types", () => { - assertStrictEquals(isBigUint64Array(new BigUint64Array()), true); -}); - -Deno.test("Should return false for invalid isBigUint64Array types", () => { - assertStrictEquals(isBigUint64Array(new BigInt64Array()), false); - assertStrictEquals(isBigUint64Array(new Float32Array()), false); - assertStrictEquals(isBigUint64Array(new Int32Array()), false); -}); - -// isBooleanObject -Deno.test("Should return true for valid Boolean object types", () => { - assertStrictEquals(isBooleanObject(new Boolean(false)), true); - assertStrictEquals(isBooleanObject(new Boolean(true)), true); -}); - -Deno.test("Should return false for invalid isBigUint64Array types", () => { - assertStrictEquals(isBooleanObject(false), false); - assertStrictEquals(isBooleanObject(true), false); - assertStrictEquals(isBooleanObject(Boolean(false)), false); - assertStrictEquals(isBooleanObject(Boolean(true)), false); -}); - -// isBoxedPrimitive -Deno.test("Should return true for valid boxed primitive values", () => { - assertStrictEquals(isBoxedPrimitive(new Boolean(false)), true); - assertStrictEquals(isBoxedPrimitive(Object(Symbol("foo"))), true); - assertStrictEquals(isBoxedPrimitive(Object(BigInt(5))), true); - assertStrictEquals(isBoxedPrimitive(new String("foo")), true); -}); - -Deno.test("Should return false for invalid boxed primitive values", () => { - assertStrictEquals(isBoxedPrimitive(false), false); - assertStrictEquals(isBoxedPrimitive(Symbol("foo")), false); -}); - -// isDateView -Deno.test("Should return true for valid DataView types", () => { - assertStrictEquals(isDataView(new DataView(new ArrayBuffer(0))), true); -}); - -Deno.test("Should return false for invalid DataView types", () => { - assertStrictEquals(isDataView(new Float64Array(0)), false); -}); - -// isDate -Deno.test("Should return true for valid date types", () => { - assertStrictEquals(isDate(new Date()), true); - assertStrictEquals(isDate(new Date(0)), true); - assertStrictEquals(isDate(new (eval("Date"))()), true); -}); - -Deno.test("Should return false for invalid date types", () => { - assertStrictEquals(isDate(Date()), false); - assertStrictEquals(isDate({}), false); - assertStrictEquals(isDate([]), false); - assertStrictEquals(isDate(new Error()), false); - assertStrictEquals(isDate(Object.create(Date.prototype)), false); -}); - -// isFloat32Array -Deno.test("Should return true for valid Float32Array types", () => { - assertStrictEquals(isFloat32Array(new Float32Array(0)), true); -}); - -Deno.test("Should return false for invalid Float32Array types", () => { - assertStrictEquals(isFloat32Array(new ArrayBuffer(0)), false); - assertStrictEquals(isFloat32Array(new Float64Array(0)), false); -}); - -// isFloat64Array -Deno.test("Should return true for valid Float64Array types", () => { - assertStrictEquals(isFloat64Array(new Float64Array(0)), true); -}); - -Deno.test("Should return false for invalid Float64Array types", () => { - assertStrictEquals(isFloat64Array(new ArrayBuffer(0)), false); - assertStrictEquals(isFloat64Array(new Uint8Array(0)), false); -}); - -// isGeneratorFunction -Deno.test("Should return true for valid generator functions", () => { - assertStrictEquals( - isGeneratorFunction(function* foo() {}), - true, - ); -}); - -Deno.test("Should return false for invalid generator functions", () => { - assertStrictEquals( - isGeneratorFunction(function foo() {}), - false, - ); -}); - -// isGeneratorObject -Deno.test("Should return true for valid generator object types", () => { - function* foo(): Iterator<void> {} - assertStrictEquals(isGeneratorObject(foo()), true); -}); - -Deno.test("Should return false for invalid generation object types", () => { - assertStrictEquals( - isGeneratorObject(function* foo() {}), - false, - ); -}); - -// isInt8Array -Deno.test("Should return true for valid Int8Array types", () => { - assertStrictEquals(isInt8Array(new Int8Array(0)), true); -}); - -Deno.test("Should return false for invalid Int8Array types", () => { - assertStrictEquals(isInt8Array(new ArrayBuffer(0)), false); - assertStrictEquals(isInt8Array(new Float64Array(0)), false); -}); - -// isInt16Array -Deno.test("Should return true for valid Int16Array types", () => { - assertStrictEquals(isInt16Array(new Int16Array(0)), true); -}); - -Deno.test("Should return false for invalid Int16Array type", () => { - assertStrictEquals(isInt16Array(new ArrayBuffer(0)), false); - assertStrictEquals(isInt16Array(new Float64Array(0)), false); -}); - -// isInt32Array -Deno.test("Should return true for valid isInt32Array types", () => { - assertStrictEquals(isInt32Array(new Int32Array(0)), true); -}); - -Deno.test("Should return false for invalid isInt32Array type", () => { - assertStrictEquals(isInt32Array(new ArrayBuffer(0)), false); - assertStrictEquals(isInt32Array(new Float64Array(0)), false); -}); - -// isStringObject -Deno.test("Should return true for valid String types", () => { - assertStrictEquals(isStringObject(new String("")), true); - assertStrictEquals(isStringObject(new String("Foo")), true); -}); - -Deno.test("Should return false for invalid String types", () => { - assertStrictEquals(isStringObject(""), false); - assertStrictEquals(isStringObject("Foo"), false); -}); - -// isMap -Deno.test("Should return true for valid Map types", () => { - assertStrictEquals(isMap(new Map()), true); -}); - -Deno.test("Should return false for invalid Map types", () => { - assertStrictEquals(isMap({}), false); - assertStrictEquals(isMap([]), false); - assertStrictEquals(isMap(new Date()), false); - assertStrictEquals(isMap(new Error()), false); -}); - -// isMapIterator -Deno.test("Should return true for valid Map Iterator types", () => { - const map = new Map(); - assertStrictEquals(isMapIterator(map.keys()), true); - assertStrictEquals(isMapIterator(map.values()), true); - assertStrictEquals(isMapIterator(map.entries()), true); - assertStrictEquals(isMapIterator(map[Symbol.iterator]()), true); -}); - -Deno.test("Should return false for invalid Map iterator types", () => { - assertStrictEquals(isMapIterator(new Map()), false); - assertStrictEquals(isMapIterator([]), false); - assertStrictEquals(isMapIterator(new Date()), false); - assertStrictEquals(isMapIterator(new Error()), false); -}); - -// isModuleNamespaceObject -Deno.test("Should return true for valid module namespace objects", () => { - assertStrictEquals(isModuleNamespaceObject(testModuleNamespaceOpbject), true); -}); - -Deno.test("Should return false for invalid module namespace objects", () => { - assertStrictEquals(isModuleNamespaceObject(assertStrictEquals), false); -}); - -// isNativeError -Deno.test("Should return true for valid Error types", () => { - assertStrictEquals(isNativeError(new Error()), true); - assertStrictEquals(isNativeError(new TypeError()), true); - assertStrictEquals(isNativeError(new RangeError()), true); -}); - -Deno.test("Should return false for invalid Error types", () => { - assertStrictEquals(isNativeError(null), false); - assertStrictEquals(isNativeError(NaN), false); -}); - -// isNumberObject -Deno.test("Should return true for valid number objects", () => { - assertStrictEquals(isNumberObject(new Number(0)), true); -}); - -Deno.test("Should return false for invalid number types", () => { - assertStrictEquals(isNumberObject(0), false); -}); - -// isBigIntObject -Deno.test("Should return true for valid number objects", () => { - assertStrictEquals(isBigIntObject(new Object(BigInt(42))), true); -}); - -Deno.test("Should return false for invalid number types", () => { - assertStrictEquals(isBigIntObject(BigInt(42)), false); -}); - -// isPromise -Deno.test("Should return true for valid Promise types", () => { - assertStrictEquals(isPromise(Promise.resolve(42)), true); -}); - -Deno.test("Should return false for invalid Promise types", () => { - assertStrictEquals(isPromise(new Object()), false); -}); - -// isRegExp -Deno.test("Should return true for valid RegExp", () => { - assertStrictEquals(isRegExp(/abc/), true); - assertStrictEquals(isRegExp(new RegExp("abc")), true); -}); - -Deno.test("Should return false for invalid RegExp types", () => { - assertStrictEquals(isRegExp({}), false); - assertStrictEquals(isRegExp("/abc/"), false); -}); - -// isSet -Deno.test("Should return true for valid Set types", () => { - assertStrictEquals(isSet(new Set()), true); -}); - -Deno.test("Should return false for invalid Set types", () => { - assertStrictEquals(isSet({}), false); - assertStrictEquals(isSet([]), false); - assertStrictEquals(isSet(new Map()), false); - assertStrictEquals(isSet(new Error()), false); -}); - -// isSetIterator -Deno.test("Should return true for valid Set Iterator types", () => { - const set = new Set(); - assertStrictEquals(isSetIterator(set.keys()), true); - assertStrictEquals(isSetIterator(set.values()), true); - assertStrictEquals(isSetIterator(set.entries()), true); - assertStrictEquals(isSetIterator(set[Symbol.iterator]()), true); -}); - -Deno.test("Should return false for invalid Set Iterator types", () => { - assertStrictEquals(isSetIterator(new Set()), false); - assertStrictEquals(isSetIterator([]), false); - assertStrictEquals(isSetIterator(new Map()), false); - assertStrictEquals(isSetIterator(new Error()), false); -}); - -// isSharedArrayBuffer -Deno.test("Should return true for valid SharedArrayBuffer types", () => { - assertStrictEquals(isSharedArrayBuffer(new SharedArrayBuffer(0)), true); -}); - -Deno.test("Should return false for invalid SharedArrayBuffer types", () => { - assertStrictEquals(isSharedArrayBuffer(new ArrayBuffer(0)), false); -}); - -// isStringObject -Deno.test("Should return true for valid String Object types", () => { - assertStrictEquals(isStringObject(new String("")), true); - assertStrictEquals(isStringObject(new String("Foo")), true); -}); - -Deno.test("Should return false for invalid String Object types", () => { - assertStrictEquals(isStringObject(""), false); - assertStrictEquals(isStringObject("Foo"), false); -}); - -// isSymbolObject -Deno.test("Should return true for valid Symbol types", () => { - assertStrictEquals(isSymbolObject(Object(Symbol("foo"))), true); -}); - -Deno.test("Should return false for invalid Symbol types", () => { - assertStrictEquals(isSymbolObject(Symbol("foo")), false); -}); - -// isTypedArray -Deno.test("Should return true for valid TypedArray types", () => { - assertStrictEquals(isTypedArray(new Uint8Array(0)), true); - assertStrictEquals(isTypedArray(new Float64Array(0)), true); -}); - -Deno.test("Should return false for invalid TypedArray types", () => { - assertStrictEquals(isTypedArray(new ArrayBuffer(0)), false); -}); - -// isUint8Array -Deno.test("Should return true for valid Uint8Array types", () => { - assertStrictEquals(isUint8Array(new Uint8Array(0)), true); -}); - -Deno.test("Should return false for invalid Uint8Array types", () => { - assertStrictEquals(isUint8Array(new ArrayBuffer(0)), false); - assertStrictEquals(isUint8Array(new Float64Array(0)), false); -}); - -// isUint8ClampedArray -Deno.test("Should return true for valid Uint8ClampedArray types", () => { - assertStrictEquals(isUint8ClampedArray(new Uint8ClampedArray(0)), true); -}); - -Deno.test("Should return false for invalid Uint8Array types", () => { - assertStrictEquals(isUint8ClampedArray(new ArrayBuffer(0)), false); - assertStrictEquals(isUint8ClampedArray(new Float64Array(0)), false); -}); - -// isUint16Array -Deno.test("Should return true for valid isUint16Array types", () => { - assertStrictEquals(isUint16Array(new Uint16Array(0)), true); -}); - -Deno.test("Should return false for invalid Uint16Array types", () => { - assertStrictEquals(isUint16Array(new ArrayBuffer(0)), false); - assertStrictEquals(isUint16Array(new Float64Array(0)), false); -}); - -// isUint32Array -Deno.test("Should return true for valid Uint32Array types", () => { - assertStrictEquals(isUint32Array(new Uint32Array(0)), true); -}); - -Deno.test("Should return false for invalid isUint16Array types", () => { - assertStrictEquals(isUint32Array(new ArrayBuffer(0)), false); - assertStrictEquals(isUint32Array(new Float64Array(0)), false); -}); - -// isWeakMap -Deno.test("Should return true for valid WeakMap types", () => { - assertStrictEquals(isWeakMap(new WeakMap()), true); -}); - -Deno.test("Should return false for invalid WeakMap types", () => { - assertStrictEquals(isWeakMap(new Set()), false); - assertStrictEquals(isWeakMap(new Map()), false); -}); - -// isWeakSet -Deno.test("Should return true for valid WeakSet types", () => { - assertStrictEquals(isWeakSet(new WeakSet()), true); -}); - -Deno.test("Should return false for invalid WeakSet types", () => { - assertStrictEquals(isWeakSet(new Set()), false); - assertStrictEquals(isWeakSet(new Map()), false); -}); |