An open API service indexing awesome lists of open source software.

https://github.com/foysal50x/js-utils

A TypeScript Utility Collection Similar to Laravel Helpers
https://github.com/foysal50x/js-utils

helper-functions js js-helpers laravel-helpers ts utility-library utitlity

Last synced: about 1 month ago
JSON representation

A TypeScript Utility Collection Similar to Laravel Helpers

Awesome Lists containing this project

README

        

**@0x26e/utils**

A TypeScript Utility Collection Similar to Laravel Helpers.

## Instalation

```bash
npm install @0x26e/utils
```

## Available Methods

### Arrays & Objects

[crossJoin](#method-array-crossJoin)

[toCssClasses](#method-object-toCssClasses)

### Miscellaneous

[blank](#method-blank)
[sleep](#method-sleep)
[usleep](#method-usleep)
[tap](#method-tap)
[until](#method-until)
[clamp](#method-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.

```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 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:

```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'
```

## 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

```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 `value`
is less than `min`, it returns `min`. If the `value` is greater than `max`, it returns `max`.
Otherwise, it returns the `value` 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)
```