1.0.1 • Published 12 months ago

typescript-utilitys v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

Package Name

typescript-utilitys

Description

Utility and helper functions for TypeScript.

Author

Srikar Phani Kumar Marti

Version

1.0.0

License

MIT

Repository

GitHub Repository

Installation

You can install the package using npm or yarn:

npm install typescript-utilitys

or

yarn add typescript-utilitys

A few of the available utils

Async Utils

FunctionDescriptionExample
retryRetries an asynchronous operation a specified number of times until it succeeds or reaches the maximum number of retries.const result = await retry(() => fetchData(url), 3);
timeoutAdds a timeout to an asynchronous operation, ensuring it completes within a specified time limit.const result = await timeout(() => fetchData(url), 5000);
raceRaces multiple asynchronous operations and returns the result of the first one to complete.const result = await race([fetchData(url1), fetchData(url2)]);
sequenceRuns asynchronous operations sequentially, waiting for each to complete before starting the next one.const result = await sequence([() => fetchData(url1), () => fetchData(url2)]);
parallelRuns multiple asynchronous operations in parallel and returns the results in the same order.const results = await parallel([() => fetchData(url1), () => fetchData(url2)]);

Function Utils

FunctionDescriptionExample
debounceDelays the execution of a function until a specified amount of time has passed since the last call.const debouncedFunc = debounce(() => console.log('Hello'), 300);
throttleEnsures a function is only called once within a specified time period.const throttledFunc = throttle(() => console.log('Hello'), 300);
memoizeCaches the results of a function based on its inputs to improve performance.const memoizedFunc = memoize(slowFunction);

Guard Utils

FunctionDescriptionExample
isStringChecks if a value is a string.const result = isString('Hello');
isNumberChecks if a value is a number.const result = isNumber(123);
isArrayChecks if a value is an array.const result = isArray([1, 2, 3]);
isObjectChecks if a value is an object.const result = isObject({ key: 'value' });
isFunctionChecks if a value is a function.const result = isFunction(() => {});

Misc

FunctionDescriptionExample
isValidUrlChecks if the URL is valid.const isValid = isValidUrl('https://google.com');
deepCloneCreates a deep clone of an object.const clone = deepClone({ key: 'value' });
mergeMerges multiple objects into one.const merged = merge({ a: 1 }, { b: 2 });
omitOmits specified keys from an object.const result = omit({ a: 1, b: 2 }, ['a']);
pickPicks specified keys from an object.const result = pick({ a: 1, b: 2 }, ['a']);

Transformations

FunctionDescriptionExample
toCamelCaseConverts a string to camel case.const result = toCamelCase('hello_world');
toSnakeCaseConverts a string to snake case.const result = toSnakeCase('helloWorld');
toKebabCaseConverts a string to kebab case.const result = toKebabCase('helloWorld');
capitalizeCapitalizes the first letter of a string.const result = capitalize('hello');
reverseStringReverses a string.const result = reverseString('hello');
convertDateConverts a date string to a specified format.const result = convertDate('2023-01-01', 'MM/DD/YYYY');
addDaysAdds a specified number of days to a date.const result = addDays(new Date(), 5);
subtractDaysSubtracts a specified number of days from a date.const result = subtractDays(new Date(), 5);
formatDateFormats a date according to a specified format.const result = formatDate(new Date(), 'YYYY-MM-DD');

Contributing

Feel free to contribute to this project by opening issues or submitting pull requests. Please follow the existing code style and conventions.

License

This project is licensed under the MIT License - see the LICENSE file for details.