@ayonli/jsext
Advanced tools
Comparing version 0.5.0 to 0.5.1
@@ -17,10 +17,10 @@ 'use strict'; | ||
* | ||
* @experimental | ||
* | ||
* @example | ||
* it("should output as expected", example(console => { | ||
* console.log("Hello, World!"); | ||
* // output: | ||
* // Hello, World! | ||
* })); | ||
* ```ts | ||
* it("should output as expected", example(console => { | ||
* console.log("Hello, World!"); | ||
* // output: | ||
* // Hello, World! | ||
* })); | ||
* ``` | ||
*/ | ||
@@ -27,0 +27,0 @@ function example(fn, options = undefined) { |
@@ -17,11 +17,13 @@ 'use strict'; | ||
* @example | ||
* const getVersion = func(async (defer) => { | ||
* const file = await fs.open("./package.json", "r"); | ||
* defer(() => file.close()); | ||
* ```ts | ||
* const getVersion = func(async (defer) => { | ||
* const file = await fs.open("./package.json", "r"); | ||
* defer(() => file.close()); | ||
* | ||
* const content = await file.readFile("utf8"); | ||
* const pkg = JSON.parse(content); | ||
* const content = await file.readFile("utf8"); | ||
* const pkg = JSON.parse(content); | ||
* | ||
* return pkg.version as string; | ||
* }); | ||
* return pkg.version as string; | ||
* }); | ||
* ``` | ||
*/ | ||
@@ -28,0 +30,0 @@ function func(fn) { |
@@ -9,6 +9,8 @@ 'use strict'; | ||
* @example | ||
* class Moment extends Date {} | ||
* ```ts | ||
* class Moment extends Date {} | ||
* | ||
* console.assert(isSubclassOf(Moment, Date)); | ||
* console.assert(isSubclassOf(Moment, Object)); // all classes are subclasses of Object | ||
* console.assert(isSubclassOf(Moment, Date)); | ||
* console.assert(isSubclassOf(Moment, Object)); // all classes are subclasses of Object | ||
* ``` | ||
*/ | ||
@@ -15,0 +17,0 @@ function isSubclassOf(ctor1, ctor2) { |
@@ -38,22 +38,30 @@ 'use strict'; | ||
* @example | ||
* const job1 = await run("./job-example.mjs", ["World"]); | ||
* console.log(await job1.result()); // Hello, World | ||
* ```ts | ||
* const job1 = await run("./job-example.mjs", ["World"]); | ||
* console.log(await job1.result()); // Hello, World | ||
* ``` | ||
* | ||
* const job2 = await run<string, [string[]]>("./job-example.mjs", [["foo", "bar"]], { | ||
* fn: "sequence", | ||
* }); | ||
* for await (const word of job2.iterate()) { | ||
* console.log(word); | ||
* // Output: | ||
* // foo | ||
* // bar | ||
* } | ||
* @example | ||
* ```ts | ||
* const job2 = await run<string, [string[]]>("./job-example.mjs", [["foo", "bar"]], { | ||
* fn: "sequence", | ||
* }); | ||
* for await (const word of job2.iterate()) { | ||
* console.log(word); | ||
* } | ||
* // output: | ||
* // foo | ||
* // bar | ||
* ``` | ||
* | ||
* const job3 = await run<string, [string]>("job-example.mjs", ["foobar"], { | ||
* fn: "takeTooLong", | ||
* }); | ||
* await job3.abort(); | ||
* const [err, res] = await jsext.try(job3.result()); | ||
* console.assert(err === null); | ||
* console.assert(res === undefined); | ||
* @example | ||
* ```ts | ||
* const job3 = await run<string, [string]>("./job-example.mjs", ["foobar"], { | ||
* fn: "takeTooLong", | ||
* }); | ||
* await job3.abort(); | ||
* const [err, res] = await _try(job3.result()); | ||
* console.assert(err === null); | ||
* console.assert(res === undefined); | ||
* ``` | ||
*/ | ||
@@ -60,0 +68,0 @@ async function run(script, args = undefined, options = undefined) { |
@@ -10,13 +10,15 @@ 'use strict'; | ||
* @example | ||
* function log(text: string) { | ||
* console.log(text); | ||
* } | ||
* ```ts | ||
* function log(text: string) { | ||
* console.log(text); | ||
* } | ||
* | ||
* const show = wrap(log, function (fn, text) { | ||
* return fn.call(this, new Date().toISOString() + " " + text); | ||
* }); | ||
* const show = wrap(log, function (fn, text) { | ||
* return fn.call(this, new Date().toISOString() + " " + text); | ||
* }); | ||
* | ||
* console.log(show.name); // log | ||
* console.log(show.length); // 1 | ||
* console.assert(show.toString() === log.toString()); | ||
* console.log(show.name); // log | ||
* console.log(show.length); // 1 | ||
* console.assert(show.toString() === log.toString()); | ||
* ``` | ||
*/ | ||
@@ -23,0 +25,0 @@ function wrap(fn, wrapper) { |
@@ -13,10 +13,10 @@ /** | ||
* | ||
* @experimental | ||
* | ||
* @example | ||
* it("should output as expected", example(console => { | ||
* console.log("Hello, World!"); | ||
* // output: | ||
* // Hello, World! | ||
* })); | ||
* ```ts | ||
* it("should output as expected", example(console => { | ||
* console.log("Hello, World!"); | ||
* // output: | ||
* // Hello, World! | ||
* })); | ||
* ``` | ||
*/ | ||
@@ -23,0 +23,0 @@ function example(fn, options = undefined) { |
@@ -13,11 +13,13 @@ import { isAsyncGenerator, isGenerator } from './external/check-iterable/index.js'; | ||
* @example | ||
* const getVersion = func(async (defer) => { | ||
* const file = await fs.open("./package.json", "r"); | ||
* defer(() => file.close()); | ||
* ```ts | ||
* const getVersion = func(async (defer) => { | ||
* const file = await fs.open("./package.json", "r"); | ||
* defer(() => file.close()); | ||
* | ||
* const content = await file.readFile("utf8"); | ||
* const pkg = JSON.parse(content); | ||
* const content = await file.readFile("utf8"); | ||
* const pkg = JSON.parse(content); | ||
* | ||
* return pkg.version as string; | ||
* }); | ||
* return pkg.version as string; | ||
* }); | ||
* ``` | ||
*/ | ||
@@ -24,0 +26,0 @@ function func(fn) { |
@@ -5,6 +5,8 @@ /** | ||
* @example | ||
* class Moment extends Date {} | ||
* ```ts | ||
* class Moment extends Date {} | ||
* | ||
* console.assert(isSubclassOf(Moment, Date)); | ||
* console.assert(isSubclassOf(Moment, Object)); // all classes are subclasses of Object | ||
* console.assert(isSubclassOf(Moment, Date)); | ||
* console.assert(isSubclassOf(Moment, Object)); // all classes are subclasses of Object | ||
* ``` | ||
*/ | ||
@@ -11,0 +13,0 @@ function isSubclassOf(ctor1, ctor2) { |
@@ -33,22 +33,30 @@ import { sequence } from './number/index.js'; | ||
* @example | ||
* const job1 = await run("./job-example.mjs", ["World"]); | ||
* console.log(await job1.result()); // Hello, World | ||
* ```ts | ||
* const job1 = await run("./job-example.mjs", ["World"]); | ||
* console.log(await job1.result()); // Hello, World | ||
* ``` | ||
* | ||
* const job2 = await run<string, [string[]]>("./job-example.mjs", [["foo", "bar"]], { | ||
* fn: "sequence", | ||
* }); | ||
* for await (const word of job2.iterate()) { | ||
* console.log(word); | ||
* // Output: | ||
* // foo | ||
* // bar | ||
* } | ||
* @example | ||
* ```ts | ||
* const job2 = await run<string, [string[]]>("./job-example.mjs", [["foo", "bar"]], { | ||
* fn: "sequence", | ||
* }); | ||
* for await (const word of job2.iterate()) { | ||
* console.log(word); | ||
* } | ||
* // output: | ||
* // foo | ||
* // bar | ||
* ``` | ||
* | ||
* const job3 = await run<string, [string]>("job-example.mjs", ["foobar"], { | ||
* fn: "takeTooLong", | ||
* }); | ||
* await job3.abort(); | ||
* const [err, res] = await jsext.try(job3.result()); | ||
* console.assert(err === null); | ||
* console.assert(res === undefined); | ||
* @example | ||
* ```ts | ||
* const job3 = await run<string, [string]>("./job-example.mjs", ["foobar"], { | ||
* fn: "takeTooLong", | ||
* }); | ||
* await job3.abort(); | ||
* const [err, res] = await _try(job3.result()); | ||
* console.assert(err === null); | ||
* console.assert(res === undefined); | ||
* ``` | ||
*/ | ||
@@ -55,0 +63,0 @@ async function run(script, args = undefined, options = undefined) { |
@@ -6,13 +6,15 @@ /** | ||
* @example | ||
* function log(text: string) { | ||
* console.log(text); | ||
* } | ||
* ```ts | ||
* function log(text: string) { | ||
* console.log(text); | ||
* } | ||
* | ||
* const show = wrap(log, function (fn, text) { | ||
* return fn.call(this, new Date().toISOString() + " " + text); | ||
* }); | ||
* const show = wrap(log, function (fn, text) { | ||
* return fn.call(this, new Date().toISOString() + " " + text); | ||
* }); | ||
* | ||
* console.log(show.name); // log | ||
* console.log(show.length); // 1 | ||
* console.assert(show.toString() === log.toString()); | ||
* console.log(show.name); // log | ||
* console.log(show.length); // 1 | ||
* console.assert(show.toString() === log.toString()); | ||
* ``` | ||
*/ | ||
@@ -19,0 +21,0 @@ function wrap(fn, wrapper) { |
@@ -1,3 +0,1 @@ | ||
import type { AssertionError } from "node:assert"; | ||
declare var Deno: any; | ||
@@ -17,10 +15,10 @@ | ||
* | ||
* @experimental | ||
* | ||
* @example | ||
* it("should output as expected", example(console => { | ||
* console.log("Hello, World!"); | ||
* // output: | ||
* // Hello, World! | ||
* })); | ||
* ```ts | ||
* it("should output as expected", example(console => { | ||
* console.log("Hello, World!"); | ||
* // output: | ||
* // Hello, World! | ||
* })); | ||
* ``` | ||
*/ | ||
@@ -122,7 +120,7 @@ export default function example<T, A extends any[] = any[]>( | ||
} catch (err: unknown) { | ||
Object.defineProperty(err as AssertionError, "stack", { | ||
Object.defineProperty(err as Error, "stack", { | ||
configurable: true, | ||
writable: true, | ||
enumerable: false, | ||
value: (err as AssertionError).stack | ||
value: (err as Error).stack | ||
+ "\n" + call.stack?.split("\n").slice(1).join("\n"), | ||
@@ -129,0 +127,0 @@ }); |
20
func.ts
@@ -13,11 +13,13 @@ // @ts-ignore | ||
* @example | ||
* const getVersion = func(async (defer) => { | ||
* const file = await fs.open("./package.json", "r"); | ||
* defer(() => file.close()); | ||
* | ||
* const content = await file.readFile("utf8"); | ||
* const pkg = JSON.parse(content); | ||
* | ||
* return pkg.version as string; | ||
* }); | ||
* ```ts | ||
* const getVersion = func(async (defer) => { | ||
* const file = await fs.open("./package.json", "r"); | ||
* defer(() => file.close()); | ||
* | ||
* const content = await file.readFile("utf8"); | ||
* const pkg = JSON.parse(content); | ||
* | ||
* return pkg.version as string; | ||
* }); | ||
* ``` | ||
*/ | ||
@@ -24,0 +26,0 @@ export default function func<T, R = any, A extends any[] = any[]>( |
@@ -7,6 +7,8 @@ import { Constructor } from "./index.ts"; | ||
* @example | ||
* class Moment extends Date {} | ||
* ```ts | ||
* class Moment extends Date {} | ||
* | ||
* console.assert(isSubclassOf(Moment, Date)); | ||
* console.assert(isSubclassOf(Moment, Object)); // all classes are subclasses of Object | ||
* console.assert(isSubclassOf(Moment, Date)); | ||
* console.assert(isSubclassOf(Moment, Object)); // all classes are subclasses of Object | ||
* ``` | ||
*/ | ||
@@ -13,0 +15,0 @@ export default function isSubclassOf<T, B>(ctor1: Constructor<T>, ctor2: Constructor<B>): boolean { |
@@ -66,26 +66,28 @@ import type { Constructor } from "./index.ts"; | ||
* @example | ||
* class Log { | ||
* log(text: string) { | ||
* console.log(text); | ||
* } | ||
* } | ||
* ```ts | ||
* class Log { | ||
* log(text: string) { | ||
* console.log(text); | ||
* } | ||
* } | ||
* | ||
* class View { | ||
* display(data: Record<string, any>[]) { | ||
* console.table(data); | ||
* } | ||
* } | ||
* class View { | ||
* display(data: Record<string, any>[]) { | ||
* console.table(data); | ||
* } | ||
* } | ||
* | ||
* class Controller extends mixins(View, Log) { | ||
* constructor(readonly topic: string) { | ||
* super(); | ||
* } | ||
* } | ||
* class Controller extends mixins(View, Log) { | ||
* constructor(readonly topic: string) { | ||
* super(); | ||
* } | ||
* } | ||
* | ||
* const ctrl = new Controller("foo"); | ||
* ctrl.log("something is happening"); | ||
* ctrl.display([{ topic: ctrl.topic, content: "something is happening" }]); | ||
* const ctrl = new Controller("foo"); | ||
* ctrl.log("something is happening"); | ||
* ctrl.display([{ topic: ctrl.topic, content: "something is happening" }]); | ||
* | ||
* console.assert(isSubclassOf(Controller, View)); | ||
* console.assert(!isSubclassOf(Controller, Log)); | ||
* console.assert(isSubclassOf(Controller, View)); | ||
* console.assert(!isSubclassOf(Controller, Log)); | ||
* ``` | ||
*/ | ||
@@ -92,0 +94,0 @@ export default function mixins<T extends Constructor<any>, M extends any[]>( |
@@ -37,3 +37,5 @@ import { hasOwn, hasOwnMethod, omit, patch, pick, as, isValid } from "./index.ts"; | ||
* @example | ||
* Object.as(bar, SomeType)?.doSomething(); | ||
* ```ts | ||
* Object.as(bar, SomeType)?.doSomething(); | ||
* ``` | ||
*/ | ||
@@ -40,0 +42,0 @@ as(value: unknown, type: StringConstructor): string | null; |
@@ -89,3 +89,5 @@ import type { Constructor } from "../index.ts"; | ||
* @example | ||
* Object.as(bar, SomeType)?.doSomething(); | ||
* ```ts | ||
* Object.as(bar, SomeType)?.doSomething(); | ||
* ``` | ||
*/ | ||
@@ -92,0 +94,0 @@ export function as(value: unknown, type: StringConstructor): string | null; |
{ | ||
"name": "@ayonli/jsext", | ||
"version": "0.5.0", | ||
"version": "0.5.1", | ||
"description": "Additional functions for JavaScript programming in practice.", | ||
@@ -5,0 +5,0 @@ "exports": { |
58
read.ts
@@ -10,15 +10,17 @@ import { isFunction } from "./try.ts"; | ||
* @example | ||
* // listen to the `onmessage` | ||
* const sse = new EventSource("/sse/message"); | ||
* ```ts | ||
* // listen to the `onmessage` | ||
* const sse = new EventSource("/sse/message"); | ||
* | ||
* for await (const msg of read(sse)) { | ||
* console.log("receive message:", msg); | ||
* } | ||
* for await (const msg of read(sse)) { | ||
* console.log("receive message:", msg); | ||
* } | ||
* | ||
* // listen to a specific event | ||
* const channel = new EventSource("/sse/broadcast"); | ||
* | ||
* for await (const msg of read(channel, { event: "broadcast" })) { | ||
* console.log("receive message:", msg); | ||
* } | ||
* // listen to a specific event | ||
* const channel = new EventSource("/sse/broadcast"); | ||
* | ||
* for await (const msg of read(channel, { event: "broadcast" })) { | ||
* console.log("receive message:", msg); | ||
* } | ||
* ``` | ||
*/ | ||
@@ -28,11 +30,13 @@ export default function read(es: EventSource, options?: { event?: string; }): AsyncIterable<string>; | ||
* @example | ||
* const ws = new WebSocket("/ws"); | ||
* ```ts | ||
* const ws = new WebSocket("/ws"); | ||
* | ||
* for await (const msg of read(ws)) { | ||
* if (typeof data === "string") { | ||
* console.log("receive text message:", data); | ||
* } else { | ||
* console.log("receive binary data:", data); | ||
* } | ||
* } | ||
* for await (const msg of read(ws)) { | ||
* if (typeof data === "string") { | ||
* console.log("receive text message:", data); | ||
* } else { | ||
* console.log("receive binary data:", data); | ||
* } | ||
* } | ||
* ``` | ||
*/ | ||
@@ -42,5 +46,7 @@ export default function read<T extends Uint8Array | string>(ws: WebSocket): AsyncIterable<T>; | ||
* @example | ||
* for await (const msg of read(self)) { | ||
* console.log("receive message from the parent window:", msg); | ||
* } | ||
* ```ts | ||
* for await (const msg of read(self)) { | ||
* console.log("receive message from the parent window:", msg); | ||
* } | ||
* ``` | ||
*/ | ||
@@ -54,5 +60,7 @@ export default function read<T>(target: EventTarget, eventMap?: { | ||
* @example | ||
* for await (const msg of read(process)) { | ||
* console.log("receive message from the parent process:", msg); | ||
* } | ||
* ```ts | ||
* for await (const msg of read(process)) { | ||
* console.log("receive message from the parent process:", msg); | ||
* } | ||
* ``` | ||
*/ | ||
@@ -59,0 +67,0 @@ export default function read<T>(target: NodeJS.EventEmitter, eventMap?: { |
@@ -496,8 +496,20 @@ # JsExt | ||
console.log(word); | ||
// Output: | ||
// foo | ||
// bar | ||
} | ||
// output: | ||
// foo | ||
// bar | ||
``` | ||
**Example (abort)** | ||
```ts | ||
const job3 = await run<string, [string]>("./job-example.mjs", ["foobar"], { | ||
fn: "takeTooLong", | ||
}); | ||
await job3.abort(); | ||
const [err, res] = await _try(job3.result()); | ||
console.assert(err === null); | ||
console.assert(res === undefined); | ||
``` | ||
--- | ||
@@ -504,0 +516,0 @@ |
48
run.ts
@@ -1,3 +0,3 @@ | ||
import type { Worker as NodeWorker } from "worker_threads"; | ||
import type { ChildProcess } from "child_process"; | ||
import type { Worker as NodeWorker } from "node:worker_threads"; | ||
import type { ChildProcess } from "node:child_process"; | ||
import { sequence } from "./number/index.ts"; | ||
@@ -44,22 +44,30 @@ import { isFunction } from "./try.ts"; | ||
* @example | ||
* const job1 = await run("./job-example.mjs", ["World"]); | ||
* console.log(await job1.result()); // Hello, World | ||
* ```ts | ||
* const job1 = await run("./job-example.mjs", ["World"]); | ||
* console.log(await job1.result()); // Hello, World | ||
* ``` | ||
* | ||
* const job2 = await run<string, [string[]]>("./job-example.mjs", [["foo", "bar"]], { | ||
* fn: "sequence", | ||
* }); | ||
* for await (const word of job2.iterate()) { | ||
* console.log(word); | ||
* // Output: | ||
* // foo | ||
* // bar | ||
* } | ||
* @example | ||
* ```ts | ||
* const job2 = await run<string, [string[]]>("./job-example.mjs", [["foo", "bar"]], { | ||
* fn: "sequence", | ||
* }); | ||
* for await (const word of job2.iterate()) { | ||
* console.log(word); | ||
* } | ||
* // output: | ||
* // foo | ||
* // bar | ||
* ``` | ||
* | ||
* const job3 = await run<string, [string]>("job-example.mjs", ["foobar"], { | ||
* fn: "takeTooLong", | ||
* }); | ||
* await job3.abort(); | ||
* const [err, res] = await jsext.try(job3.result()); | ||
* console.assert(err === null); | ||
* console.assert(res === undefined); | ||
* @example | ||
* ```ts | ||
* const job3 = await run<string, [string]>("./job-example.mjs", ["foobar"], { | ||
* fn: "takeTooLong", | ||
* }); | ||
* await job3.abort(); | ||
* const [err, res] = await _try(job3.result()); | ||
* console.assert(err === null); | ||
* console.assert(res === undefined); | ||
* ``` | ||
*/ | ||
@@ -66,0 +74,0 @@ export default async function run<T, A extends any[] = any[]>( |
@@ -15,8 +15,10 @@ type ThrottleCache = { | ||
* @example | ||
* const fn = throttle((input: string) => input, 1_000); | ||
* console.log(fn("foo")); // foo | ||
* console.log(fn("bar")); // foo | ||
* ```ts | ||
* const fn = throttle((input: string) => input, 1_000); | ||
* console.log(fn("foo")); // foo | ||
* console.log(fn("bar")); // foo | ||
* | ||
* await Promise.sleep(1_000); | ||
* console.log(fn("bar")); // bar | ||
* await Promise.sleep(1_000); | ||
* console.log(fn("bar")); // bar | ||
* ``` | ||
*/ | ||
@@ -29,11 +31,13 @@ export default function throttle<T, Fn extends (this: T, ...args: any[]) => any>( | ||
* @example | ||
* const out1 = await throttle(() => Promise.resolve("foo"), { duration: 1_000, for: "example" })(); | ||
* console.log(out1); // foo | ||
* | ||
* const out2 = await throttle(() => Promise.resolve("bar"), { duration: 1_000, for: "example" })(); | ||
* console.log(out2); // foo | ||
* ```ts | ||
* const out1 = await throttle(() => Promise.resolve("foo"), { duration: 1_000, for: "example" })(); | ||
* console.log(out1); // foo | ||
* | ||
* await Promise.sleep(1_000); | ||
* const out3 = await throttle(() => Promise.resolve("bar"), { duration: 1_000, for: "example" })(); | ||
* console.log(out3); // bar | ||
* const out2 = await throttle(() => Promise.resolve("bar"), { duration: 1_000, for: "example" })(); | ||
* console.log(out2); // foo | ||
* | ||
* await Promise.sleep(1_000); | ||
* const out3 = await throttle(() => Promise.resolve("bar"), { duration: 1_000, for: "example" })(); | ||
* console.log(out3); // bar | ||
* ``` | ||
*/ | ||
@@ -40,0 +44,0 @@ export default function throttle<T, Fn extends (this: T, ...args: any[]) => any>(handler: Fn, options: { |
116
try.ts
@@ -12,13 +12,15 @@ // @ts-ignore | ||
* @example | ||
* const iter = _try(async function* () { | ||
* // do something that may fail | ||
* }); | ||
* ```ts | ||
* const iter = _try(async function* () { | ||
* // do something that may fail | ||
* }); | ||
* | ||
* for await (const [err, val] of iter) { | ||
* if (err) { | ||
* console.error("something went wrong:", err); | ||
* } else { | ||
* console.log("current value:", val); | ||
* } | ||
* } | ||
* for await (const [err, val] of iter) { | ||
* if (err) { | ||
* console.error("something went wrong:", err); | ||
* } else { | ||
* console.log("current value:", val); | ||
* } | ||
* } | ||
* ``` | ||
*/ | ||
@@ -33,13 +35,15 @@ export default function _try<E = Error, T = any, A extends any[] = any[], TReturn = any, TNext = unknown>( | ||
* @example | ||
* const iter = _try(function* () { | ||
* // do something that may fail | ||
* }); | ||
* ```ts | ||
* const iter = _try(function* () { | ||
* // do something that may fail | ||
* }); | ||
* | ||
* for (const [err, val] of iter) { | ||
* if (err) { | ||
* console.error("something went wrong:", err); | ||
* } else { | ||
* console.log("current value:", val); | ||
* } | ||
* } | ||
* for (const [err, val] of iter) { | ||
* if (err) { | ||
* console.error("something went wrong:", err); | ||
* } else { | ||
* console.log("current value:", val); | ||
* } | ||
* } | ||
* ``` | ||
*/ | ||
@@ -54,9 +58,11 @@ export default function _try<E = Error, T = any, A extends any[] = any[], TReturn = any, TNext = unknown>( | ||
* @example | ||
* let [err, res] = await _try(async () => { | ||
* return await axios.get("https://example.org"); | ||
* }); | ||
* ```ts | ||
* let [err, res] = await _try(async () => { | ||
* return await axios.get("https://example.org"); | ||
* }); | ||
* | ||
* if (err) { | ||
* res = (err as any)["response"]; | ||
* } | ||
* if (err) { | ||
* res = (err as any)["response"]; | ||
* } | ||
* ``` | ||
*/ | ||
@@ -71,5 +77,7 @@ export default function _try<E = Error, R = any, A extends any[] = any[]>( | ||
* @example | ||
* const [err, res] = _try(() => { | ||
* // do something that may fail | ||
* }); | ||
* ```ts | ||
* const [err, res] = _try(() => { | ||
* // do something that may fail | ||
* }); | ||
* ``` | ||
*/ | ||
@@ -84,13 +92,15 @@ export default function _try<E = Error, R = any, A extends any[] = any[]>( | ||
* @example | ||
* async function* gen() { | ||
* // do something that may fail | ||
* } | ||
* ```ts | ||
* async function* gen() { | ||
* // do something that may fail | ||
* } | ||
* | ||
* for await (const [err, val] of _try(gen())) { | ||
* if (err) { | ||
* console.error("something went wrong:", err); | ||
* } else { | ||
* console.log("current value:", val); | ||
* } | ||
* } | ||
* for await (const [err, val] of _try(gen())) { | ||
* if (err) { | ||
* console.error("something went wrong:", err); | ||
* } else { | ||
* console.log("current value:", val); | ||
* } | ||
* } | ||
* ``` | ||
*/ | ||
@@ -104,11 +114,13 @@ export default function _try<E = Error, T = any, TReturn = any, TNext = unknown>( | ||
* @example | ||
* const iter = Number.sequence(1, 10); | ||
* ```ts | ||
* const iter = Number.sequence(1, 10); | ||
* | ||
* for (const [err, val] of _try(iter)) { | ||
* if (err) { | ||
* console.error("something went wrong:", err); | ||
* } else { | ||
* console.log("current value:", val); | ||
* } | ||
* } | ||
* for (const [err, val] of _try(iter)) { | ||
* if (err) { | ||
* console.error("something went wrong:", err); | ||
* } else { | ||
* console.log("current value:", val); | ||
* } | ||
* } | ||
* ``` | ||
*/ | ||
@@ -122,7 +134,9 @@ export default function _try<E = Error, T = any, TReturn = any, TNext = unknown>( | ||
* @example | ||
* let [err, res] = await _try(axios.get("https://example.org")); | ||
* ```ts | ||
* let [err, res] = await _try(axios.get("https://example.org")); | ||
* | ||
* if (err) { | ||
* res = (err as any)["response"]; | ||
* } | ||
* if (err) { | ||
* res = (err as any)["response"]; | ||
* } | ||
* ``` | ||
*/ | ||
@@ -129,0 +143,0 @@ export default function _try<E = Error, R = any>(job: Promise<R>): Promise<[E | null, R]>; |
20
wrap.ts
@@ -6,13 +6,15 @@ /** | ||
* @example | ||
* function log(text: string) { | ||
* console.log(text); | ||
* } | ||
* ```ts | ||
* function log(text: string) { | ||
* console.log(text); | ||
* } | ||
* | ||
* const show = wrap(log, function (fn, text) { | ||
* return fn.call(this, new Date().toISOString() + " " + text); | ||
* }); | ||
* const show = wrap(log, function (fn, text) { | ||
* return fn.call(this, new Date().toISOString() + " " + text); | ||
* }); | ||
* | ||
* console.log(show.name); // log | ||
* console.log(show.length); // 1 | ||
* console.assert(show.toString() === log.toString()); | ||
* console.log(show.name); // log | ||
* console.log(show.length); // 1 | ||
* console.assert(show.toString() === log.toString()); | ||
* ``` | ||
*/ | ||
@@ -19,0 +21,0 @@ export default function wrap<T, Fn extends (this: T, ...args: any[]) => any>( |
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
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
569917
7228
839
4