Skip to content

JTLavs/handy-js-functions

Repository files navigation

Functions

chunk([arr], [length])Array.<Array.<any>>

Returns an array of chunks of specified length

compact(arr)array

Returns a new array with falsey values removed.

difference(arr, exclude)arr

Returns a new array with values in the exclude array removed

fill(arr, value, [start], [end])arr

Fills the array from the specified start to end indices

uniq(arr)array

Returns a new array with duplicate values removed.

head(arr)any

Returns the first array value

nth(arr, n)any

Returns element nth positions from the start of the array is n is positive otherwise returns element nth positions from the end if n is negative.

pull(arr, ...args)Array.<any>

Returns an array with the passed elements excluded

tail(arr)Array.<any>

Returns all elements except the first element

exceptLast(arr)Array.<any>

Returns all elements except the first element

take(arr, n)Array.<any>

Returns a portion of an array to n from the start of the array

takeRight(arr, n)Array.<any>

Returns a portion of an array to n from the end of the array

mean(arr)number

Returns the average of an array of numerical values

zipObject(keys, values)object

Creates an object from an array of keys and an array of values

capitalize(str)string

Returns a string with the first character converted to uppercase

insertElement(index, element, arr)Array.<any>

Inserts an element in the nth position

sortBy(prop, arr)Array.<any>

Sorts by a property on an array of objects

endsWithChar(str, char)Boolean

Checks if a string ends with a character

repeat(str, n)string

Repeats a string n times

replace(str, newVal, target)string

Replaces a substring with another substring

words(str, [pattern])Array.<string>

Returns the words from a string as an array

omit(obj, val)object

Removes values from an object

pick(obj, val)object

Returns properties specified from an object

isDivisibleBy(x, y)Boolean

Checks if a number x is divisible by divisor y with no remainder

nextWithNoRemainder(x, y)number

Returns the next divisor with no remainder of a number if the passed divisor y has a remainder

factors(a)Array.<number>

Returns all the factors for a number

isPrime(a)boolean

Returns true if the number is a prime number

multiples(x, length)Array.<number>

Calculates the multiples of x up until length

median(arr)number

Calculates the median value of an array

primesUpTo(n)Array.<number>

Returns the number of prime numbers up until n (inclusive)

noOfVowels(str)number

Returns the number of vowels in a string

noOfConsonants(str)number

Returns the number of consonants in a string

removeSpaces(str)string

Removes spaces from a string

letterOccurance(str, letter)number

Returns the occurences of a letter in a string

formatNumber(number)string

Converts a large number to a string with a suffix

roman(number)string

Converts a number to its roman numberal

isAnagram(a, b)boolean

Returns true if a and b are anagrams

fib(num)Array.<number>

Returns the first n elements of the fibonacci sequence

nthfib(n)number

Returns the nth number in the fibonacci sequence

ordinal(num)string

Returns the ordinal of a number

palindrome(word)boolean

Returns true if a word is a palindrome

isLeapYear(arr, k)Array.<any>

Checks if a year is a leap year

shiftRight(arr, k)Array.<any>

Moves the elements to the right by a specified value

shiftLeft(arr, k)Array.<any>

Moves the elements to the left by a specified value

caesar(str, k)string

The caesar cipher

chunk([arr], [length]) → Array.<Array.<any>>

Returns an array of chunks of specified length

Kind: global function
Returns: Array.<Array.<any>> - - the array with chunks

Param Type Default Description
[arr] Array.<any> [] the original array
[length] number 0 the length of each chunk

Example

chunk([1,2,3,4,5,6,7], 2) => [[1,2], [3,4], [5,6], [7]] 

compact(arr) → array

Returns a new array with falsey values removed.

Kind: global function
Returns: array - the filtered array

Param Type Description
arr array the original array

Example

compact([undefined, 1, 0, null, 'hello']) => [1,'hello'] 

difference(arr, exclude) → arr

Returns a new array with values in the exclude array removed

Kind: global function
Returns: arr - the filtered array

Param Type Description
arr array the original array
exclude array the array with the values to exclude

Example

difference(['alice', 'bob', 'dave'], ['alice', 'bob']) => ['dave'] 

fill(arr, value, [start], [end]) → arr

Fills the array from the specified start to end indices

Kind: global function
Returns: arr - the new array

Param Type Default Description
arr Array.<any> the original array
value number the fill value
[start] number 0 the start value to fill from
[end] number arr.length the end value to stop filling at (not inclusive)

Example

fill([1,2,3,4,5,6,7], 0, 1, 3) => [1,0,0,4,5,6,7] 

uniq(arr) → array

Returns a new array with duplicate values removed.

Kind: global function
Returns: array - array with any duplicate values removed.

Param Type Description
arr Array.<any> array with duplicate values

Example

uniq([1,1,2,4,4,5,6]) => [1,2,4,5,6]

head(arr) → any

Returns the first array value

Kind: global function
Returns: any - the first element

Param Type
arr Array.<any>

Example

head([1,2,3,4,5]) => 1

