1.2.0 • Published 1 year ago

@codevachon/utilities v1.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

@codevachon/utilities

A Library Used for managing utilities in Javascript.

Install

pnpm add @codevachon/utilities
yarn add @codevachon/utilities
npm install @codevachon/utilities

Usage

asBool

returns the provided values as a boolean value

import { asBool } from "@codevachon/utilities";

if (asBool(process.env.use_thing)) {
    console.log("I can use the thing");
}

isNil

returns the provided values is a undefined or null value

import { isNil } from "@codevachon/utilities";

if (isNil(process.env.use_thing)) {
    console.log("value is underfined or null");
}

isServerSide

check if the code is running on server side.

import { isServerSide } from "@codevachon/utilities";

if (isServerSide()) {
    console.log("run this server side code");
}

returns the value of typeof window === "undefined" && typeof document === "undefined"

slugify

turn a string into a url safe slug

import { slugify } from "@codevachon/utilities";

const mySlug = slugify("Hello World!"); // => `hello-world`

doesKeyExists

Returns if a Key exists on an Object

import { doesKeyExists } from "@codevachon/utilities";

if (doesKeyExists({ foo: "bar" }, "foo")) {
    console.log("key exists on object");
}

sortObjectArrayByKey

Returns an Array of Objects sorted by Key

import { sortObjectArrayByKey } from "@codevachon/utilities";

const sortedArray = sortObjectArrayByKey([{ a: 5 }, { a: 10 }, { a: 1 }], "a");
// sortedArray = [{a: 1},{a: 5},{a: 10}]

capitalize

Returns the string in capitalize case

import { capitalize } from "@codevachon/utilities";

const str = capitalize("banana tree");
// str = "Banana tree"

camelCase

Returns the string in camelCase case

import { camelCase } from "@codevachon/utilities";

const str = camelCase("banana tree");
// str = "bananaTree"

pascalCase

Returns the string in pascalCase case

import { pascalCase } from "@codevachon/utilities";

const str = pascalCase("banana tree");
// str = "BananaTree"

uppercase

Returns the string in upper case

import { uppercase } from "@codevachon/utilities";

const str = uppercase("banana tree");
// str = "BANANA TREE"

lowercase

Returns the string in lowercase case

import { lowercase } from "@codevachon/utilities";

const str = lowercase("banana tree");
// str = "banana tree"

startsWith

Returns the string if starts with a value

import { startsWith } from "@codevachon/utilities";

if (startsWith("banana tree", "ban")) {
    console.log("string starts with ban");
}

endsWith

Returns the string if ends with a value

import { endsWith } from "@codevachon/utilities";

if (endsWith("banana tree", "ban")) {
    console.log("string ends with ban");
}

contains

Returns the string if contains a value

import { contains } from "@codevachon/utilities";

if (contains("banana tree", "ban")) {
    console.log("string contains with ban");
}

invariant

throws an error if the condition is not truthy

import { invariant } from "@codevachon/utilities";

(request: Request, response: Response, next: NextFunction) => {
    invariant(request?.headers?.token, "Expected token to be found on headers");
    // an error is thrown if request.headers.token is not defined
};

randomInt

Generates a random integer with Math.random()

import { randomInt } from "@codevachon/utilities";

const value = randomInt(1, 10);
// value => random number from 1 to 10

randomValueFromArray

Returns a Random index from the provided Array

import { randomValueFromArray } from "@codevachon/utilities";

const value = randomValueFromArray([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
// value => random index from array

randomizeArray

Returns a new array with the indexes in a randomized order

import { randomizeArray } from "@codevachon/utilities";

const value = randomizeArray([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
// value => new array with values in random order

asArray

returns the provided value an array of values

import { asArray } from "@codevachon/utilities";

const value = asArray("a");
// value => ["a"]

range

returns an Array of numbers with with indexes from start to finish

import { range } from "@codevachon/utilities";

const value = range(1, 10);
// value => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

rangeAlpha

returns an Array of characters with with indexes from start to finish

import { rangeAlpha } from "@codevachon/utilities";

const value = rangeAlpha("a", "g");
// value => ["a", "b", "c", "d", "e", "f", "g"]