From 14f1d2e462b7da62a5e255d4fcc7e4bf2bf93fc9 Mon Sep 17 00:00:00 2001 From: Hetarth Shah Date: Sat, 1 Oct 2022 15:27:53 +0530 Subject: [PATCH] Major Changes: - Reverted back to function based system from Class based system Minor Changes - Updated README.md --- README.md | 8 +- index.js | 277 ++++++++++++++++++++++++++++----------------------- package.json | 2 +- 3 files changed, 160 insertions(+), 127 deletions(-) diff --git a/README.md b/README.md index b081d79..918e283 100644 --- a/README.md +++ b/README.md @@ -15,10 +15,10 @@ In your `package.json` add the following, `"type": "module"`. ### is_array() ```js -import Helper from "@hetarth02/js-array-helpers"; +import { is_array } from "@hetarth02/js-array-helpers"; let arr = [1, 2]; -console.log(Helper.is_array(arr)); // true +console.log(is_array(arr)); // true ``` ### object_to_array @@ -29,14 +29,14 @@ const objectX = { 2: "Google" } -console.log(Helper.object_to_array(objectX)) // ['Apple', 'Microsoft', 'Google'] +console.log(object_to_array(objectX)) // ['Apple', 'Microsoft', 'Google'] ``` ### search_in_array ```js const mang = ['Microsoft', 'apple', 'netflix', 'Google'] -const result = Helper.search_in_array("app", mang); +const result = search_in_array("app", mang); console.log(result); // ['apple'] ``` diff --git a/index.js b/index.js index 2194b38..4264f28 100644 --- a/index.js +++ b/index.js @@ -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, +}; diff --git a/package.json b/package.json index 892d694..1321268 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@hetarth02/js-array-helpers", - "version": "3.2.3", + "version": "4.0.0", "description": "Array Helper functions.", "main": "index.js", "type": "module",