@0x26e/utils
v1.0.5
Published
A TypeScript Utility Collection Similar to Laravel Helpers
Downloads
7
Readme
@0x26e/utils
A TypeScript Utility Collection Similar to Laravel Helpers.
Instalation
npm install @0x26e/utils
Available Methods
Arrays & Objects
Miscellaneous
blank sleep usleep tap until clamp
Arrays & Objects
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.
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:
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'
Miscellaneous
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
await sleep(2); // Pauses execution for 2 seconds.
usleep()
Pauses the execution for a specified number of milliseconds.
await usleep(500); // Pauses execution for 500 milliseconds.
tap()
Invokes an interceptor function with a provided value and returns the result.
const tappedValue = tap(5, (n) => n * 2); // tappedValue = 10
until()
Continuously attempts a function until a condition is met.
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 value
is less than min
, it returns min
. If the value
is greater than max
, it returns max
.
Otherwise, it returns the value
itself.
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)