A collection of small JavaScript utilities by Will Stone.
Library Rules
- Zero dependencies.
- Never throw.
- Never assume anything about input value(s).
- Always return a sensible default.
Naming
Prefix | Condition |
---|---|
check | Returns a boolean |
generate | Returns new data |
to | Returns coerced input |
Functions that do not return a value do not have a prefix. |
This allows for comfortable variable naming without clashes, for example:
const isUnique = checkUnique([1, 2, 3])
Install
npm i tings
checkUnique Array
Determines if an array only contains unique items. Will return true for any input that’s not an array.
import { checkUnique } from 'tings'
checkUnique([1, 2, 3]) // true
checkUnique([1, 1, 2]) // false
checkUnique('not array') // true
Source: check-unique.ts
generateIntegers Array
Generate an array of integers from a given number to another.
import { generateIntegers } from 'tings'
generateIntegers(1, 6) // [1, 2, 3, 4, 5, 6]
generateIntegers("abc", 6) // [] - at least one input is not a number
Source: generate-integers.ts
checkDate Date
Determines if input is a valid Date.
import { checkDate } from 'tings'
checkDate(new Date()) // true
checkDate("not a date") // false
Source: check-date.ts
checkBrowser Environment
Determines if current code is running in a browser environment.
import { checkBrowser } from 'tings'
checkBrowser() // true
Source: check-browser.ts
sleep Misc
Pauses the current function.
import { sleep } from 'tings'
await sleep(2000) // sleeps for 2 seconds
await sleep("abc") // sleeps for 0 seconds as input is not a number
Source: sleep.ts
checkEven Number
Determines if input is an even integer.
import { checkEven } from 'tings'
checkEven(-4) // true
checkEven(-1) // false
checkEven(0) // true
checkEven(1) // false
checkEven(2) // true
checkEven(5) // false
checkEven(9) // false
checkEven(3.14) // false
Source: check-even.ts
checkOdd Number
Determines if input is an odd integer.
import { checkOdd } from 'tings'
checkOdd(-4) // false
checkOdd(-1) // true
checkOdd(0) // false
checkOdd(1) // true
checkOdd(2) // false
checkOdd(5) // true
checkOdd(9) // true
checkOdd(3.14) // false
Source: check-odd.ts
checkPrime Number
Determines if input is a prime number.
import { checkPrime } from 'tings'
checkPrime(0) // false
checkPrime(1) // false
checkPrime(2) // true
checkPrime(5) // true
checkPrime(9) // false
checkPrime(3.14) // false
Source: check-prime.ts
toNumber Number
Will return the given input as a number, if it cannot be converted, it will return 0.
import { toNumber } from 'tings'
toNumber('this is text') // 0
toNumber('3') // 3
toNumber(0.4) // 0.4
toNumber(new Set([])) // 0
Source: to-number.ts
toOrdinal Number
Converts a number to its ordinal string form, if possible, else returns 0th.
- 1 -> 1st
- 4.2 -> 4.2th
- notANumber -> 0th
import { toOrdinal } from 'tings'
toOrdinal('this is text') // 0th
toOrdinal('3') // 3rd
toOrdinal('11') // 11th
toOrdinal(3.14) // 3.14th
Source: to-ordinal.ts
checkCompactCase String
Determines if input is a string that does not contain white-spaces.
import { checkCompactCase } from 'tings'
checkCompactCase('thisiscompact') // true
checkCompactCase('this is not compact') // false
checkCompactCase(123) // false - input is not a string
Source: check-compact-case.ts
checkLowerCase String
Determines if input is a string containing no UPPERcase characters.
import { checkLowerCase } from 'tings'
checkLowerCase('this is lower') // true
checkLowerCase('This is not LOWER') // false
checkLowerCase(123) // false - input is not a string
Source: check-lower-case.ts
checkUrlAbsolute String
Determines if a URL string is absolute by seeing if it starts with a protocol. Will return false for any input that’s not a string.
import { checkUrlAbsolute } from 'tings'
checkUrlAbsolute("http://example.com/page") // true
checkUrlAbsolute("/page") // false
checkUrlAbsolute(123) // false - input is not a string
Source: check-url-absolute.ts
toCompactCase String
Removes all whitespace characters from a string.
import { toCompactCase } from 'tings'
toCompactCase('this is text') // thisistext
toCompactCase('someTEXT') // someTEXT
Source: to-compact-case.ts
toLetters String
Removes all non-alphabetical characters from a string.
import { toLetters } from 'tings'
toLetters('this is text') // thisistext
toLetters('123-Text!!!') // Text
Source: to-letters.ts
Top