best-library
This library provides a set of functions that facilitate the work in Javascript. Library development continues.
Available functions
- randomNum
- randomArray
- createArray
- arrayOperations
- lastElem
- sortArray
- bubbleSort
- selectionSort
- insertionSort
- binarySearch
- interpolationSearch
- factorial
- fibonacci
- RLE
- upperFirst
- measureTime
Installation
$ npm i best-library
In Node.js:
const bl = ;
bl.randomNum([min = 0], max)
The function returns a random number in the range from the first argument to the second argument
Arguments
1. [min = 0] (number): The lower bound of an interval
2. max (number): The upper bound of an interval
Returns
(number): Returns a random number
##### Examples
const number = bl;console; // 7 const number = bl;console; // 5
bl.randomArray(size, min, max, [unique = false])
The function returns an array of random numbers
Arguments
1. size (number): The Size of an array
2. min (number): The lower bound
3. max (number): The upper bound
4. unique = false (boolean): If the variable === true, the function will return an array of unique numbers
Returns
(array): Returns an array of random numbers
Examples
const array = bl;console; // (10) [7, 0, 9, 9, 3, 10, 4, 3, 6, 6] const array = bl;console // (10) [14, 4, 1, 7, 11, 15, 12, 0, 9, 6]
bl.createArray(length, element)
Create an array filled with identical elements
Arguments
1. length (number): Array length
2. element (string|number|boolean|underfined|null|object): Array element
Returns
(array): Returns array
Examples
const array = bl;console; // (3) [String, String, String]
bl.arrayOperations(array1, array2, operation)
The function performs mathematical operations on arrays. The result is written to the first array.
Arguments
1. array1 (array): The first array to perform operation
2. array2 (array): The second array to perform operation
3. operation (string): The operation that is performed on arrays
Returns
The function returns nothing
Examples
const array1 = 4 5 0 1 7;const array2 = 9 4 8 1 2;bl;console; // (5) [36, 20, 0, 1, 14]
bl.lastElem(array)
Find last array element
Arguments
1. array (array): The array to find last element
Returns
(all types): Returns last array element
Examples
const array = 4 5 0 1 7;const elem = bl;console; // 7
bl.sortArray(array)
Sort array
Arguments
1. array (array): The array to sort
Examples
const array = 5 1 3 0 3 5 9 8 1 2;bl;console; // (10) [0, 1, 1, 2, 3, 3, 5, 5, 8, 9]
bl.bubbleSort(array)
Sort the array by bubble sort
Arguments
1. array (array): The array to sort
Examples
const array = -1 5 8 0 -6 -5 3 4 1 0;bl;console; // (10) [ -6, -5, -1, 0, 0, 1, 3, 4, 5, 8 ]
bl.selectionSort(array)
Sort the array by selection sort
Arguments
1. array (array): The array to sort
Examples
const array = 5 1 -3 -3 3 -5 6 8 1 0;bl;console; // (10) [ -5, -3, -3, 0, 1, 1, 3, 5, 6, 8 ]
bl.insertionSort(array)
Sort the array by insertion sort
Arguments
1. array (array): The array to sort
Examples
const array = 5 1 -3 -3 3 -5 6 8 1 0;bl;console; // (10) [ -5, -3, -3, 0, 1, 1, 3, 5, 6, 8 ]
bl.binarySearch(array, element)
Find element index
Arguments
1. element (number): The element to find index
2. array (array): The sorted array to find element index
Returns
The function returns element index or -1
Examples
const array = -4 0 8 9 11;const index = bl;console; // 1
bl.interpolationSearch(array, element)
Find element index
Arguments
1. element (number): The element to find index
2. array (array): The sorted array to find element index
Returns
The function returns element index or -1
Examples
const array = 3 5 9 11 15 98 505;const index = bl;console; // 2
bl.factorial(number)
Calculate the factorial of numbers
Arguments
1. number (number): The number to calculate factorial
Returns
(number): Returns the factorial of a number
Examples
const num = bl;console; // 120
bl.fibonacci(number)
Calculate the fibonacci number
Arguments
1. number (number): The number to calculate fibonacci number
Returns
(number): Returns the nth fibonacci number
Examples
const num = bl;console; // 1597
bl.RLE(str)
Compress string
Arguments
1. str (string): The string to compress
Returns
(string): Returns compressed string
Examples
const str = 'AAAABBBCCXYZDDDDEEEFFFAAAAAABBBBB';const compressedStr = ;console; // A4B3C2X1Y1Z1D4E3F3A6B5
bl.upperFirst(string)
Translate to upper case first string letter
Arguments
1. string (string): The string to modify
Returns
(string): Returns the modify string
Examples
const str = 'hello, world!'console; // Hello, World!;
This function workable only in browser!
bl.measureTime(callback, [args1], [args2], ...)
Measure function execution time
Arguments
1. callback (function): The function whose time is to be measured
2. [args1], [args2], ... : Callback function arguments
Returns
(object|number): If the callback function returns a value,
the measureTime function returns object {time: number, answer: *}
else returns only function execuite time
Examples
const answer = ;console; // 0.14999999984866008;
const answer = ;console; // {time: 1181.5399999995861, answer: 63245986}