Cleans the given array of empty values

arrayClean(array: Array): Array
Parameters
array (Array) The array to be cleaned
Returns
Array: The clean array
Throws
  • TypeError: When the passed array is not actually an array.
Example
const sourceArray = [undefined, null, true, '', {}];
arrayClean(sourceArray); // Returns: [true, {}]
arrayClean() // throws: Cannot read property 'length' of undefined

Check if an array contains a specified item

arrayContains(array: Array, item: any): boolean
Parameters
array (Array) The array to check for the item
item (any) The item to look for in the array
Returns
boolean: Returns true when the item is found, otherwise false

arrayDifference

src/arrayDifference.js
arrayDifference(array1: Array, array2: Array, biDirectional: boolean): Array
Parameters
array1 (Array)
array2 (Array)
biDirectional (boolean) False returns A-B while true returns A-B concat B-A
Returns
Array:
Throws
  • TypeError: When filter can not be found on array . This generally happens when the array is null or undefined
Example
const a = [1, 2];
const b = [1, 3];
difference(a, b) // returns: [2]
difference(a, b, true) // returns: [2, 3]

Create an array from a value

arrayFrom(param: any, isNewRef: boolean?): Array
Parameters
param (any) Value to transform to an array
isNewRef (boolean?) Should return a new reference than the one from the param value
Returns
Array: The resulting array

Insert a value into an array

arrayInsert(array: Array, index: number, items: any): Array
Parameters
array (Array) The array to insert the items into
index (number) The index where to insert the values
items (any) The item(s) to insert
Returns
Array: The resulting array with the inserted items
Example
const sourceArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
sourceArray, undefined, ['a', 'b', 'c']); // Returns: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 'a', 'b', 'c']

Return the highest value (number) in the given array

arrayMax(array: Array, comparisonFn: any): Array
Parameters
array (Array) The array to be scanned
comparisonFn (any)
Returns
Array: The highest value
Throws
  • TypeError: When the passed array is not actually an array.
Example
const sourceArray = [1,3,2];
arrayMax(sourceArray); // Returns: 3
arrayClean() // throws: Cannot read property 'length' of undefined

Return the lowest value (number) in the given array

arrayMin(array: Array, comparisonFn: any): Array
Parameters
array (Array) The array to be scanned
comparisonFn (any)
Returns
Array: The lowest value
Throws
  • TypeError: When the passed array is not actually an array.
Example
const sourceArray = [3,1,2];
arrayMax(sourceArray); // Returns: 1
arrayClean() // throws: Cannot read property 'length' of undefined
arrayPluck(array: Array, propertyName: (string | number)): Array
Parameters
array (Array)
propertyName ((string | number)) The property of the
Returns
Array:
Throws
  • TypeError: When the propertyName can not be found on an item in the array . This generally happens when a value in the array is null or undefined
Example
const values = [{name: 'John'}, {name: 'James'}];
arrayPluck(values, 'name') // returns: ['John', 'James']
const values = [{name: 'John'}, {}, {name: 'James'}];
arrayPluck(values, 'name') // returns: ['John', undefined, 'James']
const values = [undefined, {name: 'James'}];
arrayPluck(values, 'name') // throws: Cannot read property 'name' of undefined

Repeats content of array by structure or by index, returns flattened array.

arrayRepeat(array: array, n: number, byIndex: boolean): array
Parameters
array (array)
n (number = 1)
byIndex (boolean = false)
Returns
array:
Example
const values = [1, 2, 3];
arrayRepeat(values, 3) // returns: [1, 2, 3, 1, 2, 3, 1, 2, 3]
const values = [1, 2, 3];
arrayRepeat(values, 3, true) // returns: [1, 1, 1, 2, 2, 2, 3, 3, 3]

Replace values in an array with given values or remove values from an array.

arrayReplace(array: Array, index: number, removeCount: number, insert: any): Array
Parameters
array (Array) The array where the values should be replaced
index (number) The index in the array where to start the replacement
removeCount (number) The number of items to remove
insert (any) The item(s) to be inserted
Returns
Array: The array with the replaced values
Example
const sourceArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
arrayReplace(sourceArray, 0, 5, ['a', 'b', 'c', 'd']); // Returns: ['a', 'b', 'c', 'd', 6, 7, 8, 9, 10]

