Utils docs

A set of JS utils functions that I use often.

String functions

initcap

Capitalizes the first letter and lowercases all other letters in each word of the passed string.

import { initcap } from 'utils'
// Prints 'Hello, World'
console.log(initcap('HELLO, WORLD'))

trimCharSeq

Trims any repeatable sequence of chars in a string to a single char.

import { trimCharSeq } from 'utils'
// 'Duplicates are removed'
console.log(trimCharSeq('Duuplicates   are remmmmoved'))

mapString

Returns a string which is populated with results of applying provided callback on each character.

import { mapString } from 'utils'

// Returns an empty string for even numbers
let fn = (c) => Number(c) % 2 === 0 ? '' : c

// '135'
console.log(mapString('12345'))

Array functions

last

Returns the last element in an array. If array is empty, returns undefined.

import { last } from 'utils'
// 4
console.log(last([1,2,3,4]))

// undefined
console.log(last([]))

toArray

Normalizes a value to an array. If a specified value is an array, returns it as is. If the specified value is null or undefined, returns an empty array.

import { toArray } from 'utils'

// [1]
console.log(toArray(1))

// [1,2,3]
console.log(toArray([1,2,3]))

// []
console.log(toArray(null))