nth(arr, n) → any

Returns element nth positions from the start of the array is n is positive otherwise returns element nth positions from the end if n is negative.

Kind: global function
Returns: any - the nth element

Param Type Description
arr Array.<any> array of elements
n number the position of the element to be returned

Example

nth([1,2,3,4,5], 3) => 4
nth([1,2,3,4,5], -3) => 2

pull(arr, ...args) → Array.<any>

Returns an array with the passed elements excluded

Kind: global function
Returns: Array.<any> - - the array with the elements removed

Param Type Description
arr Array.<any> array of elements
...args any elements to exclude

Example

nth([1,2,3,4,5], 1,2,3) => [4,5]

tail(arr) → Array.<any>

Returns all elements except the first element

Kind: global function
Returns: Array.<any> - array containing all elements except the first element

Param Type Description
arr Array.<any> the original array

Example

tail([1,2,3,4,5]) => [2,3,4,5]

exceptLast(arr) → Array.<any>

Returns all elements except the first element

Kind: global function
Returns: Array.<any> - array containing all elements except the first element

Param Type Description
arr Array.<any> the original array

Example

tail([1,2,3,4,5]) => [2,3,4,5]

take(arr, n) → Array.<any>

Returns a portion of an array to n from the start of the array

Kind: global function
Returns: Array.<any> - array containing all elements from arr[0] to arr[n]

Param Type Description
arr Array.<any> the original array
n number the index to slice the array

Example

take([1,2,3,4,5], 3) => [1,2,3]

takeRight(arr, n) → Array.<any>

Returns a portion of an array to n from the end of the array

Kind: global function
Returns: Array.<any> - array containing all elements from arr[arr.length-1] to arr[n]

Param Type Description
arr Array.<any> the original array
n number the index to slice the array

Example

takeRight([1,2,3,4,5], 3) => [3,4,5] 

mean(arr) → number

Returns the average of an array of numerical values

Kind: global function
Returns: number - the average of the elements in the array

Param Type Description
arr Array.<number> the original array of numerical values

Example

mean([1,2,3,4,5]) => 3

zipObject(keys, values) → object

Creates an object from an array of keys and an array of values

Kind: global function
Returns: object - the new object

Param Type Description
keys Array.<number> the keys for the object
values Array.<number> the values for the object

Example

