-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Reverted back to function based system from Class based system Minor Changes - Updated README.md
- Loading branch information
Showing
3 changed files
with
160 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,148 +1,181 @@ | ||
class Helper { | ||
// To throw error in case of invalid datatype. | ||
static _throw() { | ||
throw new TypeError("Must be a valid array!"); | ||
} | ||
/** | ||
* To throw error in case of invalid datatype. | ||
* | ||
* @returns TypeError | ||
*/ | ||
function _throw() { | ||
throw new TypeError("Must be a valid array!"); | ||
} | ||
|
||
// To check if arr is array. | ||
static is_array(arr) { | ||
return Array.isArray(arr) ? true : this._throw(); | ||
} | ||
/** | ||
* To check if arr is array. | ||
* | ||
* @param array arr | ||
* @returns bool | TypeError | ||
*/ | ||
function is_array(arr) { | ||
return Array.isArray(arr) ? true : _throw(); | ||
} | ||
|
||
// To check if arr has only nums. | ||
static is_num_array(arr) { | ||
var a = arr.reduce(function(result, val) { | ||
return result && typeof val === 'number'; | ||
}, true); | ||
if (a == false) { | ||
throw new TypeError("Must be an array of numbers!") | ||
} | ||
// To check if arr has only nums. | ||
function is_num_array(arr) { | ||
var a = arr.reduce(function (result, val) { | ||
return result && typeof val === "number"; | ||
}, true); | ||
if (a == false) { | ||
throw new TypeError("Must be an array of numbers!"); | ||
} | ||
} | ||
|
||
// To get the head or first element of the array. | ||
static head(arr) { | ||
this.is_array(arr); | ||
return arr[0]; | ||
} | ||
// To get the head or first element of the array. | ||
function head(arr) { | ||
is_array(arr); | ||
return arr[0]; | ||
} | ||
|
||
// To get the tail last element of the array. | ||
static tail(arr) { | ||
this.is_array(arr); | ||
let element = arr.pop(); | ||
arr.push(element); | ||
return element; | ||
} | ||
// To get the tail last element of the array. | ||
function tail(arr) { | ||
is_array(arr); | ||
let element = arr.pop(); | ||
arr.push(element); | ||
return element; | ||
} | ||
|
||
// To check the existence of an element inside the array. | ||
static in_array(arr, value) { | ||
this.is_array(arr); | ||
return arr.includes(value); | ||
} | ||
// To check the existence of an element inside the array. | ||
function in_array(arr, value) { | ||
is_array(arr); | ||
return arr.includes(value); | ||
} | ||
|
||
// To split arrays into fixed size chunks. | ||
static array_chunk(arr, chunk_size) { | ||
this.is_array(arr); | ||
if (typeof chunk_size != "number") { | ||
throw new TypeError("chunk_size should be a integer!"); | ||
} | ||
// To split arrays into fixed size chunks. | ||
function array_chunk(arr, chunk_size) { | ||
is_array(arr); | ||
if (typeof chunk_size != "number") { | ||
throw new TypeError("chunk_size should be a integer!"); | ||
} | ||
|
||
let length = arr.length; | ||
chunk_size = Math.abs(Math.round(chunk_size)); | ||
let length = arr.length; | ||
chunk_size = Math.abs(Math.round(chunk_size)); | ||
|
||
if (chunk_size > length || [0, null, NaN].includes(chunk_size)) { | ||
return arr; | ||
} else { | ||
let modified_array = []; | ||
for (let index = 0; index < length; index += chunk_size) { | ||
modified_array.push(arr.slice(index, index + chunk_size)); | ||
} | ||
arr = modified_array; | ||
return arr; | ||
if (chunk_size > length || [0, null, NaN].includes(chunk_size)) { | ||
return arr; | ||
} else { | ||
let modified_array = []; | ||
for (let index = 0; index < length; index += chunk_size) { | ||
modified_array.push(arr.slice(index, index + chunk_size)); | ||
} | ||
} | ||
|
||
// To filter out arrays by removing nullish values. | ||
static array_filter(arr) { | ||
this.is_array(arr); | ||
arr = arr.filter((e) => { | ||
return e === 0 || e; | ||
}); | ||
arr = modified_array; | ||
return arr; | ||
} | ||
} | ||
|
||
// To get the frequency of occurence of each unique element inside the array. | ||
static array_frequency(arr) { | ||
this.is_array(arr); | ||
let freq_obj = {}; | ||
arr.forEach((value) => { | ||
if (value in freq_obj) { | ||
freq_obj[value] += 1; | ||
} else { | ||
freq_obj[value] = 1; | ||
} | ||
}); | ||
return freq_obj; | ||
} | ||
// To filter out arrays by removing nullish values. | ||
function array_filter(arr) { | ||
is_array(arr); | ||
arr = arr.filter((e) => { | ||
return e === 0 || e; | ||
}); | ||
return arr; | ||
} | ||
|
||
// To convert Objects into Arrays. | ||
static object_to_array(obj) { | ||
let temp = []; | ||
const entries = Object.entries(obj); | ||
entries.forEach((ent) => temp.push(ent[1])); | ||
return temp; | ||
} | ||
// To get the frequency of occurence of each unique element inside the array. | ||
function array_frequency(arr) { | ||
is_array(arr); | ||
let freq_obj = {}; | ||
arr.forEach((value) => { | ||
if (value in freq_obj) { | ||
freq_obj[value] += 1; | ||
} else { | ||
freq_obj[value] = 1; | ||
} | ||
}); | ||
return freq_obj; | ||
} | ||
|
||
// To get indexes of all occurences of an element inside an array. | ||
static get_all_indexes(arr, val) { | ||
this.is_array(arr); | ||
var indexes = []; | ||
for (let i = 0; i < arr.length; i++) { | ||
if (arr[i] === val) { | ||
indexes.push(i); | ||
} | ||
// To convert Objects into Arrays. | ||
function object_to_array(obj) { | ||
let temp = []; | ||
const entries = Object.entries(obj); | ||
entries.forEach((ent) => temp.push(ent[1])); | ||
return temp; | ||
} | ||
|
||
// To get indexes of all occurences of an element inside an array. | ||
function get_all_indexes(arr, val) { | ||
is_array(arr); | ||
var indexes = []; | ||
for (let i = 0; i < arr.length; i++) { | ||
if (arr[i] === val) { | ||
indexes.push(i); | ||
} | ||
return indexes; | ||
} | ||
return indexes; | ||
} | ||
|
||
// To check if a substr exists within an array of strings | ||
static search_in_array(query, arr) { | ||
this.is_array(arr); | ||
return arr.filter((item) => item.toLowerCase().search(query.toLowerCase()) !== -1); | ||
} | ||
// To check if a substr exists within an array of strings | ||
function search_in_array(query, arr) { | ||
is_array(arr); | ||
return arr.filter((item) => item.toLowerCase().search(query.toLowerCase()) !== -1); | ||
} | ||
|
||
// Get sum of array | ||
static array_sum(arr) { | ||
this.is_array(arr); | ||
return arr.reduce((prev, curr) => (isNaN(curr) ? 0 : prev + curr), 0); | ||
} | ||
// Get sum of array | ||
function array_sum(arr) { | ||
is_array(arr); | ||
return arr.reduce((prev, curr) => (isNaN(curr) ? 0 : prev + curr), 0); | ||
} | ||
|
||
// Get sum of a subarray | ||
static array_slice_sum(arr, slice_start = 0, slice_end = arr.length, includes = true) { | ||
this.is_array(arr); | ||
if ( | ||
Number.isInteger(slice_start) && | ||
Number.isInteger(slice_end) && | ||
typeof includes === "boolean" | ||
) { | ||
if (includes) { | ||
return this.array_sum(arr.slice(slice_start, slice_end + 1)); | ||
} else { | ||
return this.array_sum(arr.slice(slice_start, slice_end)); | ||
} | ||
// Get sum of a subarray | ||
function array_slice_sum(arr, slice_start = 0, slice_end = arr.length, includes = true) { | ||
is_array(arr); | ||
if ( | ||
Number.isInteger(slice_start) && | ||
Number.isInteger(slice_end) && | ||
typeof includes === "boolean" | ||
) { | ||
if (includes) { | ||
return array_sum(arr.slice(slice_start, slice_end + 1)); | ||
} else { | ||
throw new TypeError("Input parameters not valid!"); | ||
return array_sum(arr.slice(slice_start, slice_end)); | ||
} | ||
} else { | ||
throw new TypeError("Input parameters not valid!"); | ||
} | ||
//To sort numeric arrays ascending and descending | ||
static sort_nums(arr, reverse = false) { | ||
this.is_array(arr); | ||
this.is_num_array(arr); | ||
if (reverse) { | ||
return arr.sort(function(a, b){return b - a}); | ||
} else { | ||
return arr.sort(function(a, b){return a - b}); | ||
} | ||
} | ||
|
||
/** | ||
* To sort numeric arrays ascending and descending | ||
* | ||
* @param array arr | ||
* @param bool reverse | ||
* @returns array | ||
*/ | ||
function sort_nums(arr, reverse = false) { | ||
is_array(arr); | ||
is_num_array(arr); | ||
if (reverse) { | ||
return arr.sort(function (a, b) { | ||
return b - a; | ||
}); | ||
} else { | ||
return arr.sort(function (a, b) { | ||
return a - b; | ||
}); | ||
} | ||
} | ||
|
||
export default Helper; | ||
export { | ||
is_array, | ||
is_num_array, | ||
head, | ||
tail, | ||
in_array, | ||
array_chunk, | ||
array_filter, | ||
array_frequency, | ||
object_to_array, | ||
get_all_indexes, | ||
search_in_array, | ||
array_sum, | ||
array_slice_sum, | ||
sort_nums, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters