From 4f05fd911f535579bca4e3c3e814aef22397f849 Mon Sep 17 00:00:00 2001 From: marlonpassos Date: Sun, 15 Sep 2024 03:57:36 +0000 Subject: [PATCH 01/19] 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 b91df2e9..70f79104 100644 --- a/docs/array/list.mdx +++ b/docs/array/list.mdx @@ -21,7 +21,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 b5440b7b8fbc992e792d56cc05b57f460912f444 Mon Sep 17 00:00:00 2001 From: marlonpassos Date: Sun, 15 Sep 2024 04:17:24 +0000 Subject: [PATCH 02/19] 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 5fde5385..968ae6b4 100644 --- a/docs/async/defer.mdx +++ b/docs/async/defer.mdx @@ -22,6 +22,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() @@ -30,12 +37,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 9e45f8e4a00d3de6d984cbf3977650ad054612e2 Mon Sep 17 00:00:00 2001 From: marlonpassos Date: Sun, 15 Sep 2024 04:35:35 +0000 Subject: [PATCH 03/19] 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 0015a486..110c2fe6 100644 --- a/docs/async/guard.mdx +++ b/docs/async/guard.mdx @@ -9,12 +9,27 @@ since: 12.1.0 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 8f4dce6512be68992d3fbe65727351fb6e44777c Mon Sep 17 00:00:00 2001 From: marlonpassos Date: Sun, 15 Sep 2024 04:40:26 +0000 Subject: [PATCH 04/19] 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 110c2fe6..9a162a9d 100644 --- a/docs/async/guard.mdx +++ b/docs/async/guard.mdx @@ -27,9 +27,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 6444f46e..66a3d400 100644 --- a/docs/async/map.mdx +++ b/docs/async/map.mdx @@ -9,11 +9,14 @@ since: 12.1.0 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 0caab660aa95711c6c5c02600bfc5f6f62fec831 Mon Sep 17 00:00:00 2001 From: marlonpassos Date: Sun, 15 Sep 2024 04:45:57 +0000 Subject: [PATCH 05/19] 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 493eb644..e2cbab5e 100644 --- a/docs/async/reduce.mdx +++ b/docs/async/reduce.mdx @@ -12,6 +12,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, @@ -23,5 +30,5 @@ const users = await _.reduce( } }, {}, -) +) // { 1: { name: 'person 1' }, 2: { name: 'person 2' }, 3: { name: 'person 3' }, 4: { name: 'person 4' } } ``` From 45e6f8dfd73de587801906e70b4d3d94dffd011c Mon Sep 17 00:00:00 2001 From: marlonpassos Date: Sun, 15 Sep 2024 04:57:09 +0000 Subject: [PATCH 06/19] 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 95a272f7..0659e4bf 100644 --- a/docs/async/retry.mdx +++ b/docs/async/retry.mdx @@ -15,10 +15,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 32045e9d6aca053c0dd2fe814a560b3191e226b4 Mon Sep 17 00:00:00 2001 From: marlonpassos Date: Mon, 16 Sep 2024 16:11:58 +0000 Subject: [PATCH 07/19] 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 1aa68aeb..d2e5043c 100644 --- a/docs/async/tryit.mdx +++ b/docs/async/tryit.mdx @@ -13,7 +13,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 9a27cf97b640118a3ded212d4cdeccd337afec1d Mon Sep 17 00:00:00 2001 From: marlonpassos Date: Mon, 16 Sep 2024 16:13:21 +0000 Subject: [PATCH 08/19] 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 84718a2f..67fb90e6 100644 --- a/docs/async/withResolvers.mdx +++ b/docs/async/withResolvers.mdx @@ -11,7 +11,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 04cac90800578a4756642a602f9b8570dd5a6a2e Mon Sep 17 00:00:00 2001 From: marlonpassos Date: Mon, 16 Sep 2024 16:19:21 +0000 Subject: [PATCH 09/19] 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 db2ef788..f731ec4f 100644 --- a/docs/curry/debounce.mdx +++ b/docs/curry/debounce.mdx @@ -13,14 +13,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 62c7d7a8297612337b8dcad8a8efa668f2ebfffd Mon Sep 17 00:00:00 2001 From: marlonpassos Date: Mon, 16 Sep 2024 16:20:11 +0000 Subject: [PATCH 10/19] 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 59a34eb5..536a748d 100644 --- a/docs/curry/once.mdx +++ b/docs/curry/once.mdx @@ -11,7 +11,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 8bc92d81d608b7cee174ec1c2ed647ea3742a5a8 Mon Sep 17 00:00:00 2001 From: marlonpassos Date: Mon, 16 Sep 2024 16:22:39 +0000 Subject: [PATCH 11/19] 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 19fd83af..e8670a53 100644 --- a/docs/curry/throttle.mdx +++ b/docs/curry/throttle.mdx @@ -21,17 +21,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 2221aab9bdff62b068ed26125abb62bca8f5b09f Mon Sep 17 00:00:00 2001 From: marlonpassos Date: Mon, 16 Sep 2024 16:24:10 +0000 Subject: [PATCH 12/19] 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 6c230dd5..eeb43e81 100644 --- a/docs/function/castComparator.mdx +++ b/docs/function/castComparator.mdx @@ -14,7 +14,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 9caacb8eab1ea201f281948f6221b44948f6db9d Mon Sep 17 00:00:00 2001 From: marlonpassos Date: Mon, 16 Sep 2024 16:27:30 +0000 Subject: [PATCH 13/19] 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 18278c56..7cee4c1c 100644 --- a/docs/number/clamp.mdx +++ b/docs/number/clamp.mdx @@ -24,5 +24,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 5ba8c9d91e616f1af44b2eec690ca4b71d975005 Mon Sep 17 00:00:00 2001 From: marlonpassos Date: Mon, 16 Sep 2024 16:30:08 +0000 Subject: [PATCH 14/19] 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 ac1b2763..97c917a6 100644 --- a/docs/typed/isResultErr.mdx +++ b/docs/typed/isResultErr.mdx @@ -9,9 +9,11 @@ since: 12.2.0 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 b3993daa1ec871e28cc75aa3e81143bd82780d59 Mon Sep 17 00:00:00 2001 From: marlonpassos Date: Mon, 16 Sep 2024 16:30:28 +0000 Subject: [PATCH 15/19] 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 f8e90cc9..88c420b0 100644 --- a/docs/typed/isResultOk.mdx +++ b/docs/typed/isResultOk.mdx @@ -9,9 +9,11 @@ since: 12.2.0 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! } From 436e0fb539c2a2e4f35c199368eecc31c9268f5e Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Fri, 1 Nov 2024 16:21:21 -0400 Subject: [PATCH 16/19] clean up defer docs --- docs/async/defer.mdx | 46 +++++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/docs/async/defer.mdx b/docs/async/defer.mdx index 968ae6b4..cfe15a05 100644 --- a/docs/async/defer.mdx +++ b/docs/async/defer.mdx @@ -1,32 +1,33 @@ --- title: defer -description: Run an async function with deferred functions +description: Defer work until an async function completes since: 12.1.0 --- ### Usage -The `_.defer` functions lets you run an async function, registering functions as you go -that should be deferred until the async function completes, and then executed. This is -really useful in scripts where failure up to or after a specific point will require some -cleanup. It's a bit like a `finally` block. +The `defer` function allows you to run an async function while registering cleanup functions that will execute after it completes. This is useful when you need to clean up resources regardless of whether the main function succeeds or fails, similar to a `finally` block. -A hat tip to Swift's `defer` for the inspiration. +The function passed to `defer` receives a `register` function as its argument. Use this to register cleanup work that should run after the main function finishes. -The function passed to `_.defer` is called with a single `register` function argument that -can be used to register the work you want to be called when the function completes. If your function throws an error and then a registered cleanup function throws -and error it is ignored by default. The register -function supports an optional second `options` argument that lets you configure a rethrow -strategy so that error in the cleanup function is rethrown. +By default, if a cleanup function throws an error after the main function has thrown an error, the cleanup error will be ignored. You can customize this behavior by passing `{ rethrow: true }` to the `register` function to have it rethrow cleanup errors instead. + +#### Clean up temporary files ```ts import * as _ from 'radashi' +// Placeholder functions const createBuildDir = async () => { - /* create build dir... */ + console.log('Creating build directory...') + await _.sleep(100) + console.log('Build directory created.') + return 'build' } const build = async () => { - /* build project... */ + console.log('Building project...') + await _.sleep(100) + console.log('Project built.') } await _.defer(async cleanup => { @@ -36,23 +37,36 @@ await _.defer(async cleanup => { await build() }) +``` + +#### Clean up resources in an API +```ts +// Placeholder API const api = { org: { create: async () => ({ id: 1 }), delete: async () => { - /* delete org... */ + console.log('Deleting organization...') + await _.sleep(100) + console.log('Deleted organization.') }, }, user: { create: async () => ({ id: 2 }), delete: async () => { - /* delete delete... */ + console.log('Deleting user...') + await _.sleep(100) + console.log('Deleted user.') }, }, } + +// Placeholder test function const executeTest = async () => { - /* exec text... */ + console.log('Executing test...') + await _.sleep(100) + console.log('Test complete.') } await _.defer(async register => { From 0bffa0b18c71053dd4c0d31ffee4b51ad5db0222 Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Fri, 1 Nov 2024 16:48:46 -0400 Subject: [PATCH 17/19] refine guard docs --- docs/async/guard.mdx | 56 ++++++++++++++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 15 deletions(-) diff --git a/docs/async/guard.mdx b/docs/async/guard.mdx index 9a162a9d..1bc5e27d 100644 --- a/docs/async/guard.mdx +++ b/docs/async/guard.mdx @@ -1,35 +1,61 @@ --- title: guard -description: Have a function return undefined if it errors out +description: Make an async function return undefined if it rejects since: 12.1.0 --- ### Usage -This lets you set a default value if an async function errors out. +The `guard` function allows you to make an async function return `undefined` if it rejects. This is useful when you want to handle errors in a functional way, such as returning a default value. ```ts -const fetchUsers = async () => { +import * as _ from 'radashi' + +const example = async () => { throw new Error() } -const users = (await _.guard(fetchUsers)) ?? [] // [] + +const result = (await _.guard(example)) ?? [] +// [] ``` -You can choose to guard only specific errors too +#### Guard only specific errors + +The 2nd argument to `guard` is an error predicate. If provided, the function will only return `undefined` if the error matches the predicate. ```ts -class InvalidUserError extends Error { - code = 'INVALID_ID' -} -const fetchUserA = async () => { - throw new InvalidUserError() +import * as _ from 'radashi' + +const DEFAULT_USER = { name: 'John Doe' } + +async function fetchUser(id: string) { + if (id === 'unknown') throw new Error('User does not exist') + if (id === 'oops') throw new ReferenceError() + return { name: 'Jim Jimmy' } } -const fetchUserB = async () => { + +const isPlainError = (err: any) => err.name === 'Error' + +const userA = + (await _.guard(() => fetchUser('unknown'), isPlainError)) ?? DEFAULT_USER +// { name: "John Doe"} + +// This one will reject. +const userB = await _.guard(() => fetchUser('oops'), isPlainError).catch(e => e) +// [object ReferenceError] +``` + +#### Synchronous guards + +The `guard` function also works with synchronous functions. + +```ts +import * as _ from 'radashi' + +function example() { throw new Error() } -const DEFAULT_USER = { name: 'John Doe' } -const isInvalidUserError = (err: any) => err.code === 'INVALID_ID' -const userA = (await _.guard(fetchUserA, isInvalidUserError)) ?? DEFAULT_USER // { name: "John Doe"} -// const userB = (await _.guard(fetchUserB, isInvalidUserError)) ?? DEFAULT_USER // this throw +const result = _.guard(example) ?? [] +// [] ``` From 70088f7212abc3bcdabe6e864916c3dc7e616504 Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Fri, 1 Nov 2024 16:51:34 -0400 Subject: [PATCH 18/19] refine map description --- docs/async/map.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/async/map.mdx b/docs/async/map.mdx index 66a3d400..c6e57bdd 100644 --- a/docs/async/map.mdx +++ b/docs/async/map.mdx @@ -6,7 +6,7 @@ since: 12.1.0 ### Usage -A map that handles callback functions that return a promise. +The `map` function is like `Array.prototype.map`, but it works with async functions. Only one item is processed at a time. ```ts const userIds = [1, 2, 3, 4] From 2e324fe74bca07b0ba1021a6d6f39d78ef91e613 Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Fri, 1 Nov 2024 16:53:47 -0400 Subject: [PATCH 19/19] split invalid case into its own example --- docs/number/clamp.mdx | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/docs/number/clamp.mdx b/docs/number/clamp.mdx index 7cee4c1c..adfb2308 100644 --- a/docs/number/clamp.mdx +++ b/docs/number/clamp.mdx @@ -19,14 +19,15 @@ range. ```ts import * as _ from 'radashi' -_.clamp(5, 1, 10) // returns 5 -_.clamp(0, 1, 10) // returns 1 -_.clamp(15, 1, 10) // returns 10 - -// Invalid range -try { - _.clamp(1, 10, 1) -} catch (e) { - console.log(`error: ${e.message}`) // error: Invalid range -} +_.clamp(5, 1, 10) // 5 +_.clamp(0, 1, 10) // 1 +_.clamp(15, 1, 10) // 10 +``` + +#### Invalid ranges + +If the minimum is greater than the maximum, an error is thrown. + +```ts +_.clamp(1, 10, 1) // throws Error ```