zipObject([1,2], ['a',b']) => { '1': 'a', '2': 'b' };

capitalize(str) → string

Returns a string with the first character converted to uppercase

Kind: global function
Returns: string - the string with the first character capitalized

Param Type Description
str string the original string

Example

capitalize('hello') => 'Hello'

insertElement(index, element, arr) → Array.<any>

Inserts an element in the nth position

Kind: global function
Returns: Array.<any> - the array with the inserted element

Param Type Description
index number the index to insert the element
element any the element to insert
arr Array.<any> the array in which to insert the element

sortBy(prop, arr) → Array.<any>

Sorts by a property on an array of objects

Kind: global function
Returns: Array.<any> - the array sorted by the property

Param Type Description
prop string the name of the prop
arr Array.<any> array of objects

endsWithChar(str, char) → Boolean

Checks if a string ends with a character

Kind: global function
Returns: Boolean - if the string ends with the passed character

Param Type Description
str string the string to check
char string the character to check

repeat(str, n) → string

Repeats a string n times

Kind: global function
Returns: string - the string repeated

Param Type Description
str string the string repeat
n number the amount of repetitions

Example

repeat('repeatme', 2) => 'repeatmerepeatme'

replace(str, newVal, target) → string

Replaces a substring with another substring

Kind: global function
Returns: string - the string with the new substring

Param Type Description
str string the string for the replacement
newVal string the new substring
target string substring to be replaced

Example

replace('hello world', 'there', 'world') => 'hello there' 

words(str, [pattern]) → Array.<string>

Returns the words from a string as an array

Kind: global function
Returns: Array.<string> - the array of words

Param Type Default Description
str string the string to obtain the words
[pattern] string "''" the pattern to split the string by

Example

words('give me the words') => [give, me, the, words]

omit(obj, val) → object

Removes values from an object

Kind: global function
Returns: object - the new object with the properties removed

Param Type Description
obj Object the original object
val Array.<any> the array of values to omit

Example

omit({name: 'Alice', age: 20, lives_in: 'New York'}, ['name', 'age']) => {'lives_in': 'New York'}

pick(obj, val) → object

Returns properties specified from an object

Kind: global function
Returns: object - the new object with the specified properties

Param Type Description
obj Object the original object
val Array.<any> the array of values to obtain

Example

pick({name: 'Alice', age: 20, lives_in: 'New York'}, ['name', 'age']) => {'name': 'Alice', age: 20}

isDivisibleBy(x, y) → Boolean

Checks if a number x is divisible by divisor y with no remainder

Kind: global function
Returns: Boolean - true if divisable with no remainder otherwise false

Param Type Description
x number the number
y number the divisor

Example

isDivisibleBy(24,12) => true
isDivisibleBy(24,5) => false 

nextWithNoRemainder(x, y) → number

Returns the next divisor with no remainder of a number if the passed divisor y has a remainder

Kind: global function
Returns: number - a divisor with no remainder

Param Type Description
x number the number
y number the divisor

Example

nextWithNoRemainder(24,12) => 12
nextWithNoRemainder(24,5) => 6 

factors(a) → Array.<number>

Returns all the factors for a number

Kind: global function
Returns: Array.<number> - array of factors

Param Type Description
a number the number to obtain the factors

Example

factors(12) => [1,2,3,4,6,12]

isPrime(a) → boolean

Returns true if the number is a prime number

Kind: global function
Returns: boolean - if a is prime

Param Type Description
a number the number to check

Example

isPrime(13) => true

multiples(x, length) → Array.<number>

Calculates the multiples of x up until length

Kind: global function
Returns: Array.<number> - the array of multiples

Param Type Description
x number the number to obtain multiples of
length number the limit

Example

multiples(10, 4) => [10,20,30,40]

median(arr) → number

Calculates the median value of an array

Kind: global function
Returns: number - the median

Param Type Description
arr Array.<number> the array of numbers

Example

median([1,2,3,4,5,6,7,8,9,10]) => 5.5

primesUpTo(n) → Array.<number>

Returns the number of prime numbers up until n (inclusive)

Kind: global function
Returns: Array.<number> - the array of prime numbers

Param Type Description
n number the limit

Example

primesUpTo(10) => [2,3,5,7]

noOfVowels(str) → number

Returns the number of vowels in a string

Kind: global function
Returns: number - the number of vowels

Param Type Description
str string the string

Example

noOfVowels('hello world') => 3

noOfConsonants(str) → number

Returns the number of consonants in a string

Kind: global function
Returns: number - the number of consonants

Param Type Description
str string the string

Example

noOfConsonants('hello world') => 7

removeSpaces(str) → string

Removes spaces from a string

Kind: global function
Returns: string - the string with the spaces removed

Param Type Description
str string the string

Example

removeSpaces('hello world') => 'helloworld'

letterOccurance(str, letter) → number

Returns the occurences of a letter in a string

Kind: global function
Returns: number - the occurences of the specified letter

Param Type Description
str string the string
letter string the letter to count

Example

letterOccurance('hello world', 'e') => 1

formatNumber(number) → string

Converts a large number to a string with a suffix

Kind: global function
Returns: string - the string with the suffix

Param Type Description
number number the number

Example

formatNumber(1000000) => 1M

roman(number) → string

Converts a number to its roman numberal

Kind: global function
Returns: string - the roman numeral

Param Type Description
number number the number

Example

roman(20) => XX

isAnagram(a, b) → boolean

Returns true if a and b are anagrams

Kind: global function
Returns: boolean - true if a and b are anagrams, otherwise false

Param Type Description
a string the first string
b string the second string

Example

isAnagram('debit card', 'bad credit') => true

fib(num) → Array.<number>

Returns the first n elements of the fibonacci sequence

Kind: global function
Returns: Array.<number> - the sequence

Param Type Description
num number the number of elements

Example

fib(10) => [0,1,1,2,3,5,8,13,21,34]

nthfib(n) → number

Returns the nth number in the fibonacci sequence

Kind: global function
Returns: number - the nth number in the sequence

Param Type Description
n number the number

Example

nthfib(10) => 34

ordinal(num) → string

Returns the ordinal of a number

Kind: global function
Returns: string - the ordinal number

Param Type Description
num number the number

Example

ordinal(1) => '1st'

palindrome(word) → boolean

Returns true if a word is a palindrome

Kind: global function
Returns: boolean - true if word is a palindrome, other wise false

Param Type Description
word string the word to test

Example

palindrome('racecar') => true

isLeapYear(arr, k) → Array.<any>

Checks if a year is a leap year

Kind: global function
Returns: Array.<any> - the shifted array

Param Type Description
arr Array.<any> the arr to shift
k number the amount to shift the array by

Example

leapYear(2004) => [4,5,1,2,3]

shiftRight(arr, k) → Array.<any>

Moves the elements to the right by a specified value

Kind: global function
Returns: Array.<any> - the shifted array

Param Type Description
arr Array.<any> the arr to shift
k number the amount to shift the array by

Example

shiftRight([1,2,3,4,5], 2) => [4,5,1,2,3]

shiftLeft(arr, k) → Array.<any>

Moves the elements to the left by a specified value

Kind: global function
Returns: Array.<any> - the shifted array

Param Type Description
arr Array.<any> the arr to shift
k number the amount to shift the array by

Example

shiftLeft([1,2,3,4,5], 2) => [3,4,5,1,2]

caesar(str, k) → string

The caesar cipher

Kind: global function
Returns: string - the encoded string

Param Type Description
str string the string to encode
k number the amount to encode by

Example

caesar('test', 2) => vguv

About

Some handy Javascript functions

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published