> For the complete documentation index, see [llms.txt](https://docs.iynxdev.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.iynxdev.com/addon-api/getting-started/internal-api/utils.md).

# Utils

This page explains how to work with the internal Utils API. It covers the most important utility functions.

## Utils API

To fetch the utility manager, add the following snippet:

```js
const utils = this.heart.core.util.util;
```

### Functions

#### `secureEval(string)`

Securely evaluate a string by removing non-numeric characters.

```js
const equation = parseInt(eval(this.heart.core.util.util.secureEval('1 * 2 + 5')));
```

#### `ms(string)`

Convert a time string to milliseconds. The format is `12h 2m 15s 262ms`.

```js
const time = this.heart.core.util.util.ms('2m 6s');
```

#### `ms(number)`

Convert milliseconds to a time string.

```js
const time = this.heart.core.util.util.ms(60000);
```

#### `resolveTime(string)`

Resolve a time string to milliseconds. This supports a wide range of formats.

```js
const time = this.heart.core.util.util.resolveTime('1,5h');
```

#### `sleep(number)`

Sleep for a specified amount of time in milliseconds.

```js
await this.heart.core.util.util.sleep(4000);
```

#### `getRandomElement(array)`

Get a random element from an array.

```js
const random = this.heart.core.util.util.getRandomElement([1, 2, 3, 4]);
```

#### `upperCase(string)`

Convert the first letter of a string to uppercase.

```js
const value = this.heart.core.util.util.upperCase('hello');
```

#### `generateRandom(number, number, array)`

Generate a random string.

```js
const id = this.heart.core.util.util.generateRandom(7, 10, ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']);
```

#### `isNumber(string)`

Check whether a string is a number.

```js
const isNumber = this.heart.core.util.util.isNumber('2165236727247347835835845845');
```

#### `isEquation(string)`

Check whether a string is an equation.

```js
const isEquation = this.heart.core.util.util.isNumber('2 * 326 * 23623 + d + 326 - 23');
```

#### `validateHex(string)`

Validate a hex color code.

```js
const isHex = this.heart.core.util.util.validateHex('#783224');
```

#### `isPicture(string)`

Check whether a file is a picture based on its extension.

```js
const isPicture = this.heart.core.util.util.isPicture(attachment.name);
```

#### `validateURL(string)`

Validate a URL.

```js
const isURL = this.heart.core.util.util.validateURL('https://discord.gg/');
```
