my-dev-utils v1.0.1
My Dev Utils - A Simple Utility Library for Developers
My Dev Utils is a collection of simple utility functions for everyday tasks in JavaScript development. This library provides functions to handle tasks such as removing duplicates from arrays, formatting dates, generating unique identifiers, and more.
Installation
To install the library, you can use npm or yarn:
npm install my-dev-utils
or
yarn add my-dev-utils
Functions
1. Array Utilities
removeDuplicates(arr)
Removes duplicates from an array.
Parameters:
arr
(Array): The array from which duplicates will be removed.
Returns: A new array with unique values.
Example:
const { removeDuplicates } = require('my-dev-utils').arrayUtils;
const arr = [1, 2, 2, 3, 4, 4, 5];
console.log(removeDuplicates(arr)); // Output: [1, 2, 3, 4, 5]
groupBy(arr, key)
Groups an array of objects by a specific key.
Parameters:
arr
(Array): The array of objects to be grouped.key
(string): The key to group by.
Returns: An object where each key is the value of the specified key in the original objects, and the values are arrays of objects that share the same key.
Example:
const { groupBy } = require('my-dev-utils').arrayUtils;
const objects = [
{ id: 1, category: 'A' },
{ id: 2, category: 'B' },
{ id: 3, category: 'A' },
];
console.log(groupBy(objects, 'category'));
// Output: { A: [{ id: 1, category: 'A' }, { id: 3, category: 'A' }], B: [{ id: 2, category: 'B' }] }
2. Date Utilities
formatDate(date, formatString = "dd/MM/yyyy")
Formats a date using the given format string.
Parameters:
date
(Date | string): The date to be formatted.formatString
(string): The format string (default is "dd/MM/yyyy").
Returns: A string representing the formatted date.
Example:
const { formatDate } = require('my-dev-utils').dateUtils;
const formattedDate = formatDate(new Date(), 'yyyy-MM-dd');
console.log(formattedDate); // Example output: 2024-11-12
daysBetween(date1, date2)
Calculates the number of days between two dates.
Parameters:
date1
(Date | string): The first date.date2
(Date | string): The second date.
Returns: The number of days between the two dates.
Example:
const { daysBetween } = require('my-dev-utils').dateUtils;
const days = daysBetween('2024-11-10', '2024-11-12');
console.log(days); // Output: 2
3. UUID Utilities
generateUUID()
Generates a unique identifier (UUID).
- Returns: A string representing the generated UUID.
Example:
const { generateUUID } = require('my-dev-utils').uuidUtils;
const uuid = generateUUID();
console.log(uuid); // Output: 'e1b0f459-f56b-4d5e-b083-0813214e6b06'
generateAlphanumericId(length = 8)
Generates an alphanumeric identifier of a given length.
Parameters:
length
(number): The length of the identifier (default is 8).
Returns: A string representing the alphanumeric identifier.
Example:
const { generateAlphanumericId } = require('my-dev-utils').uuidUtils;
const id = generateAlphanumericId(10);
console.log(id); // Output: 'jX7V4BZs93'
4. String Utilities
capitalize(str)
Capitalizes the first letter of a string.
Parameters:
str
(string): The string to be capitalized.
Returns: The string with the first letter capitalized.
Example:
const { capitalize } = require('my-dev-utils').stringUtils;
const capitalized = capitalize("hello world");
console.log(capitalized); // Output: 'Hello world'
isPalindrome(str)
Checks if a string is a palindrome.
Parameters:
str
(string): The string to check.
Returns: A boolean value indicating whether the string is a palindrome.
Example:
const { isPalindrome } = require('my-dev-utils').stringUtils;
console.log(isPalindrome('madam')); // Output: true
console.log(isPalindrome('hello')); // Output: false
License
OPEN SOURCE PROJECT MAKE WITH ❤ BY DEKHIL OMRAN