ts-scribe
v0.5.5
Published
A collection of utility functions and types for TypeScript projects in Node.js
Downloads
281
Maintainers
Readme
TS-Scribe
A TypeScript utility library tailored for Node.js and Bun development. It provides a rich set of utility functions and advanced types to enhance productivity and code quality.
Installation
npm install ts-scribebun add ts-scribeFunctions
Core
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).
Array
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.
Async
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.
List
SortedList: Maintains a sorted list.WeightedList: Selects items based on weight/probability.
Math
clamp: Clamps a number between min and max.greatestCommonDivisor: Finds the GCD of multiple numbers.smallestCommonMultiple: Finds the LCM of multiple numbers.
Object
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.
Random
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.
String
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.
System
isBrowser: Returnstrueif running in a browser environment.isNode: Returnstrueif running in Node.js.getEnvironment: Returns 'Browser', 'Bun', 'Node', or 'Unknown' depending on the context it's run in.
Typeguards
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: Representsnull | undefined.NonNullish: Excludesnull | undefined.Mandatory<T>: Removes nullish from all properties ofT.Nestable<T>: A type that can nest recursively.GenericFunction: A generic function type.TypeOfString: Validtypeofoperator string values.TypeOfType<T>: Infers thetypeoftype string from a value.UnionToIntersection<U>: Converts a union to an intersection.SmartPartial<T>: Only makes properties optional if their type includesundefined.Simplify<T>: Flattens complex types for better readability.OverloadUnion<T>: Turns a union of functions into a single overloaded function type.DeepReadonly<T>: Deep version ofReadonly<T>.DeepPartial<T>: Deep version ofPartial<T>.Serializable: Represents a type that can be serialized to a JSON-compatible format.
Development
benchmark: Wrap around a function to benchmark it's performance without affecting app behavior.
Credits
License
MIT
