A TypeScript Utility Collection Similar to Laravel Helpers
npm install @0x26e/utils@0x26e/utils
A TypeScript Utility Collection Similar to Laravel Helpers.
``bash`
npm install @0x26e/utils
#### crossJoin()
Returns the Cartesian product of the given arrays.
The Cartesian product of multiple sets is a set of all possible combinations where each combination contains one element from each input array.
`typescript`
const result = crossJoin([1, 2], ['a', 'b']);
console.log(result);
// Output: [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']]
#### toCssClasses()
The toCssClasses function conditionally compiles a CSS class string. The method accepts an object of classes where the object key contains the class or classes you wish to add, while the value is a boolean expression. If the object key has a numeric key, it will always be included in the rendered class list:
`typescript`
const isActive = false;
const hasError = true;
const classes = toCssClasses([
'p-4',
{ 'font-bold': isActive, 'bg-red': hasError },
]);
console.log(classes);
// Output: 'p-4 bg-red'
#### blank()
The blank function determines whether the given value is "blank":
blank('');
blank(' ');
blank(null);
blank(collect());
// true
blank(0);
blank(true);
blank(false);
// false
#### sleep()
Pauses the execution for a specified number of seconds
`typescript`
await sleep(2); // Pauses execution for 2 seconds.
#### usleep()
Pauses the execution for a specified number of milliseconds.
`typescript`
await usleep(500); // Pauses execution for 500 milliseconds.
#### tap()
Invokes an interceptor function with a provided value and returns the result.
`typescript`
const tappedValue = tap(5, (n) => n * 2); // tappedValue = 10
#### until()
Continuously attempts a function until a condition is met.
`typescript`
const result = await until(
() => isConditionTrue(),
() => fetchData(),
2,
);
#### clamp()
Returns a value clamped to the inclusive range of min and max. This function ensures that the value falls within the specified range. If the valuemin
is less than , it returns min. If the value is greater than max, it returns max.value
Otherwise, it returns the itself.
`typescript
const clamped1 = clamp(5, 1, 10);
console.log(clamped1); // 5
const clamped2 = clamp(15, 1, 10);
console.log(clamped2); // 10 (since 15 is greater than max 10)
const clamped3 = clamp(-5, 1, 10);
console.log(clamped3); // 1 (since -5 is less than min 1)
``