Create an array from the passed param. (When the value is an object only takes the iterable properties from the object (not its prototypes))

arrayTo(param: (Object | String | Array)): Array
Parameters
param ((Object | String | Array)) The value to transform to an array
Returns
Array: Returns the param transformed into an array, or an empty array of the value can not be transformed.
Example
arrayTo('myData') // Returns: ['myData']
const sourceObject = {
    name: 'ANC First Visit',
    shortName: 'ANC1',
    level: 1,
};
arrayTo(sourceObject); // Returns: ['ANC First Visit', 'ANC1', 1]
arrayTo(null); // Returns: []

Creates an array of unique values from the array passed. This function does not do a deep compare. Objects with the same values will therefore not be filtered.

arrayUnique(array: Array): Array
Parameters
array (Array) The array to create the array of uniques from
Returns
Array: The array containing the unique values
Throws
  • TypeError: Is thrown when the argument passed is not an array
Example
const sourceArray = [1, 1, 2, 3, 2, 4, 4, 3];
arrayUnique(sourceArray); // returns: [1, 2, 3, 4]
const A = {name: 'A'};
const B = {name: 'B'};
arrayUnique([A, A, B, B]); // Returns: [{name: 'A'}, {name: 'B'}]
const sourceArray = [{name: 'A'}, {name: 'B'}, {name: 'B'}];
arrayUnique(sourceArray); // Returns: [{name: 'A'}, {name: 'B'}, {name: 'B'}]

camelCaseToUnderscores

src/camelCaseToUnderscores.js

Convert a camelCase word to its underscore_case equivalent

camelCaseToUnderscores(wordToTransform: string): string
Parameters
wordToTransform (string) The string to transform.
Returns
string: The wordToTransform as a underscore separated string eg. camelCase -> camel_case
Example
const word = 'HelloMyAmazingWorld';
camelCaseToUnderscores(word); // Returns: 'hello_my_amazing_world'

Create a clone a value

clone(item: any)
Parameters
item (any)
Example
const person1 = {name: 'John'};
const person2 = clone(person1); // Returns {name: 'John'}
// but
person1 !== person2 // Returns:  true

Check if the value is an Array

isArray
Parameters
param (any) Value to be checked
Returns
boolean: Returns true when the param is an Array
Example
isArray([]); // Returns: true
isArray({}); // Returns: false
isArray('Hello'); // Returns: false

isArrayOfStrings

src/isArrayOfStrings.js

Check if an array consists of strings

isArrayOfStrings(arrayToCheck: Array): boolean
Parameters
arrayToCheck (Array) The array to be checked for string values
Returns
boolean: True when the array contains just string otherwise false
Example
isArrayOfStrings(['Mark', 'John']) // Returns: true
isArrayOfStrings([{name: 'Mark'}, {name: 'John'}]) // Returns: false

Check if a value is a boolean value true or false

isBoolean
Parameters
param (any) The value to check for boolean type
Returns
boolean: Returns true when the value is a boolean otherwise false

Check if param is defined.

isDefined(param: any): boolean
Parameters
param (any) The value to check
Returns
boolean: Returns false when param is undefined otherwise true.

Check if a value is concidered empty. An empty value is an empty Set, Map or Array. Depending on if the allowEmptyString flag, empty strings are also considered empty

isEmpty(param: any, allowEmptyString: boolean): boolean
Parameters
param (any) Value to check
allowEmptyString (boolean) When set to true an empty string will not be considered empty .
Returns
boolean: Returns true when the value is empty , otherwise false
Example
isEmpty(''); // Returns: true
isEmpty('', true); // Returns: false
isEmpty(null); // Returns: true
isEmpty(new Map()); // Returns: true

Check if the value is a Function

isFunction
Parameters
param (any) Value to be checked
Returns
boolean: Returns true when the param is a Function

Check if the users browser is a version of Internet Explorer

isIE(ua: any): (boolean | number)
Parameters
ua (any) The users user-agent string.
Returns
(boolean | number): Returns the version number (10, 11, 12) when the browser is IE, otherwise false
Example
// When using IE10
isIE(); // Returns: 10

