1.1.0 • Published 2 years ago

handy-js-functions v1.1.0

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

��## Functions

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

ParamTypeDefaultDescription
arrArray.<any>[]the original array
lengthnumber0the 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

ParamTypeDescription
arrarraythe 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

ParamTypeDescription
arrarraythe original array
excludearraythe 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

ParamTypeDefaultDescription
arrArray.<any>the original array
valuenumberthe fill value
startnumber0the start value to fill from
endnumberarr.lengththe 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.

ParamTypeDescription
arrArray.<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

ParamType
arrArray.<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

ParamTypeDescription
arrArray.<any>array of elements
nnumberthe 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

ParamTypeDescription
arrArray.<any>array of elements
...argsanyelements 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

ParamTypeDescription
arrArray.<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

ParamTypeDescription
arrArray.<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 arr0 to arrn

ParamTypeDescription
arrArray.<any>the original array
nnumberthe 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 arrarr.length-1 to arrn

ParamTypeDescription
arrArray.<any>the original array
nnumberthe 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

ParamTypeDescription
arrArray.<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

ParamTypeDescription
keysArray.<number>the keys for the object
valuesArray.<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

ParamTypeDescription
strstringthe 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

ParamTypeDescription
indexnumberthe index to insert the element
elementanythe element to insert
arrArray.<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

ParamTypeDescription
propstringthe name of the prop
arrArray.<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

ParamTypeDescription
strstringthe string to check
charstringthe character to check

repeat(str, n) string

Repeats a string n times

Kind: global function
Returns: string - the string repeated

ParamTypeDescription
strstringthe string repeat
nnumberthe 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

ParamTypeDescription
strstringthe string for the replacement
newValstringthe new substring
targetstringsubstring 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

ParamTypeDefaultDescription
strstringthe string to obtain the words
patternstring"''"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

ParamTypeDescription
objObjectthe original object
valArray.<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

ParamTypeDescription
objObjectthe original object
valArray.<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

ParamTypeDescription
xnumberthe number
ynumberthe 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

ParamTypeDescription
xnumberthe number
ynumberthe 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

ParamTypeDescription
anumberthe 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

ParamTypeDescription
anumberthe 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

ParamTypeDescription
xnumberthe number to obtain multiples of
lengthnumberthe 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

ParamTypeDescription
arrArray.<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

ParamTypeDescription
nnumberthe 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

ParamTypeDescription
strstringthe 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

ParamTypeDescription
strstringthe 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

ParamTypeDescription
strstringthe 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

ParamTypeDescription
strstringthe string
letterstringthe 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

ParamTypeDescription
numbernumberthe number

Example

formatNumber(1000000) => 1M

roman(number) string

Converts a number to its roman numberal

Kind: global function
Returns: string - the roman numeral

ParamTypeDescription
numbernumberthe 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

ParamTypeDescription
astringthe first string
bstringthe 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

ParamTypeDescription
numnumberthe 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

ParamTypeDescription
nnumberthe number

Example

nthfib(10) => 34

ordinal(num) string

Returns the ordinal of a number

Kind: global function
Returns: string - the ordinal number

ParamTypeDescription
numnumberthe 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

ParamTypeDescription
wordstringthe 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

ParamTypeDescription
arrArray.<any>the arr to shift
knumberthe 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

ParamTypeDescription
arrArray.<any>the arr to shift
knumberthe 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

ParamTypeDescription
arrArray.<any>the arr to shift
knumberthe 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

ParamTypeDescription
strstringthe string to encode
knumberthe amount to encode by

Example

caesar('test', 2) => vguv
1.1.0

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago