js-utilities-libs
v1.0.5
Published
The `js-utilities-libs` package is a JavaScript library that provides a collection of utility functions for common programming tasks. It includes functions for string manipulation, array operations, date and time handling, mathematical calculations, objec
Readme
js-utilities-libs
Description
The js-utilities-libs package is a JavaScript library that provides a collection of utility functions for common programming tasks. It includes functions for string manipulation, array operations, date and time handling, mathematical calculations, object manipulation, data validation, file and path operations, network utilities, data formatting, and error handling. Each function is designed to be simple, efficient, and versatile, making it easy to use in a wide range of projects.
Features
- String Manipulation
- Array Operations
- Date and Time Handling
- Mathematical Calculations
- Object Manipulation
- Data Validation
- File and Path Operations
- Network Utilities
- Data Formatting
- Error Handling
Installation
npm install js-utilities-libsUsage
String Manipulation
const {
trim,
toCamelCase,
toSnakeCase,
contains,
} = require("js-utilities-libs");
console.log(trim(" example ")); // Output: 'example'
console.log(toCamelCase("hello_world")); // Output: 'helloWorld'
console.log(toSnakeCase("helloWorld")); // Output: 'hello_world'
console.log(contains("example", "amp")); // Output: falseArray Operations
const {
sortArray,
filterEvenNumbers,
mapToUppercase,
sumArray,
} = require("js-utilities-libs");
console.log(sortArray([3, 1, 5, 2, 4])); // Output: [1, 2, 3, 4, 5]
console.log(filterEvenNumbers([1, 2, 3, 4, 5])); // Output: [2, 4]
console.log(mapToUppercase(["hello", "world"])); // Output: ['HELLO', 'WORLD']
console.log(sumArray([1, 2, 3, 4, 5])); // Output: 15Date and Time
const { formatDate, dateDiffInDays } = require("js-utilities-libs");
const today = new Date();
const tomorrow = new Date(today);
tomorrow.setDate(today.getDate() + 1);
console.log(formatDate(today)); // Output: 'YYYY-MM-DD'
console.log(dateDiffInDays(today, tomorrow)); // Output: 1Math Functions
const { getRandomNumber, calculateAverage } = require("js-utilities-libs");
console.log(getRandomNumber(1, 10)); // Output: Random number between 1 and 10
console.log(calculateAverage([1, 2, 3, 4, 5])); // Output: 3Object Manipulation
const { mergeObjects, cloneObject, hasProperty } = require("js-utilities-libs");
const obj1 = { a: 1 };
const obj2 = { b: 2 };
console.log(mergeObjects(obj1, obj2)); // Output: { a: 1, b: 2 }
console.log(cloneObject(obj1)); // Output: { a: 1 }
console.log(hasProperty(obj1, "a")); // Output: trueValidation
const { validateEmail, validateURL } = require("js-utilities-libs");
console.log(validateEmail("[email protected]")); // Output: true
console.log(validateURL("https://example.com")); // Output: trueFile and Path Operations
const { readFile, writeFile } = require("js-utilities-libs");
readFile("example.txt")
.then((data) => {
console.log(data); // Output: Contents of the file
})
.catch((err) => {
console.error(err);
});
writeFile("example.txt", "Hello, world!")
.then(() => {
console.log("File written successfully!");
})
.catch((err) => {
console.error(err);
});Network Utilities
const { fetchData } = require("js-utilities-libs");
fetchData("https://jsonplaceholder.typicode.com/posts/1")
.then((data) => {
console.log(data); // Output: JSON data from the API
})
.catch((err) => {
console.error(err);
});Data Formatting
const { objectToJson, jsonToObject } = require("js-utilities-libs");
const obj = { key: "value" };
const jsonString = objectToJson(obj);
console.log(jsonString); // Output: '{"key":"value"}'
console.log(jsonToObject(jsonString)); // Output: { key: 'value' }Error Handling
const { logError, formatErrorMessage } = require("js-utilities-libs");
try {
// Code that may throw an error
throw new Error("An error occurred");
} catch (error) {
logError(error); // Output: [timestamp] Error: An error occurred
console.log(formatErrorMessage(error)); // Output: An error occurred: An error occurred
}License
js-utilities-libs is released under the MIT License.
Version History
1.0.0
Author
Name :Deepak Sharma | Email : [email protected] Name :Amit Bhandari | Email : [email protected]
