A collection of utility functions and types for TypeScript projects in Node.js
npm install ts-scribebash
npm install ts-scribe
`
`bash
bun add ts-scribe
`
Functions
$3
- run: Helper to run a function or block of code in a cleaner way.
- parseBoolean: Parses various values into a boolean.
- parseNumber: Parses various values into a number.
- safeJsonParse: Safely parses a string without throwing.
- safeJsonStringify: Safely stringifies a JSON object without throwing (handles issues like circular references).
$3
- arrChunk: Splits an array into chunks of a specified size.
- arrDifference: Returns the difference between two arrays.
- arrGroupBy: Groups an array of objects by a specified key.
- arrIntersection: Returns the intersection of two arrays.
- arrIntersectionDeep: Deep intersection between arrays of objects or values.
- arrPluck: Extracts a list of property values from an array of objects.
- arrPowerset: Returns the powerset of an array.
- arrShuffle: Shuffles an array randomly.
- arrUniqueBy: Returns an array of unique values based on a specified key.
- toArray: Converts almost anything into an array.
$3
- asyncForEach: Runs async operations over an array in parallel.
- asyncMap: Similar to asyncForEach but with the behavior of Array.map function.
- debounce: Debounces a function or promise.
- maybe: Maybe monad for null-safe operations.
- retry: Retries a promise a specified number of times.
- sleep: Pauses execution for a specified time.
- waterfall: Chains functions that pass results to each other.
$3
- SortedList: Maintains a sorted list.
- WeightedList: Selects items based on weight/probability.
$3
- clamp: Clamps a number between min and max.
- greatestCommonDivisor: Finds the GCD of multiple numbers.
- smallestCommonMultiple: Finds the LCM of multiple numbers.
$3
- objectDeepClone: Deeply clones an object.
- objectDeepEquals: Deeply compares two objects or arrays.
- objectDeepFreeze: Deep freezes an object recursively.
- objectFlatten: Flattens an object into dot notation.
- objectPrune: Removes properties with undefined values.
- objectRemoveKeys: Removes specified keys from an object.
#### Deepmerge
Exposes the API of deepmerge-ts, check their documentation for more details.
$3
- randomBool: Generates a random boolean, optionally biased.
- randomInt: Generates a random integer in a given range.
- randomSample: Randomly samples values from an array.
- randomString: Generates a random string of given length/charset.
$3
- strCamelCase: Converts a string to camelCase.
- strDotCase: Converts a string to dot.case.
- strHeaderCase: Converts a string to Header Case.
- strKebabCase: Converts a string to kebab-case.
- strPascalCase: Converts a string to PascalCase.
- strSnakeCase: Converts a string to snake_case.
- strTruncate: Truncates a string with configurable options.
- strSlugify: Slugifies a string, converting it into a URL-friendly format with customizable options.
$3
- isBrowser: Returns true if running in a browser environment.
- isNode: Returns true if running in Node.js.
- getEnvironment: Returns 'Browser', 'Bun', 'Node', or 'Unknown' depending on the context it's run in.
$3
- isDefined: Checks if a value is not null/undefined/NaN.
- isEmptyObject: Checks if an object has no own properties.
- isEmptyValue: Checks for null, undefined, NaN, empty string/array/object.
- isNumber: Validates numeric values or strings that can be parsed as numbers.
- isString: Validates if a value is a string.
Types
- Primitive: JavaScript primitive types.
- Nullish: Represents null | undefined.
- NonNullish: Excludes null | undefined.
- Mandatory: Removes nullish from all properties of T.
- Nestable: A type that can nest recursively.
- GenericFunction: A generic function type.
- TypeOfString: Valid typeof operator string values.
- TypeOfType: Infers the typeof type string from a value.
- UnionToIntersection: Converts a union to an intersection.
- SmartPartial: Only makes properties optional if their type includes undefined.
- Simplify: Flattens complex types for better readability.
- OverloadUnion: Turns a union of functions into a single overloaded function type.
- DeepReadonly: Deep version of Readonly.
- DeepPartial: Deep version of Partial.
- Serializable: Represents a type that can be serialized to a JSON-compatible format.
$3
- benchmark`: Wrap around a function to benchmark it's performance without affecting app behavior.