A collection of small JavaScript utilities by Will Stone.

Install

npm i tings

isBrowser

Determines if current code is running in a browser environment.

Example

import isBrowser from 'tings/lib/isBrowser' isBrowser() // true

isCompactCase

Determines if input is a string that does not contain whitespaces.

Example

import isCompactCase from 'tings/lib/isCompactCase' isCompactCase('thisiscompact') // true isCompactCase('this is not compact') // false

isLowerCase

Determines if input is a string containing no UPPERcase characters.

Example

import isLowerCase from 'tings/lib/isLowerCase' isLowerCase('this is lower') // true isLowerCase('This is not LOWER') // false

isUniq

Determines if an array only contains unique items. Will return true for any input that's not an array.

Example

import isUniq from 'tings/lib/isUniq' isUniq([1, 2, 3]) // true isUniq([1, 1, 2]) // false isUniq('not array') // true

sleep

Pauses the current function.

Example

import sleep from 'tings/lib/sleep' await sleep(2000) // sleeps for 2 seconds

toCompactCase

Removes all whitespace characters from a string.

Example

import toCompactCase from 'tings/lib/toCompactCase' toCompactCase('this is text') // thisistext toCompactCase('someTEXT') // someTEXT

toLetters

Removes all non-alphabetical characters from a string.

Example

import toLetters from 'tings/lib/toLetters' toLetters('this is text') // thisistext toLetters('123-Text!!!') // Text

toNumberAlways

Will return the given input as a number, if it cannot be converted, it will return 0.

Example

import toNumberAlways from 'tings/lib/toNumberAlways' toNumberAlways('this is text') // 0 toNumberAlways('3') // 3 toNumberAlways(0.4) // 0.4 toNumberAlways(new Set([])) // 0

toOrdinal

Converts a number to its ordinal string form, if possible, else returns 0th.

Example

import toOrdinal from 'tings/lib/toOrdinal' toOrdinal('this is text') // 0th toOrdinal('3') // 3rd toOrdinal('11') // 11th