From 2befaa42a8f067cc3a0d92b0011f691c1062868c Mon Sep 17 00:00:00 2001 From: Hetarth Shah Date: Sun, 28 Aug 2022 17:28:37 +0530 Subject: [PATCH] Major changes v3 - Migrated from funtion based to a class based approach Minor changes - Updated the README.md according to new changes - added two new helper methods - node version changes in workflow file --- .github/workflows/release-package.yml | 4 +- README.md | 24 ++- index.js | 201 ++++++++++++++------------ package.json | 2 +- 4 files changed, 122 insertions(+), 109 deletions(-) diff --git a/.github/workflows/release-package.yml b/.github/workflows/release-package.yml index a476d69..4d464b0 100644 --- a/.github/workflows/release-package.yml +++ b/.github/workflows/release-package.yml @@ -11,7 +11,7 @@ jobs: - uses: actions/checkout@v3 - uses: actions/setup-node@v3 with: - node-version: 12 + node-version: "16.x" - run: npm ci - run: npm test @@ -25,7 +25,7 @@ jobs: - uses: actions/checkout@v3 - uses: actions/setup-node@v3 with: - node-version: 12 + node-version: "16.x" registry-url: https://npm.pkg.github.com/ - run: npm ci - run: npm publish diff --git a/README.md b/README.md index 1f97fcf..b081d79 100644 --- a/README.md +++ b/README.md @@ -15,32 +15,28 @@ In your `package.json` add the following, `"type": "module"`. ### is_array() ```js -import { is_array, object_to_array ,search_in_array} from "@hetarth02/js-array-helpers"; +import Helper from "@hetarth02/js-array-helpers"; let arr = [1, 2]; -console.log(is_array(arr)); // true +console.log(Helper.is_array(arr)); // true ``` ### object_to_array ```js - const objectX = { - 0:"Apple", - 1:"Microsoft", - 2:"Google" - } +const objectX = { + 0: "Apple", + 1: "Microsoft", + 2: "Google" +} -console.log(object_to_array(objectX)) // [ 'Apple', 'Microsoft', 'Google' ] - +console.log(Helper.object_to_array(objectX)) // ['Apple', 'Microsoft', 'Google'] ``` ### search_in_array ```js - -const mang = [ 'Microsoft','apple','netflix','Google' ] +const mang = ['Microsoft', 'apple', 'netflix', 'Google'] -const result = search_in_array("app",mang); +const result = Helper.search_in_array("app", mang); console.log(result); // ['apple'] - - ``` diff --git a/index.js b/index.js index bb73fe6..cd8bc00 100644 --- a/index.js +++ b/index.js @@ -1,111 +1,128 @@ -// To throw error in case of invalid datatype. -function _throw() { - throw new TypeError("Must be a valid array!"); -} - -// To check if arr is array. -function is_array(arr) { - return Array.isArray(arr) ? true : _throw(); -} +class Helper { + // To throw error in case of invalid datatype. + static _throw() { + throw new TypeError("Must be a valid array!"); + } -// To get the head or first element of the array. -function head(arr) { - is_array(arr); - return arr[0]; -} + // To check if arr is array. + static is_array(arr) { + return Array.isArray(arr) ? true : this._throw(); + } -// 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 get the head or first element of the array. + static head(arr) { + this.is_array(arr); + return arr[0]; + } -// To check the existence of an element inside the array. -function in_array(arr, value) { - is_array(arr); - return arr.includes(value); -} + // 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 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!"); + // To check the existence of an element inside the array. + static in_array(arr, value) { + this.is_array(arr); + return arr.includes(value); } - let length = arr.length; - chunk_size = Math.abs(Math.round(chunk_size)); + // 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!"); + } - 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)); + 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; } - arr = modified_array; + } + + // To filter out arrays by removing nullish values. + static array_filter(arr) { + this.is_array(arr); + arr = arr.filter((e) => { + return e === 0 || e; + }); return arr; } -} -// To filter out arrays based on datatypes. -function array_filter(arr) { - is_array(arr); - arr = arr.filter((e) => { - return e === 0 || e; - }); - 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 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; + // 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 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); + } } - }); - return freq_obj; -} + return indexes; + } -// 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 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 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); - } + // Get sum of array + static array_sum(arr) { + this.is_array(arr); + return arr.reduce((prev, curr) => (isNaN(curr) ? 0 : prev + curr), 0); } - return indexes; -} -function search_in_array(query,array) { - return array.filter(item => item.toLowerCase().search(query.toLowerCase()) !== -1) + // 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)); + } + } else { + throw new TypeError("Input parameters not valid!"); + } + } } -export { - head, - tail, - is_array, - in_array, - array_chunk, - array_filter, - array_frequency, - object_to_array, - get_all_indexes, - search_in_array, -}; +export default Helper; diff --git a/package.json b/package.json index 78d0b45..892d694 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@hetarth02/js-array-helpers", - "version": "2.2.3", + "version": "3.2.3", "description": "Array Helper functions.", "main": "index.js", "type": "module",