// When using FireFox
isIE(); // Returns: false

TODO: It is not very good practice to return two different types of values from a function. Perhaps we can return `true` on IE and have a secondary function to get the version?

Check if a value is an integer

isInteger(param: any): boolean
Parameters
param (any) Value that will be checked if it is an integer
Returns
boolean: Returns true when param is a integer otherwise false
Example
isInteger(17); // Returns: true
isInteger(0xFF); // Returns: true
isInteger(-17); // Returns: true
isInteger(2e-3); // Returns: false
isIterable(checkForIterator: any): boolean
Parameters
checkForIterator (any) The value that should be checked
Returns
boolean: Returns true when the value is iterable, otherwise false.
Example
isIterable('My value'); // Returns:  true
isIterable([1, 2, 3]); // Returns: true
isIterable({}); // Returns: false
let iterableObject = {
    [Symbol.iterator]: () => {}
};
isIterable(iterableObject); // Returns: true

Check if a value is a number

isNumber(param: any): boolean
Parameters
param (any) Value that will be checked if it is a number
Returns
boolean: Returns true when param is a Number otherwise false

Check if a value is numeric

isNumeric(param: any): boolean
Parameters
param (any) Value to be checked
Returns
boolean: Returns true when the param is a numeric value

Check if the value is an Object

isObject(param: any): boolean
Parameters
param (any) Value to be checked
Returns
boolean: Returns true when the param is an Object

Check if a value is a primitive (string, number, boolean)

isPrimitive(param: any): boolean
Parameters
param (any) Value to be checked for a primitive type definition.
Returns
boolean: Returns true if the value is a primitive, otherwise false

Check if the value is a String

isString
Parameters
param (any) Value to be checked
Returns
boolean: Returns true when the param is a String

numberConstrain

src/numberConstrain.js

Constrains the value between the passed min and max

numberConstrain(number: Number, min: Number, max: Number): (Number | any)
Parameters
number (Number) The value to be constrained
min (Number) The minumum number that the value should be within
max (Number) The maximum number that the value should be within
Returns
(Number | any): The resulting number

Returns the number of decimal places in a number

numberDecimals(number: Number): Number
Parameters
number (Number) The number to be checked
Returns
Number: The number of decimal places in the given number

Check if a number is finite

numberIsFinite
Parameters
param (any) Number to be checked
Returns
boolean: Returns true when the param is a finite number

Fixes the number to a certain amount of decimals

numberToFixed
Parameters
value (Number) The value to apply the function to
precision (Number = 0) The amount of decimal digits to fix to
Returns
String: The "fixed" number with the specified amount of decimals

Applies properties from "config" if they are undefined in "object.

objectApplyIf(object: Object, config: Object): Object
Parameters
object (Object) The object to receive properties
config (Object) The object to pass properties
Returns
Object: The object that may have received properties
Example
let object = {id: 1}
const config = {id: 2, name: 'Name'}
objectApplyIf(object, config); // returns: {id: 1, name: 'Name'}

Applies properties from "config" if they are undefined in "object.

cleanObject(object: Object, isEmptyFn: Function): Object
Parameters
object (Object) The object to be cleaned
isEmptyFn (Function = isEmpty) Optional isEmpty function
Returns
Object: A new cleaned object
Example
let object = {id: 1, name: undefined}
objectClean(object); // returns: {id: 1}

stringReplaceAll

src/stringReplaceAll.js

Replace all occurrences of the matchValue within the str parameter.

stringReplaceAll(str: string, matchValue: string, replaceValue: (string | function), ignore: boolean): (XML | void | string | any)
Parameters
str (string) The string to operate on
matchValue (string) The value to match on
replaceValue ((string | function)) The value to replace the matches with
ignore (boolean) Case sensitivity ignore flag. Pass true to ignore case. (Defaults to false )
Returns
(XML | void | string | any): The resulting string.

Removes all spaces before first after last text char.

stringTrim(str: string): string
Parameters
str (string) The string to operate on
Returns
string: The resulting string.

Generates a universally unique identifier

uuid(): string
Returns
string: The uuid