Skip to content

Latest commit

 

History

History
159 lines (103 loc) · 3.33 KB

README.md

File metadata and controls

159 lines (103 loc) · 3.33 KB

@0x26e/utils

A TypeScript Utility Collection Similar to Laravel Helpers.

Instalation

npm install @0x26e/utils

Available Methods

Arrays & Objects

crossJoin

Miscellaneous

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 array of classes (class can be string or object) 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)