core-helper-packages
v1.3.0
Published
core-helper-packages is a utility library designed to simplify common programming tasks in JavaScript. It includes a comprehensive set of functions for working with arrays and strings, making your development process faster and more efficient
Downloads
10
Readme
Certainly! Here is a README.md
formatted with Markdown language, tailored for your core-helper-packages
npm package:
# Core Helper Packages
A collection of utility functions for common array and string operations in JavaScript.
## Installation
To use this package, install it via npm:
```bash
npm install core-helper-packages
Usage
You can import the package into your project using CommonJS or ES Modules syntax:
CommonJS
const coreHelper = require('core-helper-packages');
ES Modules
import * as coreHelper from 'core-helper-packages';
API
String Utilities
isString(value)
Checks if the given value is a string.
- Parameters:
value
(any) - The value to check. - Returns:
boolean
-true
if the value is a string, otherwisefalse
.
Array Utilities
isArray(value)
Checks if the given value is an array.
- Parameters:
value
(any) - The value to check. - Returns:
boolean
-true
if the value is an array, otherwisefalse
.
isNull(value)
Checks if the given value is null
.
- Parameters:
value
(any) - The value to check. - Returns:
boolean
-true
if the value isnull
, otherwisefalse
.
isUndefined(value)
Checks if the given value is undefined
.
- Parameters:
value
(any) - The value to check. - Returns:
boolean
-true
if the value isundefined
, otherwisefalse
.
isNullOrUndefined(value)
Checks if the given value is either null
or undefined
.
- Parameters:
value
(any) - The value to check. - Returns:
boolean
-true
if the value isnull
orundefined
, otherwisefalse
.
isArrayEmpty(arr)
Checks if an array is empty.
- Parameters:
arr
(Array) - The array to check. - Returns:
boolean
-true
if the array is empty, otherwisefalse
. - Throws:
TypeError
- If the input is not an array.
getArrayLength(arr)
Gets the length of an array.
- Parameters:
arr
(Array) - The array to get the length of. - Returns:
number
- The length of the array. - Throws:
TypeError
- If the input is not an array.
firstElement(arr)
Retrieves the first element of an array.
- Parameters:
arr
(Array) - The array to get the first element from. - Returns: The first element of the array.
- Throws:
TypeError
- If the input is not an array.
lastElement(arr)
Retrieves the last element of an array.
- Parameters:
arr
(Array) - The array to get the last element from. - Returns: The last element of the array, or
undefined
if the array is empty. - Throws:
TypeError
- If the input is not an array.
pushElement(arr, element)
Adds an element to the end of an array.
- Parameters:
arr
(Array) - The array to modify.element
(any) - The element to add.
- Returns: The array with the new element added.
- Throws:
TypeError
- If the first argument is not an array.
popElement(arr)
Removes the last element from an array.
- Parameters:
arr
(Array) - The array to modify. - Returns: The array with the last element removed.
- Throws:
TypeError
- If the input is not an array.
sumArray(arr)
Calculates the sum of all numeric values in an array.
- Parameters:
arr
(Array) - The array containing numeric values. - Returns:
number
- The sum of the numeric values in the array. - Throws:
TypeError
- If the input is not an array or contains non-numeric values.
averageArray(arr)
Calculates the average of all numeric values in an array.
- Parameters:
arr
(Array) - The array containing numeric values. - Returns:
number
- The average of the numeric values in the array. - Throws:
TypeError
- If the input is not an array or contains non-numeric values.
maxArray(arr)
Finds the maximum value in an array.
- Parameters:
arr
(Array) - The array to search. - Returns:
number
- The maximum value in the array. - Throws:
TypeError
- If the input is not a non-empty array.
minArray(arr)
Finds the minimum value in an array.
- Parameters:
arr
(Array) - The array to search. - Returns:
number
- The minimum value in the array. - Throws:
TypeError
- If the input is not a non-empty array.
mapArray(arr, callback)
Applies a function to each element of the array and returns a new array.
- Parameters:
arr
(Array) - The array to map over.callback
(Function) - The function to apply to each element.
- Returns: A new array with each element transformed by the callback function.
- Throws:
TypeError
- If the first argument is not an array or the second argument is not a function.
filterArray(arr, predicate)
Filters elements of the array based on a predicate function and returns a new array.
- Parameters:
arr
(Array) - The array to filter.predicate
(Function) - The function to test each element.
- Returns: A new array with elements that pass the predicate function.
- Throws:
TypeError
- If the first argument is not an array or the second argument is not a function.
flatArray(arr)
Flattens a nested array into a single-level array.
- Parameters:
arr
(Array) - The nested array to flatten. - Returns: The flattened array.
- Throws:
TypeError
- If the input is not an array.
uniqueArray(arr)
Removes duplicate values from an array.
- Parameters:
arr
(Array) - The array from which to remove duplicates. - Returns: The array with duplicate values removed.
- Throws:
TypeError
- If the input is not an array.
Examples
Here are some example usages for each function:
const coreHelper = require('core-helper-packages');
// String Utilities
console.log(coreHelper.isString("hello")); // true
// Array Utilities
const arr = [1, 2, 3, 4];
console.log(coreHelper.isArray(arr)); // true
console.log(coreHelper.isArrayEmpty([])); // true
console.log(coreHelper.getArrayLength(arr)); // 4
console.log(coreHelper.firstElement(arr)); // 1
console.log(coreHelper.lastElement(arr)); // 4
console.log(coreHelper.pushElement(arr, 5)); // [1, 2, 3, 4, 5]
console.log(coreHelper.popElement(arr)); // [1, 2, 3, 4]
console.log(coreHelper.sumArray(arr)); // 10
console.log(coreHelper.averageArray(arr)); // 2.5
console.log(coreHelper.maxArray(arr)); // 4
console.log(coreHelper.minArray(arr)); // 1
console.log(coreHelper.mapArray(arr, x => x * 2)); // [2, 4, 6, 8]
console.log(coreHelper.filterArray(arr, x => x > 2)); // [3, 4]
console.log(coreHelper.flatArray([[1], [2, 3], [4]])); // [1, 2, 3, 4]
console.log(coreHelper.uniqueArray([1, 2, 2, 3, 4, 4])); // [1, 2, 3, 4]