### Iterate with Radash _.range Generator Source: https://github.com/sodiray/radash/blob/master/docs/getting-started.mdx The `_.range` function returns a generator for iterating, eliminating the need for traditional `for (let i)` loops. It provides a more modern and readable way to generate sequences of numbers, supporting custom start, end, and step values. ```TypeScript for (const i of _.range(0, 4)) { console.log(i) // 0, 1, 2, 3, 4 } for (const i of _.range(10, 20, 2)) { console.log(i) // 10, 12, 14, 16, 18, 20 } ``` -------------------------------- ### Register Cleanup Functions with Radash _.defer Source: https://github.com/sodiray/radash/blob/master/docs/getting-started.mdx The `_.defer` function allows registering cleanup functions to run after an async operation, similar to a `try/finally` block but with the flexibility to register cleanup at specific points. It's useful for managing resources like file handles or updating API statuses based on success or failure. ```TypeScript await _.defer(async (defer) => { await api.builds.updateStatus('in-progress') defer((err) => { api.builds.updateStatus(err ? 'failed' : 'success') }) fs.mkdir('build') defer(() => { fs.unlink('build') }) await build() }) ``` -------------------------------- ### Map and Filter with Radash _.select Source: https://github.com/sodiray/radash/blob/master/docs/getting-started.mdx The `_.select` function combines mapping and filtering into a single iteration, optimizing performance by avoiding separate `map` and `filter` operations or complex `reduce` implementations. It takes a list, a mapper function, and a filter function, processing them efficiently in one pass. ```TypeScript const superPoweredGodsFromEgypt = _.select( gods, g => ({ ...g, power: g.power * g.power }), g => g.culture === 'egypt' ) ``` -------------------------------- ### Convert List to Object with Radash _.objectify Source: https://github.com/sodiray/radash/blob/master/docs/getting-started.mdx The `_.objectify` function simplifies converting a list into an object in a single step, avoiding the need for multi-step processes or manual `reduce` implementations. It takes a list and functions to determine the key and value for each item in the resulting object. ```TypeScript const godsByCulture = _.objectify(gods, g => g.name, g => g.culture) ``` -------------------------------- ### Handle Errors with Radash _.try Source: https://github.com/sodiray/radash/blob/master/docs/getting-started.mdx The `_.try` function abstracts the logical fork of a try/catch block, providing an error-first callback response. It allows for cleaner error handling in asynchronous operations by returning an array containing either an error or the successful response. ```TypeScript const [err, response] = await _.try(api.gods.create)({ name: 'Ra' }) if (err) { throw new Error('Your god is weak and could not be created') } ``` -------------------------------- ### Install radash in Node.js projects Source: https://github.com/sodiray/radash/blob/master/docs/installation.mdx Instructions for installing the radash utility library using common Node.js package managers. This includes commands for both npm and Yarn. ```Shell npm install radash ``` ```Shell yarn add radash ``` -------------------------------- ### Generate List by Start and End with Radash list(start, end) Source: https://github.com/sodiray/radash/blob/master/docs/array/list.mdx Shows how to use the `list` function with two arguments, interpreted as `start` and `end`. It creates a list of numbers ranging from the start value to the end value, inclusive. ```ts list(2, 6) // [2, 3, 4, 5, 6] ``` -------------------------------- ### Install Radash Library Source: https://github.com/sodiray/radash/blob/master/README.md Provides the command-line instruction to add the Radash library to a project using the Yarn package manager. ```bash yarn add radash ``` -------------------------------- ### Basic Usage of `get` Function in TypeScript Source: https://github.com/sodiray/radash/blob/master/docs/object/get.mdx Demonstrates how to use the `get` function to retrieve values from a nested object. It illustrates accessing elements using both array bracket notation and dot notation for paths, and shows how to provide a fallback default value when the specified path does not exist in the object. ```TypeScript import { get } from 'radash'\n\nconst fish = {\n name: 'Bass',\n weight: 8,\n sizes: [\n {\n maturity: 'adult',\n range: [7, 18],\n unit: 'inches'\n }\n ]\n}\n\nget( fish, 'sizes[0].range[1]' ) // 18\nget( fish, 'sizes.0.range.1' ) // 18\nget( fish, 'foo', 'default' ) // 'default' ``` -------------------------------- ### Decomposed Function Composition Example Source: https://github.com/sodiray/radash/blob/master/docs/curry/compose.mdx Illustrates the underlying mechanism of function composition by explicitly nesting function calls. This example is functionally equivalent to the `compose` usage, providing a clearer visual representation of how functions are chained and executed sequentially. ```typescript const decomposed = ( useZero( objectize( increment( increment( returnArg('num')))))) decomposed() // => 2 ``` -------------------------------- ### Flattening a deep object using Radash keys, get, and objectify Source: https://github.com/sodiray/radash/blob/master/docs/object/keys.mdx This example illustrates a more advanced use case where `keys` is combined with `get` and `objectify` from the Radash library to flatten a complex, deeply nested object. It transforms the original object into a new object where keys are the dot-separated paths to the original values, effectively creating a flat representation. ```typescript import { keys, get, objectify } from 'radash' objectify( keys(ra), key => key, key => get(ra, key) ) // => { // 'name': 'ra' // 'power': 100 // 'friend.name': 'loki' // 'friend.power': 80 // 'enemies.0.name': 'hathor' // 'enemies.0.power': 12 // } ``` -------------------------------- ### Basic usage of pascal function in TypeScript Source: https://github.com/sodiray/radash/blob/master/docs/string/pascal.mdx Demonstrates how to use the `pascal` function from the `radash` library to convert various strings into PascalCase. It shows examples with single and multiple word strings. ```ts import { pascal } from 'radash' pascal('hello world') // => 'HelloWorld' pascal('va va boom') // => 'VaVaBoom' ``` -------------------------------- ### Get First Item from Array (TypeScript) Source: https://github.com/sodiray/radash/blob/master/docs/array/first.mdx Demonstrates how to use the `first` utility from `radash` to retrieve the first element of an array, or a specified default value if the array is empty. It shows examples for both non-empty and empty arrays. ```ts import { first } from 'radash' const gods = ['ra', 'loki', 'zeus'] first(gods) // => 'ra' first([], 'vishnu') // => 'vishnu' ``` -------------------------------- ### Radash Core Utility Functions Usage Examples Source: https://github.com/sodiray/radash/blob/master/README.md Illustrates common usage patterns for several Radash utility functions, including `max`, `sum`, `fork`, `sort`, `boil` for array manipulation, `objectify` for transforming arrays to objects, `get` for deep property access, `try` for robust error handling with async operations, and `map` for asynchronous array iteration. ```typescript import * as _ from 'radash' const gods = [{ name: 'Ra', power: 'sun', rank: 100, culture: 'egypt' }, { name: 'Loki', power: 'tricks', rank: 72, culture: 'norse' }, { name: 'Zeus', power: 'lightning', rank: 96, culture: 'greek' }] _.max(gods, g => g.rank) // => ra _.sum(gods, g => g.rank) // => 268 _.fork(gods, g => g.culture === 'norse') // => [[loki], [ra, zeus]] _.sort(gods, g => g.rank) // => [ra, zeus, loki] _.boil(gods, (a, b) => a.rank > b.rank ? a : b) // => ra _.objectify( gods, g => g.name.toLowerCase(), g => _.pick(g, ['power', 'rank', 'culture']) ) // => { ra, zeus, loki } const godName = _.get(gods, g => g[0].name) const [err, god] = await _.try(api.gods.findByName)(godName) const allGods = await _.map(gods, async ({ name }) => { return api.gods.findByName(name) }) ``` -------------------------------- ### Convert String to Dash Case with Radash Source: https://github.com/sodiray/radash/blob/master/docs/string/dash.mdx Demonstrates the basic usage of the `dash` function from the `radash` library to transform a string into dash-case format. The example shows how to import and apply the function to a sample string. ```typescript import { dash } from 'radash' dash('green fish blue fish') // => green-fish-blue-fish ``` -------------------------------- ### Generate Number Sequences with Radash list (Basic Usage) Source: https://github.com/sodiray/radash/blob/master/docs/array/list.mdx Demonstrates various ways to use the `list` function from `radash` to generate arrays. Examples include creating sequences with default values, specific values, values derived from an index function, and with a custom step size. ```ts import { list } from 'radash' list(3) // [0, 1, 2, 3] list(0, 3) // [0, 1, 2, 3] 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, 6, i => i, 2) // [0, 2, 4, 6] ``` -------------------------------- ### Combine Arrays into Object with zipToObject (TypeScript) Source: https://github.com/sodiray/radash/blob/master/docs/array/zip-to-object.mdx Demonstrates how to use the `zipToObject` function from the `radash` library to create an object. It shows examples of mapping two arrays, using a custom mapping function, and assigning null values. ```TypeScript import { zipToObject } from 'radash' const names = ['ra', 'zeus', 'loki'] const cultures = ['egypt', 'greek', 'norse'] zipToObject(names, cultures) // => { ra: egypt, zeus: greek, loki: norse } zipToObject(names, (k, i) => k + i) // => { ra: ra0, zeus: zeus1, loki: loki2 } zipToObject(names, null) // => { ra: null, zeus: null, loki: null } ``` -------------------------------- ### Generate List with Custom Step Size with Radash list(start, end, value, step) Source: https://github.com/sodiray/radash/blob/master/docs/array/list.mdx Explains the `list` function's fourth argument, `step`, which defines the increment between values. This allows generating lists with specific intervals from `start` to `end`. ```ts list(2, 4, i => i, 2) // [2, 4] list(25, 100, i => i, 25) // [25, 50, 75, 100] ``` -------------------------------- ### Generate List with Custom Value or Function with Radash list(start, end, value) Source: https://github.com/sodiray/radash/blob/master/docs/array/list.mdx Demonstrates the `list` function with a third argument, which can be a fixed `value` or a function. If a function, it's called for each item with its index to determine the value. ```ts list(2, 4, {}) // [{}, {}, {}] list(2, 4, null) // [null, null, null] list(2, 4, (i) => i) // [2, 3, 4] ``` -------------------------------- ### Radash Range Function: Start and End Arguments Source: https://github.com/sodiray/radash/blob/master/docs/array/range.mdx Shows the `range` function's behavior when given two arguments. These arguments are interpreted as the `start` and `end` of the sequence, and the function yields values from `start` up to `end` (inclusive). ```ts range(2, 6) // yields 2, 3, 4, 5, 6 ``` -------------------------------- ### Basic usage of isFloat in TypeScript Source: https://github.com/sodiray/radash/blob/master/docs/typed/is-float.mdx Demonstrates how to use the `isFloat` function to check if various values are floating-point numbers. It shows examples with a float, an integer, and a string, illustrating the expected boolean output for each. ```ts import { isFloat } from 'radash' isFloat(12.233) // => true isFloat(12) // => false isFloat('hello') // => false ``` -------------------------------- ### Basic Usage of isPromise in TypeScript Source: https://github.com/sodiray/radash/blob/master/docs/typed/is-promise.mdx This snippet demonstrates how to use the `isPromise` function from the `radash` library to check different types of values. It shows examples with a string, an array, and an actual Promise object, illustrating the boolean output for each case. ```TypeScript import { isPromise } from 'radash' isPromise('hello') // => false isPromise(['hello']) // => false isPromise(new Promise(res => res())) // => true ``` -------------------------------- ### Transforming Object Keys with mapKeys in TypeScript Source: https://github.com/sodiray/radash/blob/master/docs/object/map-keys.mdx This example showcases the `mapKeys` function from the 'radash' library. It demonstrates mapping object keys to uppercase and mapping keys to their corresponding values, illustrating how the callback function receives both the key and value for each entry to produce a new object with transformed keys. ```TypeScript import { mapKeys } from 'radash' const ra = { mode: 'god', power: 'sun' } mapKeys(ra, key => key.toUpperCase()) // => { MODE, POWER } mapKeys(ra, (key, value) => value) // => { god: 'god', power: 'power' } ``` -------------------------------- ### Basic Usage of `upperize` Function (TypeScript) Source: https://github.com/sodiray/radash/blob/master/docs/object/upperize.mdx This example demonstrates how to use the `upperize` function from the `radash` library. It takes an object as input and transforms all its keys to uppercase, illustrating a common object manipulation pattern. The `upperize` function is a convenient shortcut for `_.mapKeys(obj, k => k.toUpperCase())`. ```typescript import { upperize } from 'radash' const ra = { Mode: 'god', Power: 'sun' } upperize(ra) // => { MODE, POWER } ``` -------------------------------- ### Template a String with Data in TypeScript Source: https://github.com/sodiray/radash/blob/master/docs/string/template.mdx Demonstrates how to use the `template` function from the `radash` library to replace placeholders in a string with values from a data object. It shows examples using both the default `{{...}}` delimiters and a custom regular expression for ` <...> ` delimiters. ```TypeScript import { template } from 'radash' template('It is {{color}}', { color: 'blue' }) // => It is blue template('It is ', { color: 'blue' }, /<(.+?)>/g) // => It is blue ``` -------------------------------- ### Basic usage of isString Source: https://github.com/sodiray/radash/blob/master/docs/typed/is-string.mdx Pass in a value and get a boolean indicating if the value is a string. ```ts import { isString } from 'radash' isString('hello') // => true isString(['hello']) // => false ``` -------------------------------- ### Reconstruct Object from Flattened Key Paths in TypeScript Source: https://github.com/sodiray/radash/blob/master/docs/object/construct.mdx This example demonstrates the basic usage of the `construct` function from the `radash` library. It takes a flattened object, where keys like 'friend.name' or 'enemies.0.name' represent nested paths, and reconstructs it into its original hierarchical object structure. The snippet shows both the input flattened object and the expected output nested object. ```ts import { construct } from 'radash' const flat = { name: 'ra', power: 100, 'friend.name': 'loki', 'friend.power': 80, 'enemies.0.name': 'hathor', 'enemies.0.power': 12 } construct(flat) // { // name: 'ra', // power: 100, // friend: { // name: 'loki', // power: 80 // }, // enemies: [ // { // name: 'hathor', // power: 12 // } // ] // } ``` -------------------------------- ### Radash Range Function: Step Size Argument Source: https://github.com/sodiray/radash/blob/master/docs/array/range.mdx Demonstrates the `range` function with a fourth argument, `step`. This argument controls the increment between yielded values, allowing the generation of sequences that skip values from `start` to `end`. ```ts range(2, 4, i => i, 2) // yields 2, 4 range(25, 100, i => i, 25) // yields 25, 50, 75, 100 ``` -------------------------------- ### Basic Array Splitting with `fork` in TypeScript Source: https://github.com/sodiray/radash/blob/master/docs/array/fork.mdx This example illustrates the fundamental usage of the `fork` function from the `radash` library. It takes an array of objects and a predicate function, then returns two arrays: one with elements that satisfy the condition (e.g., power greater than 90) and another with elements that do not. ```TypeScript import { fork } from 'radash' const gods = [ { name: 'Ra', power: 100 }, { name: 'Zeus', power: 98 }, { name: 'Loki', power: 72 }, { name: 'Vishnu', power: 100 } ] const [finalGods, lesserGods] = fork(gods, f => f.power > 90) // [[ra, vishnu, zues], [loki]] ``` -------------------------------- ### Basic Usage of `_.defer` for Async Cleanup Source: https://github.com/sodiray/radash/blob/master/docs/async/defer.mdx The `_.defer` function allows you to execute an asynchronous operation while registering functions that will be called for cleanup once the main async function completes. This is particularly useful for resource management, ensuring operations like file deletion or API resource cleanup occur. The `register` function (aliased as `cleanup` in examples) accepts a function to be deferred and an optional `options` object, which can include `rethrow: true` to propagate errors from cleanup functions. ```TypeScript import { defer } from 'radash' await defer(async (cleanup) => { const buildDir = await createBuildDir() cleanup(() => fs.unlink(buildDir)) await build() }) 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 }) await executeTest(org, user) }) ``` -------------------------------- ### Get Smallest Item from Array using Radash `min` Source: https://github.com/sodiray/radash/blob/master/docs/array/min.mdx Demonstrates how to use the `min` function from the Radash library to find the object with the smallest 'weight' property in an array of fish objects. It takes an array and an accessor function as arguments to determine the value for comparison. ```TypeScript import { min } from 'radash' const fish = [ { name: 'Marlin', weight: 105, source: 'ocean' }, { name: 'Bass', weight: 8, source: 'lake' }, { name: 'Trout', weight: 13, source: 'lake' } ] min(fish, f => f.weight) // => {name: "Bass", weight: 8, source: "lake"} ``` -------------------------------- ### Basic Usage of Boil Function in TypeScript Source: https://github.com/sodiray/radash/blob/master/docs/array/boil.mdx Demonstrates how to use the `boil` function from the `radash` library to reduce an array of objects to a single item based on a custom comparison logic. This example finds the 'god' with the highest 'power' attribute. ```ts import { boil } from 'radash' const gods = [ { name: 'Ra', power: 100 }, { name: 'Zeus', power: 98 }, { name: 'Loki', power: 72 } ] boil(gods, (a, b) => (a.power > b.power ? a : b)) // => { name: 'Ra', power: 100 } ``` -------------------------------- ### Basic Usage of Radash `retry` Function with TypeScript Source: https://github.com/sodiray/radash/blob/master/docs/async/retry.mdx Demonstrates how to use the `_.retry` function to execute an async function with various retry configurations. It shows examples with default retries, custom retry counts, fixed delays between retries, and exponential backoff using a function. ```typescript import { retry } from 'radash' await retry({}, api.users.list) await retry({ times: 10 }, api.users.list) await retry({ times: 2, delay: 1000 }, api.users.list) // exponential backoff await retry({ backoff: i => 10**i }, api.users.list) ``` -------------------------------- ### Extracting all deep keys from an object using Radash keys Source: https://github.com/sodiray/radash/blob/master/docs/object/keys.mdx This example demonstrates the basic usage of the `keys` function from the Radash library. It takes a deeply nested JavaScript object and returns a flat array containing all keys, including those from nested objects and array elements, represented with dot notation for paths. ```typescript import { keys } from 'radash' const ra = { name: 'ra', power: 100, friend: { name: 'loki', power: 80 }, enemies: [ { name: 'hathor', power: 12 } ] } keys(ra) // => [ // 'name', // 'power', // 'friend.name', // 'friend.power', // 'enemies.0.name', // 'enemies.0.power' // ] ``` -------------------------------- ### Cluster Array Elements into Sub-Arrays (TypeScript) Source: https://github.com/sodiray/radash/blob/master/docs/array/cluster.mdx Demonstrates how to use the `cluster` function from the `radash` library to split an array into smaller arrays of a specified size. It shows an example of clustering a list of names into groups of three, illustrating the function's input and expected output. ```TypeScript import { cluster } from 'radash' const gods = ['Ra', 'Zeus', 'Loki', 'Vishnu', 'Icarus', 'Osiris', 'Thor', 'Apollo', 'Artemis', 'Athena'] cluster(gods, 3) // => [ // [ 'Ra', 'Zeus', 'Loki' ], // [ 'Vishnu', 'Icarus', 'Osiris' ], // ['Thor', 'Apollo', 'Artemis'], // ['Athena'] // ] ``` -------------------------------- ### Generate Unique ID with Radash Source: https://github.com/sodiray/radash/blob/master/docs/random/uid.mdx Demonstrates how to use the `uid` function from the `radash` library to generate unique string identifiers. It shows examples of specifying the desired length and optionally including a special character. The function is noted to be optimized for simplicity rather than performance or security. ```ts import { uid } from 'radash' uid(7) // => UaOKdlW uid(20, '*') // => dyJdbC*NsEgcnGjTHS ``` -------------------------------- ### Delay Execution with Radash sleep Function Source: https://github.com/sodiray/radash/blob/master/docs/async/sleep.mdx This snippet demonstrates the basic usage of the `sleep` function from the Radash library. It asynchronously pauses the execution for a specified number of milliseconds. The example shows how to wait for 2 seconds using `await sleep(2000)`. ```TypeScript import { sleep } from 'radash' await sleep(2000) // => waits 2 seconds ``` -------------------------------- ### Basic Usage of Radash `title` Function in TypeScript Source: https://github.com/sodiray/radash/blob/master/docs/string/title.mdx This snippet demonstrates the fundamental usage of the `title` function from the Radash library. It takes a string as input and converts it into title case, capitalizing the first letter of each word. Examples include converting various string formats like 'hello world', 'va_va_boom', 'root-hook', and 'queryItems' into their respective title-cased forms. ```TypeScript import { title } from 'radash' title('hello world') // => 'Hello World' title('va_va_boom') // => 'Va Va Boom' title('root-hook') // => 'Root Hook' title('queryItems') // => 'Query Items' ``` -------------------------------- ### Create Ordered Series from Enum/Union Type (TypeScript) Source: https://github.com/sodiray/radash/blob/master/docs/series/series.mdx Demonstrates the basic usage of the `series` function to create an ordered collection from a simple union type (`Weekday`). It shows various operations like finding min/max, navigating next/previous elements, getting first/last, and spinning through the series, including handling wrap-around. ```ts import { series } from 'radash' type Weekday = 'monday' | 'tuesday' | 'wednesday' | 'thursday' | 'friday' const weekdays = series([ 'monday', 'tuesday', 'wednesday', 'thursday', 'friday' ]) weekdays.min('tuesday', 'thursday') // => 'tuesday' weekdays.max('wednesday', 'monday') // => 'wednesday' weekdays.next('wednesday') // => 'thursday' weekdays.previous('tuesday') // => 'monday' weekdays.first() // => 'monday' weekdays.last() // => 'friday' weekdays.next('friday') // => null weekdays.next('friday', weekdays.first()) // => 'monday' weekdays.spin('monday', 3) // => 'thursday' ``` -------------------------------- ### Combine Arrays with Radash zip in TypeScript Source: https://github.com/sodiray/radash/blob/master/docs/array/zip.mdx Demonstrates the basic usage of the `zip` function from the `radash` library. This function takes multiple arrays as arguments and returns a new array where elements at the same index from the input arrays are grouped together into sub-arrays. The example shows how to combine arrays of names and cultures. ```TypeScript import { zip } from 'radash' const names = ['ra', 'zeus', 'loki'] const cultures = ['egypt', 'greek', 'norse'] zip(names, cultures) // => [ // [ra, egypt] // [zeus, greek] // [loki, norse] // ] ``` -------------------------------- ### Basic Usage of radash isObject Function Source: https://github.com/sodiray/radash/blob/master/docs/typed/is-object.mdx Demonstrates how to use the `isObject` function from the `radash` library. It takes a value as input and returns a boolean indicating whether the value is a plain JavaScript object. Examples include various data types like strings, arrays, null, and objects to illustrate its behavior. ```ts import { isObject } from 'radash' isObject('hello') // => false isObject(['hello']) // => false isObject(null) // => false isObject({ say: 'hello' }) // => true ``` -------------------------------- ### Shift Array Elements Right in TypeScript Source: https://github.com/sodiray/radash/blob/master/docs/array/shift.mdx Demonstrates how to use the `shift` function from the `radash` library to cyclically shift array elements to the right by a given number of positions. The example shows shifting an array of numbers by 3 positions, resulting in the last elements moving to the beginning. ```ts import { shift } from 'radash' const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9] shift(arr, 3) // => [7, 8, 9, 1, 2, 3, 4, 5, 6] ``` -------------------------------- ### Count occurrences of items in an array using radash.counting Source: https://github.com/sodiray/radash/blob/master/docs/array/counting.mdx Demonstrates how to use the `counting` function from the `radash` library to count occurrences of items in an array based on a callback function that extracts an identifier. The example counts gods by their culture, showing how to transform an array of objects into an object mapping unique identifiers to their counts. ```ts import { counting } from 'radash' const gods = [ { name: 'Ra', culture: 'egypt' }, { name: 'Zeus', culture: 'greek' }, { name: 'Loki', culture: 'greek' } ] counting(gods, g => g.culture) // => { egypt: 1, greek: 2 } ``` -------------------------------- ### Basic Usage of invert function in TypeScript Source: https://github.com/sodiray/radash/blob/master/docs/object/invert.mdx This snippet illustrates the basic usage of the `invert` function from the 'radash' library. It takes an object, `powersByGod`, and returns a new object where the keys and values are swapped, effectively mapping values back to their original keys. The example shows how 'sun' becomes a key mapped to 'ra', 'tricks' to 'loki', and 'lightning' to 'zeus'. ```TypeScript import { invert } from 'radash' const powersByGod = { ra: 'sun', loki: 'tricks', zeus: 'lighning' } invert(gods) // => { sun: ra, tricks: loki, lightning: zeus } ``` -------------------------------- ### Applying Radash `chain` for Object Property Processing Source: https://github.com/sodiray/radash/blob/master/docs/curry/chain.mdx This example showcases a more practical application of `chain` for transforming properties of objects within an array. It defines functions to extract a property and then transform its value, demonstrating how `chain` can be used to create a reusable pipeline for data manipulation, such as mapping over an array of objects. ```typescript import { chain } from 'radash' type Deity = { name: string rank: number } const gods: Deity[] = [ { rank: 8, name: 'Ra' }, { rank: 7, name: 'Zeus' }, { rank: 9, name: 'Loki' } ] const getName = (god: Deity) => item.name const upperCase = (text: string) => text.toUpperCase() as Uppercase const getUpperName = chain( getName, upperCase ) getUpperName(gods[0]) // => 'RA' gods.map(getUpperName) // => ['RA', 'ZEUS', 'LOKI'] ``` -------------------------------- ### Replace or Append Item in Array (TypeScript) Source: https://github.com/sodiray/radash/blob/master/docs/array/replace-or-append.mdx Demonstrates how to use the `replaceOrAppend` function from the `radash` library. It takes an array, an item to insert/update, and an identity function to find a match. If a match is found, the existing item is replaced; otherwise, the new item is appended. Examples show both replacement and appending scenarios. ```TypeScript import { replaceOrAppend } from 'radash' const fish = [ { name: 'Marlin', weight: 105 }, { name: 'Salmon', weight: 19 }, { name: 'Trout', weight: 13 } ] const salmon = { name: 'Salmon', weight: 22 } const sockeye = { name: 'Sockeye', weight: 8 } replaceOrAppend(fish, salmon, f => f.name === 'Salmon') // => [marlin, salmon (weight:22), trout] replaceOrAppend(fish, sockeye, f => f.name === 'Sockeye') // => [marlin, salmon, trout, sockeye] ``` -------------------------------- ### Basic usage of pick function Source: https://github.com/sodiray/radash/blob/master/docs/object/pick.mdx Demonstrates how to use the `pick` function to create a new object containing only specified keys from an original object. It takes an object and an array of keys as input, returning a new object with only those properties. ```ts import { pick } from 'radash' const fish = { name: 'Bass', weight: 8, source: 'lake', barckish: false } pick(fish, ['name', 'source']) // => { name, source } ``` -------------------------------- ### Create Partial Function with Radash Source: https://github.com/sodiray/radash/blob/master/docs/curry/partial.mdx Demonstrates how to use the `partial` function from the `radash` library to create a new function with some arguments pre-filled. It shows a basic addition function being partially applied. ```TypeScript import { partial } from 'radash' const add = (a: number, b: number) => a + b const addFive = partial(add, 5) addFive(2) // => 7 ``` -------------------------------- ### Currying tryit for Reusable Error-First Functions Source: https://github.com/sodiray/radash/blob/master/docs/async/tryit.mdx Shows how `tryit` can be curried, allowing you to pre-apply the `tryit` wrapper to a function. This creates a new, reusable error-first version of the original function, which can then be called with its arguments. ```ts import { tryit } from 'radash' const findUser = tryit(api.users.find) const [err, user] = await findUser(userId) ``` -------------------------------- ### Basic Usage of Radash compose Function Source: https://github.com/sodiray/radash/blob/master/docs/curry/compose.mdx Demonstrates the fundamental application of the `compose` function from the `radash` library. It shows how to define a series of functions and then compose them into a single callable entity, where each function receives and calls the next function in the chain. ```typescript import { compose } from 'radash' const useZero = (fn: any) => () => fn(0) const objectize = (fn: any) => (num: any) => fn({ num }) const increment = (fn: any) => ({ num }: any) => fn({ num: num + 1 }) const returnArg = (arg: any) => (args: any) => args[arg] const composed = compose( useZero, objectize, increment, increment, returnArg('num') ) composed() // => 2 ``` -------------------------------- ### Basic Usage of Radash Range Function Source: https://github.com/sodiray/radash/blob/master/docs/array/range.mdx Demonstrates various ways to use the `range` function from `radash`, including single argument (size), start/end, custom value generation (static or function-based), and step size. Also illustrates iterating over the generated sequence using a `for...of` loop. ```ts import { range } from 'radash' range(3) // yields 0, 1, 2, 3 range(0, 3) // yields 0, 1, 2, 3 range(0, 3, 'y') // yields y, y, y, y range(0, 3, () => 'y') // yields y, y, y, y range(0, 3, i => i) // yields 0, 1, 2, 3 range(0, 3, i => `y${i}`) // yields y0, y1, y2, y3 range(0, 3, obj) // yields obj, obj, obj, obj range(0, 6, i => i, 2) // yields 0, 2, 4, 6 for (const i of range(0, 200, 10)) { console.log(i) // => 0, 10, 20, 30 ... 190, 200 } for (const i of range(0, 5)) { console.log(i) // => 0, 1, 2, 3, 4, 5 } ``` -------------------------------- ### Basic usage of radash.proxied Source: https://github.com/sodiray/radash/blob/master/docs/curry/proxied.mdx Demonstrates how to use the `radash.proxied` function to create a dynamic object. Property access and method calls on the created object are handled by a custom function, allowing for flexible and dynamic behavior. ```ts import { proxied } from 'radash' type Property = 'name' | 'size' | 'getLocation' const person = proxied((prop: Property) => { switch (prop) { case 'name': return 'Joe' case 'size': return 20 case 'getLocation' return () => 'here' } }) person.name // => Joe person.size // => 20 person.getLocation() // => here ``` -------------------------------- ### Get Last Item from Array (TypeScript) Source: https://github.com/sodiray/radash/blob/master/docs/array/last.mdx Demonstrates how to retrieve the last element from an array using `radash.last`, including handling empty arrays with a default value. ```typescript import { last } from 'radash' const fish = ['marlin', 'bass', 'trout'] const lastFish = last(fish) // => 'trout' const lastItem = last([], 'bass') // => 'bass' ``` -------------------------------- ### Get Random Item from Array (TypeScript) Source: https://github.com/sodiray/radash/blob/master/docs/random/draw.mdx Demonstrates the basic usage of the 'draw' function from the 'radash' library to retrieve a single random element from a TypeScript array. ```typescript import { draw } from 'radash' const fish = ['marlin', 'bass', 'trout'] draw(fish) // => a random fish ``` -------------------------------- ### Check if a value is an Array using radash.isArray Source: https://github.com/sodiray/radash/blob/master/docs/typed/is-array.mdx This snippet demonstrates how to use the `isArray` function from the `radash` library to determine if a given value is an array. It shows examples with a string and an array. ```TypeScript import { isArray } from 'radash' isArray('hello') // => false isArray(['hello']) // => true ``` -------------------------------- ### Basic Usage of Radash select for Filtering and Mapping Source: https://github.com/sodiray/radash/blob/master/docs/array/select.mdx Demonstrates the fundamental application of the `select` function from the `radash` library. It shows how to filter an array of objects based on a predicate function and simultaneously map the filtered elements to a desired property, all within a single iteration. ```ts import { select } from 'radash' const fish = [ { name: 'Marlin', weight: 105, source: 'ocean' }, { name: 'Bass', weight: 8, source: 'lake' }, { name: 'Trout', weight: 13, source: 'lake' } ] select( fish, f => f.weight, f => f.source === 'lake' ) // => [8, 13] ``` -------------------------------- ### Basic Usage of Radash `chain` for Numeric Transformations Source: https://github.com/sodiray/radash/blob/master/docs/curry/chain.mdx This snippet illustrates the fundamental concept of `chain` by composing simple arithmetic functions. It demonstrates how `chain` takes multiple functions as arguments and returns a new function that executes them sequentially, passing the result of each function to the next, ultimately returning the final computed value. ```typescript import { chain } from 'radash' const add = (y: number) => (x: number) => x + y const mult = (y: number) => (x: number) => x * y const addFive = add(5) const double = mult(2) const chained = chain(addFive, double) chained(0) // => 10 chained(7) // => 24 ```