Hey, so I made a light-weight library with functions that come in handy from time to time. Overtime, this will grow. There's some Leetcode questions in here too that I like to compile together in one place to study/refer to later on. Also contains some functions found in the Lodash library. Just helps to streamline workflow so you don't have to write functions you've used before over and over.
Update: Added some math functions
Install it using npm i vic-library
(If you ctrl+f
and find a few useful functions you might need, this library is great for you because it's not massive like the Lodash library, for example.)
Takes 3 arguments:
- Integer
- Lower bound (Int)
- Upper bound (Int)
Returns: - Integer
If the integer provided is smaller than the lower bound, the function returns the lower bound as the final number.
Takes 3 arguments:
- Integer
- Start value (Int)
- End value (Int)
Returns: - Boolean
Checks the provided integer is withing range of start value and end value. If outside of provided range, the function returns a value offalse
.
NOTE If no end value is proved, start value becomes0
and provided start value becomes end value. If the start value is larger than the end value, the function will swap the values. This functionality is provided as a fail-safe should the user pass in incorrect arguments.
Takes 1 argument:
- String
Returns: - Array
Splits long strings into an array of each word found in the original string.
Takes 2 arguments:
- String
- Length (Int)
Returns: - String
Adds padding (spaces) to each side of the array to meet the desired length provided.
Takes 1 argument:
- String
Returns - String
Translate given string into a whale-intelligable language.e
andu
found inside the string will be double. Filters out consonants. Returned string will be in all capitals.
Takes 2 arguments:
- Object
- Key
Returns: - Boolean
Returnstrue
if provided object contains the key and the key contains a value. Returns false if the provided object does not contain the provided key or the key does not have a value.
NOTE Currently doesn't work with nested values
Takes 1 argument:
- Object
Returns: - Object
Iterates through each key of the provided object and swaps the key and value.
NOTE In the case of duplicate values in the object, subsequent values will overwrite property assignments of previous values.
Takes 2 arguments:
- Object
- A predicate function (return Boolean)
Returns: - Object.key
Iterates through each key / value pair and calls the provided predicate function to test the value at its corresponding key. Returns the first key with a truthy value. Returnsundefined
if no key is truthy.
Takes 2 arguments:
- Array
- Integer
Returns - Array
Removes a value from an array at a given value.
Takes 2 arguments:
- Object
- A predicate function (takes 3 args: current index, current element index, and the provided array)
Returns - Array
Creates a new copy of the provided array, dropping elements from the beginning of the array until an element causes the predicate function to return a falsy value.
Takes 2 arguments:
- Array
- Size (Int)
Returns: - Array
Disjunction the provided array into an array of arrays at the provided size.
NOTE If the array cannot be broken up evenly, the last array chunk will be truncated to math the provided size argument.
NOTE If no size is supplied, it's value will be set to1
.
Takes 1 argument:
- String
Returns: - Integer
Converts a given roman numeral as a string and returns the corresponding integer. The European Spanish language uses a lot of roman numerals so this is useful in translation scenarios.
Takes 1 argument:
- Integer
Returns: - String
Formats a given integer into a string with the appropriate decimals, commas, and dollar sign. Currently configured to USD.
Takes 3 arguments:
- Integer (A value of quadratic)
- Integer (B value of quadratic)
- Integer (C value of quadratic)
Returns: - String || Array
Finds x intercepts of parabola from quadratic equation (a^2x + bx + c) in an array. If no x intercepts are found, returns string format of imaginary solutions (i).
Takes1 argument:
- Integer
Returns: - Integer
Finds factorial of any number Example: 5! = 5 * 4 * 3 * 2 * 1 = 120.