A collection of small JavaScript utilities by Will Stone.

Install

npm i tings

isCompactCase

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

Example

import isCompactCase from 'tings/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/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/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/sleep'

await sleep(2000) // sleeps for 2 seconds

toCompactCase

Removes all whitespace characters from a string.

Example

import toCompactCase from 'tings/toCompactCase'

toCompactCase('this is text') // thisistext
toCompactCase('someTEXT') // someTEXT

toLetters

Removes all non-alphabetical characters from a string.

Example

import toLetters from 'tings/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/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.

  • 1 -> 1st
  • 4.2 -> 4.2th
  • notANumber -> 0th

Example

import toOrdinal from 'tings/toOrdinal'

toOrdinal('this is text') // 0th
toOrdinal('3') // 3rd
toOrdinal('11') // 11th