1.9.0 • Published 3 years ago
@theonlydevsever/utilities v1.9.0
@theonlydevsever/utilities
Small & helpful utility functions that everyone is tired of writing
Install
// using yarn
yarn add @theonlydevsever/utilities
// using npm
npm install @theonlydevsever/utilities
capitalize
This function capitalizes the first character in a passed string
Param | Type | Details | Required |
---|---|---|---|
value | string | The string to capitalize. Defaults to an empty string | false |
import { capitalize } from "@theonlydevsever/utilities";
const x = "dogs";
const capitalizedX = capitalize(x); // => "Dogs"
const y = "Cats";
const capitalizedY = capitalize(y); // => "Cats"
// The function will also remove any wrapping whitespace
const z = " it's really spacious in here ";
const capitalizedZ = capitalize(z); // => "It's really spacious in here"
forceArray
Sometimes, when retrieving a list of records from a third-party API, you will be sent back an array of records or a single record.
To help standardize workflow in your applications, this function forces the return of an array.
Param | Type | Details | Required |
---|---|---|---|
data | Generic \| Generic[] | A single value or an array of values | true |
import { forceArray } from "@theonlydevsever/utilities";
const apiResponse1 = "Volition";
const arr1 = forceArray<string>(apiResponse1); // => ["Volition"]
const apiResponse2 = ["Kezia", "Fortress"];
const arr2 = forceArray<string>(apiResponse2); // => ["Kezia", "Fortress"]
isValueOfType
This function will return whether or not the value passed is of the passed type.
This is useful for standardizing input going in and coming out of a system, as well as ensuring type validity before performing any type specific operations.
Param | Type | Details | Required |
---|---|---|---|
value | unknown | This value can be anything | true |
type | ExtendedPrimitiveType | Can be one of:arraybigintbooleanfunctionnullnumberobjectstringsymbolundefined | true |
import { isValueOfType } from "@theonlydevsever/utilities";
const func = () => console.log("Red Stapler");
if (isValueOfType(func, "function")) {
func();
} else {
// The value of `func` was not what was expected -- handle this scenario accordingly...
}
// Other Examples
isValueOfType("Pong", "string"); // => true
isValueOfType(42, "array"); // => false
isValueOfType(BigInt(23456), "bigint"); // => true
isValueOfType([1, 2, 3], "array"); // => true
isValueOfType({ id: 1, name: "Bubbles" }, "object") // => true
isValueOfType("function in disguise", "function") // => false