1.9.0 • Published 2 years ago

@theonlydevsever/utilities v1.9.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

@theonlydevsever/utilities

test publish tag

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

ParamTypeDetailsRequired
valuestringThe string to capitalize. Defaults to an empty stringfalse
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.

ParamTypeDetailsRequired
dataGeneric \| Generic[]A single value or an array of valuestrue
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.

ParamTypeDetailsRequired
valueunknownThis value can be anythingtrue
typeExtendedPrimitiveTypeCan be one of:arraybigintbooleanfunctionnullnumberobjectstringsymbolundefinedtrue
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
1.9.0

2 years ago

1.8.4

2 years ago

1.8.3

2 years ago

1.8.2

3 years ago

1.8.1

3 years ago

1.8.0

3 years ago

1.7.1

3 years ago

1.7.0

3 years ago

1.6.3

3 years ago

1.6.2

3 years ago

1.6.1

3 years ago

1.6.0

3 years ago

1.5.0

3 years ago

1.4.0

3 years ago

1.3.0

3 years ago