Here will be all the methods that can be used in vanilla javascript/typescript and also in react.
Capitalize the first letter of the string.
import { upperFirst } from "essentials-utils";
upperFirst("hello world"); // Hello world
Generates an array of numbers.
import { generateArray } from "essentials-utils";
generateArray(10); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Navigate to a specific route.
import { navigateHandler } from "essentials-utils";
navigateHandler("/home", false);
// Navigate to /home
navigateHandler("/home", true);
// Navigate to /home in a new tab
Fetch data from an API.
import { fetcher } from "essentials-utils";
fetcher("https://api.github.com/users/wesleyara")
.then(response => console.log(response))
.catch(error => console.log(error));
// or
const response = await fetcher("https://api.github.com/users/wesleyara");
console.log(response);
Delay the execution of a function.
import { delay } from "essentials-utils";
delay(1000).then(() => console.log("Hello world"));
// or
await delay(1000);
Reverse a string.
import { reverseString } from "essentials-utils";
reverseString("Hello world"); // dlrow olleH
Check if two arrays are equal.
import { arrayEquals } from "essentials-utils";
arrayEquals([1, 2, 3], [1, 2, 3]); // true
Convert an array to an object.
import { arrayToObject } from "essentials-utils";
arrayToObject(
[
{ id: 1, name: "a" },
{ id: 2, name: "b" },
],
"id",
); // { 1: { id: 1, name: "a" }, 2: { id: 2, name: "b" } }
Remove an item from an array.
import { removeArrayItemByValue } from "essentials-utils";
removeArrayItemByValue([1, 2, 3], 2); // [1, 3]
Remove an item from an array by index.
import { removeArrayItemByIndex } from "essentials-utils";
removeArrayItemByIndex([1, 2, 3], 1); // [1, 3]
Convert an object to an array.
import { objectToArray } from "essentials-utils";
objectToArray({ a: 1, b: 2 }); // [{ a: 1 }, { b: 2 }]
Get the current route.
import { currentRouter } from "essentials-utils";
const router = currentRouter();
console.log(router.pathname); // /home
Log a message in the console.
import { smartLog } from "essentials-utils";
const App = () => {
smartLog("Hello world", App); // [App] - DEBUG: Hello world
};
Check if an object is empty.
import { isEmptyObject } from "essentials-utils";
isEmptyObject({}); // true
random array method
import { shuffleArray } from "essentials-utils";
// called in a component
const arr1 = [1, 2, 3, 4, 5];
const arr2 = shuffleArray(arr1);
console.log("arr2", arr2);
Generate a random number.
import { generateRandomNumber } from "essentials-utils";
generateRandomNumber(1, 10); // 5
Get the percentage of a number.
import { getPercentage } from "essentials-utils";
getPercentage(10, 100); // 10
Clone an item.
import { deepClone } from "essentials-utils";
const obj = { a: 1, b: 2 };
const obj2 = deepClone(obj); // { a: 1, b: 2 }
Get the last item of an array.
import { arrayLastItem } from "essentials-utils";
arrayLastItem([1, 2, 3]); // 3
Generate an array with null values.
import { generateNullArray } from "essentials-utils";
generateNullArray(5); // [null, null, null, null, null]
Generate a random string.
import { generateRandomString } from "essentials-utils";
generateRandomString(5); // 5f2d1
Generate a random color.
import { generateRandomColor } from "essentials-utils";
generateRandomColor(); // #f2d1a5
Set a value in the local storage.
import { setStorage } from "essentials-utils";
setStorage("name", "Wesley");
Get a value from the local storage.
import { getStorage } from "essentials-utils";
getStorage("name"); // Wesley
Remove a value from the local storage.
import { removeStorage } from "essentials-utils";
removeStorage("name");
Get the current location of the navigator.
import { getNavigatorCurrentLocation } from "essentials-utils";
getNavigatorCurrentLocation().then((location) => console.log(location));
Mask a phone number.
import { phoneMask } from "essentials-utils";
phoneMask("1234567890"); // (12) 3456-7890
Mask a CPF number.
import { cpfMask } from "essentials-utils";
cpfMask("12345678900"); // 123.456.789-00
Mask a CNPJ number.
import { cnpjMask } from "essentials-utils";
cnpjMask("12345678900000"); // 12.345.678/9000-00
Mask a CEP number.
import { cepMask } from "essentials-utils";
cepMask("12345678"); // 12345-678
Log a message in the console with color.
import { colorLog } from "essentials-utils";
console.log(colorLog("Hello world", { color: "red" }));