From 822b66cfd83e43084c6f788f865f5aa4f1475cb6 Mon Sep 17 00:00:00 2001 From: marlonpassos Date: Sun, 15 Sep 2024 03:57:36 +0000 Subject: [PATCH 01/15] docs(list): fix error in example --- docs/array/list.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/array/list.mdx b/docs/array/list.mdx index 0117056f..abfcfefb 100644 --- a/docs/array/list.mdx +++ b/docs/array/list.mdx @@ -20,7 +20,7 @@ _.list(0, 3, 'y') // [y, y, y, y] _.list(0, 3, () => 'y') // [y, y, y, y] _.list(0, 3, i => i) // [0, 1, 2, 3] _.list(0, 3, i => `y${i}`) // [y0, y1, y2, y3] -_.list(0, 3, obj) // [obj, obj, obj, obj] +_.list(0, 3, {}) // [{}, {}, {}, {}] _.list(0, 6, i => i, 2) // [0, 2, 4, 6] ``` From 9f681235742e09511d6a2493ba6ac96db7192f60 Mon Sep 17 00:00:00 2001 From: marlonpassos Date: Sun, 15 Sep 2024 04:17:24 +0000 Subject: [PATCH 02/15] docs(defer): fix error in example --- docs/async/defer.mdx | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/docs/async/defer.mdx b/docs/async/defer.mdx index 7655ca4a..26e8e189 100644 --- a/docs/async/defer.mdx +++ b/docs/async/defer.mdx @@ -21,6 +21,13 @@ strategy so that error in the cleanup function is rethrown. ```ts import * as _ from 'radashi' +const createBuildDir = async () => { + /* create build dir... */ +} +const build = async () => { + /* build project... */ +} + await _.defer(async cleanup => { const buildDir = await createBuildDir() @@ -29,12 +36,30 @@ await _.defer(async cleanup => { await build() }) +const api = { + org: { + create: async () => ({ id: 1 }), + delete: async () => { + /* delete org... */ + }, + }, + user: { + create: async () => ({ id: 2 }), + delete: async () => { + /* delete delete... */ + }, + }, +} +const executeTest = async () => { + /* exec text... */ +} + await _.defer(async register => { const org = await api.org.create() register(async () => api.org.delete(org.id), { rethrow: true }) const user = await api.user.create() - register(async () => api.users.delete(user.id), { rethrow: true }) + register(async () => api.user.delete(user.id), { rethrow: true }) await executeTest(org, user) }) From 11efc79bd0f28e0c6b0a9145efb383b2c916009e Mon Sep 17 00:00:00 2001 From: marlonpassos Date: Sun, 15 Sep 2024 04:35:35 +0000 Subject: [PATCH 03/15] docs(guard): fix error in example --- docs/async/guard.mdx | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/docs/async/guard.mdx b/docs/async/guard.mdx index 387607d4..f612064e 100644 --- a/docs/async/guard.mdx +++ b/docs/async/guard.mdx @@ -8,12 +8,27 @@ description: Have a function return undefined if it errors out This lets you set a default value if an async function errors out. ```ts -const users = (await guard(fetchUsers)) ?? [] +const fetchUsers = async () => { + throw new Error() +} +const users = (await _.guard(fetchUsers)) ?? [] // [] ``` You can choose to guard only specific errors too ```ts +class InvalidUserError extends Error { + code = 'INVALID_ID' +} +const fetchUserA = async () => { + throw new InvalidUserError() +} +const fetchUserB = async () => { + throw new Error() +} +const DEFAULT_USER = { name: 'Jhon Doe' } const isInvalidUserError = (err: any) => err.code === 'INVALID_ID' -const user = (await guard(fetchUser, isInvalidUserError)) ?? DEFAULT_USER + +const userA = (await _.guard(fetchUserA, isInvalidUserError)) ?? DEFAULT_USER // { name: "Jhon Doe"} +// const userB = (await _.guard(fetchUserB, isInvalidUserError)) ?? DEFAULT_USER // this throw ``` From 311c72efc8a221ed5d7d51b929748829a5f0ad24 Mon Sep 17 00:00:00 2001 From: marlonpassos Date: Sun, 15 Sep 2024 04:40:26 +0000 Subject: [PATCH 04/15] docs(map): fix error in example --- docs/async/guard.mdx | 4 ++-- docs/async/map.mdx | 9 ++++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/docs/async/guard.mdx b/docs/async/guard.mdx index f612064e..4c62eb19 100644 --- a/docs/async/guard.mdx +++ b/docs/async/guard.mdx @@ -26,9 +26,9 @@ const fetchUserA = async () => { const fetchUserB = async () => { throw new Error() } -const DEFAULT_USER = { name: 'Jhon Doe' } +const DEFAULT_USER = { name: 'John Doe' } const isInvalidUserError = (err: any) => err.code === 'INVALID_ID' -const userA = (await _.guard(fetchUserA, isInvalidUserError)) ?? DEFAULT_USER // { name: "Jhon Doe"} +const userA = (await _.guard(fetchUserA, isInvalidUserError)) ?? DEFAULT_USER // { name: "John Doe"} // const userB = (await _.guard(fetchUserB, isInvalidUserError)) ?? DEFAULT_USER // this throw ``` diff --git a/docs/async/map.mdx b/docs/async/map.mdx index c2e799cf..62dc9ac5 100644 --- a/docs/async/map.mdx +++ b/docs/async/map.mdx @@ -8,11 +8,14 @@ description: Map an array with an async function A map that handles callback functions that return a promise. ```ts -import * as _ from 'radashi' - const userIds = [1, 2, 3, 4] +const api = { + users: { + find: async (id: number) => id < 3, + }, +} const users = await _.map(userIds, async userId => { return await api.users.find(userId) -}) +}) // [true, true, false, false] ``` From 93603a2078357581ca3f424eb511aba71f23469b Mon Sep 17 00:00:00 2001 From: marlonpassos Date: Sun, 15 Sep 2024 04:45:57 +0000 Subject: [PATCH 05/15] docs(reduce): fix error in example --- docs/async/reduce.mdx | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/docs/async/reduce.mdx b/docs/async/reduce.mdx index ca87a612..8d57899a 100644 --- a/docs/async/reduce.mdx +++ b/docs/async/reduce.mdx @@ -11,6 +11,13 @@ A reduce that handles callback functions that return a promise. import * as _ from 'radashi' const userIds = [1, 2, 3, 4] +const api = { + users: { + async find(id: number) { + return { name: `person ${id}` } + }, + }, +} const users = await _.reduce( userIds, @@ -22,5 +29,5 @@ const users = await _.reduce( } }, {}, -) +) // { 1: { name: 'person 1' }, 2: { name: 'person 2' }, 3: { name: 'person 3' }, 4: { name: 'person 4' } } ``` From 8fcdd855413693beaaf10e09ab5da9e3db682ad9 Mon Sep 17 00:00:00 2001 From: marlonpassos Date: Sun, 15 Sep 2024 04:57:09 +0000 Subject: [PATCH 06/15] docs(retry): fix error in example --- docs/async/retry.mdx | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/docs/async/retry.mdx b/docs/async/retry.mdx index c0ca0188..a2728c64 100644 --- a/docs/async/retry.mdx +++ b/docs/async/retry.mdx @@ -14,10 +14,21 @@ The `backoff` option is like delay but uses a function to sleep -- makes for eas ```ts import * as _ from 'radashi' -await _.retry({}, api.users.list) -await _.retry({ times: 10 }, api.users.list) -await _.retry({ times: 2, delay: 1000 }, api.users.list) +const api = { + users: { + async list() { + if (Math.random() < 0.5) { + throw new Error('Random error') + } + return [] + }, + }, +} + +await _.retry({}, api.users.list) // try 3 times before failing +await _.retry({ times: 10 }, api.users.list) // try 10 times before failing +await _.retry({ times: 2, delay: 1000 }, api.users.list) // try 2 times with 1 second delay // exponential backoff -await _.retry({ backoff: i => 10 ** i }, api.users.list) +await _.retry({ backoff: i => 10 ** i }, api.users.list) // try 3 times with 10, 100, 1000 ms delay ``` From 7128fe0eaaf7c90cbec5c719993289f71404bf15 Mon Sep 17 00:00:00 2001 From: marlonpassos Date: Mon, 16 Sep 2024 16:11:58 +0000 Subject: [PATCH 07/15] docs(tryit): fix error in example --- docs/async/tryit.mdx | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/docs/async/tryit.mdx b/docs/async/tryit.mdx index bca5c4bb..1ee7d8e0 100644 --- a/docs/async/tryit.mdx +++ b/docs/async/tryit.mdx @@ -12,7 +12,16 @@ The `tryit` function let's you wrap a function to convert it to an error-first f ```ts import * as _ from 'radashi' -const [err, user] = await _.tryit(api.users.find)(userId) +const api = { + users: { + find: async (id: number) => id < 3 ? { id, name: 'Alice' } : throw new Error('Not found'), + }, +} +const userIdA = 1 +const userIdB = 3 + +const [err, user] = await _.tryit(api.users.find)(userId) // [null, { id: 1, name: 'Alice' }] +const [err, user] = await _.tryit(api.users.find)(userIdB) // [Error('Not found'), undefined] ``` ### Currying From 4c128ee8d35e66cd47ab6c424ef9b36ad1db97a7 Mon Sep 17 00:00:00 2001 From: marlonpassos Date: Mon, 16 Sep 2024 16:13:21 +0000 Subject: [PATCH 08/15] docs(withResolvers): fix error in example --- docs/async/withResolvers.mdx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/async/withResolvers.mdx b/docs/async/withResolvers.mdx index e5dcd099..c73804ac 100644 --- a/docs/async/withResolvers.mdx +++ b/docs/async/withResolvers.mdx @@ -10,7 +10,9 @@ Creates a new promise and returns the resolve and reject functions along with th The ponyfill for https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/withResolvers ```ts -const { resolve, reject, promise } = withResolvers() +import * as _ from 'radashi' + +const { resolve, reject, promise } = _.withResolvers() resolve(42) ``` From b95f1bf1f40edd30b62d032a43a9f198235ea9e7 Mon Sep 17 00:00:00 2001 From: marlonpassos Date: Mon, 16 Sep 2024 16:19:21 +0000 Subject: [PATCH 09/15] docs(debounce): fix error in example --- docs/curry/debounce.mdx | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/docs/curry/debounce.mdx b/docs/curry/debounce.mdx index a84fb827..273c7ce0 100644 --- a/docs/curry/debounce.mdx +++ b/docs/curry/debounce.mdx @@ -12,14 +12,18 @@ If called again during this waiting period, it resets the timer. Your source fun ```ts import * as _ from 'radashi' -// Send a search request to the API server when the user stops typing -// for at least 100ms. -input.addEventListener( - 'change', - _.debounce({ delay: 100 }, (event: InputEvent) => { - api.movies.search(event.target.value) - }), -) +const processData = (data: string) => { + console.log(`Processing data: "${data}"...`) +} +const debouncedProcessData = _.debounce({ delay: 100 }, processData) + +debouncedProcessData('data1') // Never logs +debouncedProcessData('data2') // Never logs +debouncedProcessData('data3') // Processing data: "data3"... + +setTimeout(() => { + debouncedProcessData('data4') // Processing data: "data4"... (200ms later) +}, 200) ``` ## Options From c13f7108ea5d72bcb0176ba562fe2791c0fd8b0c Mon Sep 17 00:00:00 2001 From: marlonpassos Date: Mon, 16 Sep 2024 16:20:11 +0000 Subject: [PATCH 10/15] docs(once): fix error in example --- docs/curry/once.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/curry/once.mdx b/docs/curry/once.mdx index 76175f60..d44d71bb 100644 --- a/docs/curry/once.mdx +++ b/docs/curry/once.mdx @@ -10,7 +10,7 @@ Create a wrapper around a given function such that it executes at most once. Sub ```ts import * as _ from 'radashi' -const fn = once(() => Math.random()) +const fn = _.once(() => Math.random()) fn() // 0.5 fn() // 0.5 ``` From 2388b013080aca6e9cd2feb88a0b2fcfaadc909e Mon Sep 17 00:00:00 2001 From: marlonpassos Date: Mon, 16 Sep 2024 16:22:39 +0000 Subject: [PATCH 11/15] docs(throttle): fix error in example --- docs/curry/throttle.mdx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/curry/throttle.mdx b/docs/curry/throttle.mdx index 3691013e..a7edcf8b 100644 --- a/docs/curry/throttle.mdx +++ b/docs/curry/throttle.mdx @@ -20,17 +20,17 @@ The returned throttled function also includes these methods: - `trigger(...args): void`: To invoke the wrapped function without waiting for the next interval ```typescript -import { throttle } from 'radashi' +import * as _ from 'radashi' // Throttle a scroll event handler const handleScroll = () => { console.log('Scroll position:', window.scrollY) } -const throttledScroll = throttle({ interval: 200 }, handleScroll) +const throttledScroll = _.throttle({ interval: 200 }, handleScroll) window.addEventListener('scroll', throttledScroll) // Throttle an API call -const throttledFetch = throttle( +const throttledFetch = _.throttle( { interval: 5000, trailing: true }, async () => { const response = await fetch('https://api.example.com/data') From 40a3ea19d95efae2a6a431c286ff6a24a7d96f06 Mon Sep 17 00:00:00 2001 From: marlonpassos Date: Mon, 16 Sep 2024 16:24:10 +0000 Subject: [PATCH 12/15] docs(castComparator): fix error in example --- docs/function/castComparator.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/function/castComparator.mdx b/docs/function/castComparator.mdx index d881d053..e31b24f4 100644 --- a/docs/function/castComparator.mdx +++ b/docs/function/castComparator.mdx @@ -13,7 +13,7 @@ The first argument of `castComparator` is called the `mapping`. This can be eith - **Property Name**: If `mapping` is a property name, it maps the input values to a property of the input values with a comparable value. ```ts -import * as _ from 'radash' +import * as _ from 'radashi' const users = [ { id: 1, firstName: 'Alice', lastName: 'Smith' }, From 1a0b68925e53e816a089fc359567af581b540068 Mon Sep 17 00:00:00 2001 From: marlonpassos Date: Mon, 16 Sep 2024 16:27:30 +0000 Subject: [PATCH 13/15] docs(clamp): fix error in example --- docs/number/clamp.mdx | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/docs/number/clamp.mdx b/docs/number/clamp.mdx index 58e8b85c..01c3a9af 100644 --- a/docs/number/clamp.mdx +++ b/docs/number/clamp.mdx @@ -23,5 +23,9 @@ _.clamp(0, 1, 10) // returns 1 _.clamp(15, 1, 10) // returns 10 // Invalid range -_.clamp(1, 10, 1) // throws +try { + _.clamp(1, 10, 1) +} catch (e) { + console.log(`error: ${e.message}`) // error: Invalid range +} ``` From d67466e0b38c5de1a302b573aa8eb674ff9096a6 Mon Sep 17 00:00:00 2001 From: marlonpassos Date: Mon, 16 Sep 2024 16:30:08 +0000 Subject: [PATCH 14/15] docs(isResultErr): fix error in example --- docs/typed/isResultErr.mdx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/typed/isResultErr.mdx b/docs/typed/isResultErr.mdx index f976b67a..15b5643d 100644 --- a/docs/typed/isResultErr.mdx +++ b/docs/typed/isResultErr.mdx @@ -8,9 +8,11 @@ description: Returns true for failed Result tuple Check if a value is both a `Result` tuple and an `Err` result. ```ts +import * as _ from 'radashi' + declare const value: unknown -if (isResultErr(value)) { +if (_.isResultErr(value)) { value // <-- now an Err type value[0] // <-- This is the error! } From 1a90ccc24e5e00ff296899d827e064e0fcc12618 Mon Sep 17 00:00:00 2001 From: marlonpassos Date: Mon, 16 Sep 2024 16:30:28 +0000 Subject: [PATCH 15/15] docs(isResultOk): fix error in example --- docs/typed/isResultOk.mdx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/typed/isResultOk.mdx b/docs/typed/isResultOk.mdx index 5485e3fb..5c06c38d 100644 --- a/docs/typed/isResultOk.mdx +++ b/docs/typed/isResultOk.mdx @@ -8,9 +8,11 @@ description: Returns true for successful Result tuple Check if a value is both a `Result` tuple and an `Ok` result. ```ts +import * as _ from 'radashi' + declare const value: unknown -if (isResultOk(value)) { +if (_.isResultOk(value)) { value // <-- now an Ok type value[1] // <-- This is the resulting value! }