Skip to content

Commit

Permalink
Major Changes:
Browse files Browse the repository at this point in the history
- Reverted back to function based system from Class based system

Minor Changes

- Updated README.md
  • Loading branch information
Hetarth02 committed Oct 1, 2022
1 parent 937cc1c commit 14f1d2e
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 127 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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']
```
277 changes: 155 additions & 122 deletions index.js
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,
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 14f1d2e

Please sign in to comment.