Methods
camelCase(str) → {string|undefined}
Converts a string to camelCase.
Parameters:
Name | Type | Description |
---|---|---|
str |
string | The input string. |
- Source:
Returns:
The camelCased string or undefined if input is invalid.
- Type
- string | undefined
Example
camelCase("hello world"); // "helloWorld"
capitalize(str) → {string|undefined}
Capitalizes the first letter of a string.
Parameters:
Name | Type | Description |
---|---|---|
str |
string | The input string. |
- Source:
Returns:
The capitalized string, or undefined if input is invalid.
- Type
- string | undefined
Example
capitalize("hello world"); // "Hello world"
chunkArray(array, size) → {Array|undefined}
Splits an array into smaller arrays ("chunks") of a specified size.
Parameters:
Name | Type | Description |
---|---|---|
array |
Array | The array to be chunked. |
size |
number | The size of each chunk (must be a positive integer). |
- Source:
Returns:
An array containing the chunked arrays, or undefined if inputs are invalid.
- Type
- Array | undefined
Example
chunkArray([1, 2, 3, 4, 5], 2); // Returns: [[1, 2], [3, 4], [5]]
chunkArray('not-an-array', 2); // Returns: undefined
countOccurrences(str, sub) → {number|undefined}
Counts the occurrences of a substring within a string.
Parameters:
Name | Type | Description |
---|---|---|
str |
string | The main string. |
sub |
string | The substring to count. |
- Source:
Returns:
The number of occurrences or undefiend if any or both inputs are not valid strings.
- Type
- number | undefined
Example
countOccurrences("hello hello", "hello"); // 2
deepClone(obj) → {Object|null}
Creates a deep clone of an object.
Parameters:
Name | Type | Description |
---|---|---|
obj |
Object | The object to clone. |
- Source:
Returns:
The deep-cloned object or null if the argument is not a valid object
- Type
- Object | null
Example
const original = { a: 1, b: { c: 2 } };
const clone = deepClone(original);
clone.b.c = 3;
console.log(original.b.c); // 2
diffInDays(date1, date2) → {number|undefined}
Calculates the difference in days between two dates.
Parameters:
Name | Type | Description |
---|---|---|
date1 |
Date | The first date. |
date2 |
Date | The second date. |
- Source:
Returns:
The difference in days, or undefined if the inputs are invalid.
- Type
- number | undefined
diffInMonths(date1, date2) → {number|undefined}
Calculates the difference in months between two dates.
Parameters:
Name | Type | Description |
---|---|---|
date1 |
Date | The first date. |
date2 |
Date | The second date. |
- Source:
Returns:
The difference in months, or undefined if the inputs are invalid.
- Type
- number | undefined
diffInYears(date1, date2) → {number|undefined}
Calculates the difference in years between two dates.
Parameters:
Name | Type | Description |
---|---|---|
date1 |
Date | The first date. |
date2 |
Date | The second date. |
- Source:
Returns:
The difference in years, or undefined if the inputs are invalid.
- Type
- number | undefined
difference(arr1, arr2) → {Array|undefined}
Returns the elements in the first array that are not present in the second array. Case-sensitive comparison. Objects and arrays are compared by reference.
Parameters:
Name | Type | Description |
---|---|---|
arr1 |
Array | The first array being entered as an argument |
arr2 |
Array | The second array being entered as an argument |
- Source:
Returns:
An new array with the elements in array1 that are not present in array2, undefined if one or both the arrays are not valid.
- Type
- Array | undefined
Example
difference([1, 2, 3, 4, 5, 6],[2, 4, 6]); // Returns: [1, 3, 5]
difference(["Test", "my", "array"],["My", "Array"]); // Returns: ['Test', 'my', 'array']
endsWith(str, suffix) → {boolean|undefined}
Checks if a string ends with a given substring.
Parameters:
Name | Type | Description |
---|---|---|
str |
string | The main string. |
suffix |
string | The suffix to check. |
- Source:
Returns:
True if the string ends with the suffix, false otherwise and undefined if one or both argument inputs are not valid.
- Type
- boolean | undefined
Example
endsWith("hello world", "world"); // true
get(obj, path, defaultValueopt) → {*}
Retrieves the value at a given path within an object.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
obj |
Object | The object to query. |
|
path |
string | The path of the property to get. |
|
defaultValue |
* |
<optional> |
The value returned if the path doesn't exist. |
- Source:
Returns:
The value at the specified path or the default value.
- Type
- *
Example
const obj = { a: { b: { c: 3 } } };
get(obj, 'a.b.c'); // 3
get(obj, 'a.b.d', 'default'); // 'default'
getDayOfTheWeek(date) → {string|undefined}
Gets the day of the week for a given date.
Parameters:
Name | Type | Description |
---|---|---|
date |
Date | The date to check. |
- Source:
Returns:
The name of the day (e.g., "Monday"), or undefined if the input is invalid.
- Type
- string | undefined
getRandomInt(min, max) → {number|NaN}
Generates a random integer within a specified range (inclusive)
Parameters:
Name | Type | Description |
---|---|---|
min |
number | The minimum value of the range. |
max |
number | The maximum value of the range. |
- Source:
Returns:
A random integer between min and max (inclusive), or NaN if min or max are not valid numbers.
- Type
- number | NaN
hasDuplicates(array) → {boolean|undefined}
Checks if an array contains duplicate values
Parameters:
Name | Type | Description |
---|---|---|
array |
Array | The array to check for duplicate values |
- Source:
Returns:
Returns true if array contains duplicate values, false if not, or undefined if either input is invalid.
- Type
- boolean | undefined
Example
hasDuplicates([1, 2, 2, 5, 10, 10]); // Returns: true
hasDuplicates(["John", "Alice", "Mark"]); // Returns: false
hasDuplicates('not-an-array'); // Returns: undefined
hasKey(obj, key) → {boolean|null}
Checks if an object has a specific property as its own (not inherited).
Parameters:
Name | Type | Description |
---|---|---|
obj |
Object | The object to check. |
key |
string | The property name to check for. |
- Source:
Returns:
True if the property exists, false otherwise, null if one or both arguments are not valid
- Type
- boolean | null
Example
hasKey({ a: 1, b: 2 }, 'b'); // true
hasKey({ a: 1, b: 2 }, 'c'); // false
intersection(arr1, arr2) → {Array|undefined}
Returns common elements between two arrays. Case-sensitive comparison. Objects and arrays are compared by reference.
Parameters:
Name | Type | Description |
---|---|---|
arr1 |
Array | The first array being entered as an argument |
arr2 |
Array | The second array being entered as an argument |
- Source:
Returns:
An array of elements common to both input arrays, or undefined if either input is invalid.
- Type
- Array | undefined
Example
intersection([1, 2, 3, 5, 8, 13, 21, 34], [2, 5, 7, 8, 15, 21, 34]); // Returns [2, 5, 8, 21, 34];
intersection(["John", "Alice", "Ben", "Forest"], ["Alice", "Jermaine", "Forest", "Josh"]); //Returns: ["Alice", "Forest"];
isAnagram(str1, str2) → {boolean|undefined}
Checks if two strings are anagrams (contain the same characters in any order).
Parameters:
Name | Type | Description |
---|---|---|
str1 |
string | First string to check |
str2 |
string | Second string to compare with |
- Source:
Returns:
True if the strings have the same characters, or false if otherwise and undefined if one or both input strings are not valid strings.
- Type
- boolean | undefined
Example
isAnagram("listen", "silent"); // true
isAnagram("hello", "world"); // false
isArrayEmpty(array) → {boolean|undefined}
Checks if an array is empty
Parameters:
Name | Type | Description |
---|---|---|
array |
Array | The array to be checked |
- Source:
Returns:
True if array is empty, false if not, undefined if input provided is not a valid array.
- Type
- boolean | undefined
Example
isArrayEmpty([]); // true
isArrayEmpty(['John', 'Maria', 'Joseph']); // false
isArrayEqual(arr1, arr2) → {boolean|undefined}
Performs a shallow comparison to check if two arrays are equal in both value and order. Only compares primitive values (e.g., strings, numbers, booleans).
Parameters:
Name | Type | Description |
---|---|---|
arr1 |
Array | The first array being entered as an argument |
arr2 |
Array | The second array being entered as an argument |
- Source:
Returns:
True if the provided Arrays are equal in their values, false if they have different values, undefined if one or both the arrays are not valid.
- Type
- boolean | undefined
Example
isArrayEqual([1, 2, 3], [1, 2, 3]); // true
isArrayEqual([1, 2, 3], [1, 2, "3"]); // false
isArrayEqual([1, 2], [2, 1]); // false
isArrayEqual([1, 2], 'not-an-array'); // undefined
isArrayEqual(["one", { name: "John" }], ["one", { name: "John" }]); // false (different object references)
isEmpty(obj) → {boolean|null}
Checks if an object is empty (has no enumerable properties).
Parameters:
Name | Type | Description |
---|---|---|
obj |
Object | The object to check. |
- Source:
Returns:
True if the object is empty, false otherwise, null if object is null or not an object
- Type
- boolean | null
Example
isEmpty({}); // true
isEmpty({ key: 'value' }); // false
isEven(number) → {boolean|undefined}
Checks if a number is even
Parameters:
Name | Type | Description |
---|---|---|
number |
number | The number to check. |
- Source:
Returns:
True if the number is even, false otherwise. Returns undefined if the input is not a valid number.
- Type
- boolean | undefined
isLeapYear(year) → {boolean|undefined}
Checks if a given year is a leap year.
Parameters:
Name | Type | Description |
---|---|---|
year |
number | The year to check. |
- Source:
Returns:
True if the year is a leap year, false otherwise, or undefined if the input is invalid.
- Type
- boolean | undefined
isOdd(number) → {boolean|undefined}
Checks if a number is odd
Parameters:
Name | Type | Description |
---|---|---|
number |
number | The number to check. |
- Source:
Returns:
True if the number is odd, false otherwise. Returns undefined if the input is not a valid number.
- Type
- boolean | undefined
kebabCase(str) → {string|undefined}
Converts a string to kebab-case.
Parameters:
Name | Type | Description |
---|---|---|
str |
string | The input string. |
- Source:
Returns:
The kebab-cased string or undefined if input is not a valid string.
- Type
- string | undefined
Example
kebabCase("hello world"); // "hello-world"
merge(target, source) → {Object|null}
Merges two objects, with properties from the second object overwriting those in the first.
Parameters:
Name | Type | Description |
---|---|---|
target |
Object | The target object to merge into. |
source |
Object | The source object to merge from. |
- Source:
Returns:
The merged object, or null if one or both arguments are not valid objects.
- Type
- Object | null
Example
merge({ a: 1, b: 2 }, { b: 3, c: 4 }); // { a: 1, b: 3, c: 4 }
removeDuplicates(array) → {Array|undefined}
Removes duplicate primitive values from an array
Parameters:
Name | Type | Description |
---|---|---|
array |
Array | The array to check for duplicate values |
- Source:
Returns:
An array with no duplicate values or undefined if input is invalid.
- Type
- Array | undefined
Example
removeDuplicates([1, 2, 2, 3, 4, 5, 5, 6]); // Returns: [1, 2, 3, 4, 5, 6]
removeDuplicates('not-an-array'); // Returns: undefined
removeFalsy(array) → {Array|undefined}
Remove falsy values from an array
Falsy values include: false
, 0
, ''
(empty string), null
, undefined
, and NaN
.
Parameters:
Name | Type | Description |
---|---|---|
array |
Array | The array to check for falsy values |
- Source:
Returns:
An array with no falsy values or undefined if input is invalid.
- Type
- Array | undefined
Example
removeDuplicates('not-an-array'); // Returns: undefined
removeFalsy([1, "Ben", null, {"key": "value"}, undefined, 23, 0, true, false]) // Returns: [1, 'Ben', {…}, 23, true]
removeWhitespace(str) → {string|undefined}
Removes all whitespace from a string.
Parameters:
Name | Type | Description |
---|---|---|
str |
string | The input string. |
- Source:
Returns:
The string without whitespace or undefined if input is invalid.
- Type
- string | undefined
Example
removeWhitespace(" hello world "); // "helloworld"
reverseString(str) → {string|undefined}
Reverses a string.
Parameters:
Name | Type | Description |
---|---|---|
str |
string | The input string. |
- Source:
Returns:
The reversed string or undefined if input is not a valid string.
- Type
- string | undefined
Example
reverseString("hello"); // "olleh"
roundTo(number, decimalPlaces) → {number|NaN}
Rounds a number to a specified number of decimal places
Parameters:
Name | Type | Description |
---|---|---|
number |
number | The number to round. |
decimalPlaces |
number | The number of decimal places to round to. Defaults to 0. |
- Source:
Returns:
The rounded number.Returns NaN if the inputs are not valid numbers.
- Type
- number | NaN
set(obj, path, value) → {Object}
Sets the value at a given path within an object.
Parameters:
Name | Type | Description |
---|---|---|
obj |
Object | The object to modify. |
path |
string | The path of the property to set. |
value |
* | The value to set. |
- Source:
Returns:
The updated object.
- Type
- Object
Example
const obj = { a: { b: { c: 3 } } };
set(obj, 'a.b.c', 4);
console.log(obj.a.b.c); // 4
set(obj, 'a.b.d.e', 5);
console.log(obj.a.b.d.e); // 5
snakeCase(str) → {string|undefined}
Converts a string to snake_case.
Parameters:
Name | Type | Description |
---|---|---|
str |
string | The input string. |
- Source:
Returns:
The snake_cased string or undefined if input is not a valid string.
- Type
- string | undefined
Example
snakeCase("hello world"); // "hello_world"
startsWith(str, prefix) → {boolean}
Checks if a string starts with a given substring.
Parameters:
Name | Type | Description |
---|---|---|
str |
string | The main string. |
prefix |
string | The prefix to check. |
- Source:
Returns:
True if the string starts with the prefix, false otherwise. Returns undefined if one or both inputs are not valid strings.
- Type
- boolean
Example
startsWith("hello world", "hello"); // true
sum(numbers) → {number|NaN}
Returns the sum of all numbers in an array
Parameters:
Name | Type | Description |
---|---|---|
numbers |
Array.<numbers> | An array of numbers. |
- Source:
Returns:
An integer value representing the sum of all numbers in the array, or NaN if not a valid array or if any element of the array is not a number.
- Type
- number | NaN
truncate(str, length) → {string|undefined}
Truncates a string to a specified length, adding "..." if truncated.
Parameters:
Name | Type | Description |
---|---|---|
str |
string | The input string. |
length |
number | The maximum length. |
- Source:
Returns:
The truncated string, undefined if input is not a valid string or return the origin string if Length is not a positive number.
- Type
- string | undefined
Example
truncate("Hello world", 5); // "Hello..."