oh-my-lib v0.5.0
Oh My Lib
This is a library of Javascript functions created with the purpose of practicing my Javascript skills and putting into practice some less used techniques. Therefore, not all functions are done in a way that has the best possible performance because they may have been done by focusing on a specific approach for study purposes.
Functions categories:
Math functions
max
Called to biggest between two passed values.
Input: (number/string, number/string)
Output: number/string
const result = max(5, 2);
//result == 5
min
Called to smaller between two passed values.
Input: (number/string, number/string)
Output: number/string
const result = min(5, 2);
//result == 2
sumAllInRange
Returns the sum of all integers between the first and the second parameters. If passed a third parameter, the result the result will be the sum of numbers starting from the first parameter but steping the passed value until reach the second parameter.
Input: (integer, integer, integer=1)
Output: number
const result = sumAllInRange(3, 99, 4);
//result == 1275
Array functions
first
Return the first element of the array
Input: (array)
Output: number/string
const result = first([1,2,3]);
//result == 1
tail
Return the the array without the first element
Input: (array)
Output: array
const result = tail([1,2,3]);
//result == [2,3]
last
Return the last element of the array
Input: (array)
Output: number/string
const result = last([1,2,3]);
//result == 3
init
Return the the array without the last element
Input: (array)
Output: array
const result = init([1,2,3]);
//result == [1,2]
each
Called to execute some passed function over all elements of a passed array without return any result.
Input: (array, function)
Output: undefined
each([1,2,3], (value) => { console.log(value) });
filter
Returns as result a list with the elements that passed the test defined by the passed function.
Input: (array, function)
Output: array
filter([1,2,3], (value) => { return value > 1 });
//result == [2,3]
maxOfList
Called to get the biggest value between the values of a passed array.
Input: (array)
Output: number/string
const result = lib.maxOfList([5,10,2,9]);
//result == 10
maxOccurrence
Called to get the most frequent element of the array. If more than one element have the same frenquency, the first element to reach that frequency will be returned.
Input: (array)
Output: number/string
const result = lib.maxOccurrence(['a','a','c','b','c','c']);
//result == 'c'
trueValues
Returns a copy of the passed array without the falsy values. In Javascript, the falsy values are false, null, 0, "", undefined and NaN.
Input: (array)
Output: array
const result = lib.trueValues(['c', 0, null, 1]);
//result == ['c', 1]
except
Returns a copy of the passed array without the passed values.
Input: (array, number/string...)
Output: array
const result = lib.except(['a','a','c','b','c','c'], 'b', 'c');
//result == ['a', 'a']
Collection functions
extractProperty
Called to get the value of a property with the given name of all elements of a passed larrayist.
Input: (array, string)
Output: array
const list = [
{ a: 1, b: 2, c: 3 },
{ a: 4, b: 5, c: 6 },
{ a: 7, b: 8, c: 9 },
{ a: 10, b: 11, c: 12 },
{ a: 13, b: 14, c: 15 }
];
const result = lib.extractProperty(list, 'b');
//result == [2,5,8,11,14]
maxOfProperty
Called to get the biggest value of a passed property from a passed array of objects.
Input: (array, string)
Output: number/string
const result = maxOfProperty([
{ age: 20 },
{ age: 25 },
{ age: 10 },
{ age: 40 }
], 'age');
//result == 40