A Library Used for managing utilities in Javascript.
pnpm add @codevachon/utilities
yarn add @codevachon/utilities
npm install @codevachon/utilities
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");
}
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");
}
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"
turn a string into a url safe slug
import { slugify } from "@codevachon/utilities";
const mySlug = slugify("Hello World!"); // => `hello-world`
Returns if a Key
exists on an Object
import { doesKeyExists } from "@codevachon/utilities";
if (doesKeyExists({ foo: "bar" }, "foo")) {
console.log("key exists on object");
}
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}]
Returns the string in capitalize case
import { capitalize } from "@codevachon/utilities";
const str = capitalize("banana tree");
// str = "Banana tree"
Returns the string in camelCase case
import { camelCase } from "@codevachon/utilities";
const str = camelCase("banana tree");
// str = "bananaTree"
Returns the string in pascalCase case
import { pascalCase } from "@codevachon/utilities";
const str = pascalCase("banana tree");
// str = "BananaTree"
Returns the string in upper case
import { uppercase } from "@codevachon/utilities";
const str = uppercase("banana tree");
// str = "BANANA TREE"
Returns the string in lowercase case
import { lowercase } from "@codevachon/utilities";
const str = lowercase("banana tree");
// str = "banana tree"
Returns the string if starts with a value
import { startsWith } from "@codevachon/utilities";
if (startsWith("banana tree", "ban")) {
console.log("string starts with ban");
}
Returns the string if ends with a value
import { endsWith } from "@codevachon/utilities";
if (endsWith("banana tree", "ban")) {
console.log("string ends with ban");
}
Returns the string if contains a value
import { contains } from "@codevachon/utilities";
if (contains("banana tree", "ban")) {
console.log("string contains with ban");
}
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
};
Generates a random integer with Math.random()
import { randomInt } from "@codevachon/utilities";
const value = randomInt(1, 10);
// value => random number from 1 to 10
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
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
returns the provided value an array of values
import { asArray } from "@codevachon/utilities";
const value = asArray("a");
// value => ["a"]
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]
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"]
Generated using TypeDoc