0.2.16
Cleans the given array of empty values
(Array)
The array to be
cleaned
Array
:
The clean array
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
(Array)
The array to check for the item
(any)
The item to look for in the array
boolean
:
Returns true when the item is found, otherwise false
(Array)
(Array)
(boolean)
False returns A-B while true returns A-B concat B-A
Array
:
filter
can not be found on
array
. This generally happens when the array is
null
or
undefined
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
(any)
Value to transform to an array
Array
:
The resulting array
Insert a value into an array
(Array)
The array to insert the items into
(number)
The index where to insert the values
(any)
The item(s) to insert
Array
:
The resulting array with the inserted
items
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
(Array)
The array to be scanned
(any)
Array
:
The highest value
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
(Array)
The array to be scanned
(any)
Array
:
The lowest value
const sourceArray = [3,1,2];
arrayMax(sourceArray); // Returns: 1
arrayClean() // throws: Cannot read property 'length' of undefined
Array
:
propertyName
can not be found on an item in the
array
. This generally happens when a value in the array is
null
or
undefined
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.
array
:
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.
(Array)
The array where the values should be replaced
(number)
The index in the array where to start the replacement
(number)
The number of items to remove
(any)
The item(s) to be inserted
Array
:
The array with the replaced values
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))
Array
:
Returns the
param
transformed into an array, or an empty array of the value can not be transformed.
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.
(Array)
The array to create the array of uniques from
Array
:
The array containing the unique values
array
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'}]
Convert a camelCase word to its underscore_case equivalent
(string)
The string to transform.
string
:
The wordToTransform as a underscore separated string eg. camelCase -> camel_case
const word = 'HelloMyAmazingWorld';
camelCaseToUnderscores(word); // Returns: 'hello_my_amazing_world'
Create a clone a value
(any)
const person1 = {name: 'John'};
const person2 = clone(person1); // Returns {name: 'John'}
// but
person1 !== person2 // Returns: true
Check if the value is an Array
(any)
Value to be checked
boolean
:
Returns true when the
param
is an Array
isArray([]); // Returns: true
isArray({}); // Returns: false
isArray('Hello'); // Returns: false
Check if an array consists of strings
(Array)
The array to be checked for string values
boolean
:
True when the array contains just string otherwise false
isArrayOfStrings(['Mark', 'John']) // Returns: true
isArrayOfStrings([{name: 'Mark'}, {name: 'John'}]) // Returns: false
Check if a value is a boolean value true
or false
(any)
The value to check for boolean type
boolean
:
Returns
true
when the value is a boolean otherwise
false
Check if param
is defined.
(any)
The value to check
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
(any)
Value to check
(boolean)
When set to true an empty string will not be considered
empty
.
boolean
:
Returns
true
when the value is
empty
, otherwise false
isEmpty(''); // Returns: true
isEmpty('', true); // Returns: false
isEmpty(null); // Returns: true
isEmpty(new Map()); // Returns: true
Check if the value is a Function
(any)
Value to be checked
boolean
:
Returns true when the
param
is a Function
Check if the users browser is a version of Internet Explorer
(any)
The users user-agent string.
(boolean | number)
:
Returns the version number (10, 11, 12) when the browser is IE, otherwise
false
// 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
(any)
Value that will be checked if it is an integer
boolean
:
Returns
true
when param is a integer otherwise false
isInteger(17); // Returns: true
isInteger(0xFF); // Returns: true
isInteger(-17); // Returns: true
isInteger(2e-3); // Returns: false
(any)
The value that should be checked
boolean
:
Returns
true
when the value is iterable, otherwise false.
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
(any)
Value that will be checked if it is a number
boolean
:
Returns true when param is a Number otherwise false
Check if a value is numeric
(any)
Value to be checked
boolean
:
Returns true when the
param
is a numeric value
Check if the value is an Object
(any)
Value to be checked
boolean
:
Returns true when the
param
is an Object
Check if a value is a primitive (string, number, boolean)
(any)
Value to be checked for a primitive type definition.
boolean
:
Returns
true
if the value is a primitive, otherwise
false
Check if the value is a String
(any)
Value to be checked
boolean
:
Returns true when the
param
is a String
Constrains the value between the passed min and max
(Number)
The value to be constrained
(Number)
The minumum number that the value should be within
(Number)
The maximum number that the value should be within
(Number | any)
:
The resulting number
Returns the number of decimal places in a number
(Number)
The number to be checked
Number
:
The number of decimal places in the given number
Check if a number is finite
(any)
Number to be checked
boolean
:
Returns true when the
param
is a finite number
Fixes the number to a certain amount of decimals
(Number)
The value to apply the function to
(Number
= 0
)
The amount of decimal digits to fix to
String
:
The "fixed" number with the specified amount of decimals
Applies properties from "config" if they are undefined in "object.
Object
:
The object that may have received properties
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.
Object
:
A new cleaned object
let object = {id: 1, name: undefined}
objectClean(object); // returns: {id: 1}
Replace all occurrences of the matchValue
within the str
parameter.
(XML | void | string | any)
:
The resulting string.
Removes all spaces before first after last text char.
(string)
The string to operate on
string
:
The resulting string.
Generates a universally unique identifier
string
:
The uuid