@brmorillo/utils v9.1.1
@brmorillo/utils
Project Description
@brmorillo/utils is a TypeScript utility library designed to simplify common tasks by providing efficient and reusable implementations. It works seamlessly in both Node.js and browser environments.
Installation and Usage
Install
To add the library to your project, use:
npm install @brmorillo/utilsor if you use Yarn:
yarn add @brmorillo/utilsor with pnpm:
pnpm add @brmorillo/utilsUsage
Import the specific utilities you need:
import { ArrayUtils, MathUtils, StringUtils } from '@brmorillo/utils';
// Example usage
console.log(ArrayUtils.removeDuplicates({ array: [1, 2, 2, 3] })); // [1, 2, 3]
console.log(MathUtils.roundToDecimals({ value: 3.14159, decimals: 2 })); // 3.14
console.log(StringUtils.capitalizeFirstLetter({ input: 'hello' })); // "Hello"Or import the entire library:
import Utils from '@brmorillo/utils';
console.log(Utils.ArrayUtils.removeDuplicates({ array: [1, 2, 3, 3] })); // [1, 2, 3]
console.log(Utils.MathUtils.randomInRange({ min: 5, max: 10 })); // Random value between 5 and 10Project Features
Array Utility Functions
removeDuplicates: Removes duplicate values from an array.intersect: Finds the intersection of two arrays.flatten: Flattens a multi-dimensional array into a single-dimensional array.groupBy: Groups elements of an array based on a grouping function.shuffle: Shuffles the elements of an array randomly.findSubset: Finds the first object in an array where the subset matches the superset.isSubset: Checks if a subset object is fully contained within a superset object.
Conversion Utility Functions
convertSpace: Converts values between different units of space.convertWeight: Converts values between different units of weight.convertVolume: Converts values between different units of volume.convertValue: Converts values betweenstring,number, andbigint.
Cryptography Utility Functions
aesEncrypt: Encrypts data using AES-256-CBC.aesDecrypt: Decrypts data encrypted with AES-256-CBC.chacha20Encrypt: Encrypts data using ChaCha20.chacha20Decrypt: Decrypts data encrypted with ChaCha20.rsaGenerateKeyPair: Generates an RSA key pair.rsaEncrypt: Encrypts data using an RSA public key.rsaDecrypt: Decrypts data encrypted with an RSA public key using the private key.rsaSign: Signs data using an RSA private key.rsaVerify: Verifies a signature using an RSA public key.eddsaGenerateKeyPair: Generates an EdDSA key pair (using Ed25519).eddsaSign: Signs data using an EdDSA private key.eddsaVerify: Verifies an EdDSA signature using a public key.blowfishEncrypt: Encrypts data using Blowfish.blowfishDecrypt: Decrypts data encrypted with Blowfish.tripleDESEncrypt: Encrypts data using Triple DES.tripleDESDecrypt: Decrypts data encrypted with Triple DES.twofishEncrypt: Encrypts data using Twofish.twofishDecrypt: Decrypts data encrypted with Twofish.serpentEncrypt: Encrypts data using Serpent.serpentDecrypt: Decrypts data encrypted with Serpent.
Date Utility Functions
now: Returns the current date and time.createInterval: Creates an interval between two dates.addTime: Adds a specific duration to a date.removeTime: Subtracts a specific duration from a date.diffBetween: Calculates the difference between two dates.toUTC: Converts a date to UTC.toTimeZone: Converts a date to a specified timezone.
Hash Utility Functions
bcryptHash: Encrypts a string value using bcrypt.bcryptCompare: Compares a string value with an encrypted value.bcryptRandomString: Generates a random string using bcrypt.sha256Hash: Hashes a string value using SHA-256.sha256HashJson: Hashes a JSON object using SHA-256.sha256GenerateToken: Generates a random token using SHA-256.sha512Hash: Hashes a string value using SHA-512.sha512HashJson: Hashes a JSON object using SHA-512.sha512GenerateToken: Generates a random token using SHA-512.
Math Utility Functions
roundToDecimals: Rounds a number to the specified number of decimal places.calculatePercentage: Calculates the percentage of a value.gcd: Finds the greatest common divisor of two numbers.lcm: Finds the least common multiple of two numbers.randomInRange: Generates a random number within a range.clamp: Clamps a number within a range.
Number Utility Functions
isEven: Checks if a number is even.isOdd: Checks if a number is odd.roundDown: Rounds a number down to the nearest integer.roundUp: Rounds a number up to the nearest integer.roundToNearest: Rounds a number to the nearest integer.toCents: Converts a number to cents.addDecimalPlaces: Adds decimal places to a number.removeDecimalPlaces: Removes decimal places from a number.randomIntegerInRange: Generates a random integer within a range.factorial: Calculates the factorial of a number.clamp: Clamps a number within a range.isPrime: Checks if a number is a prime.
Object Utility Functions
findValue: Finds a value in an object by a specified key or path.deepClone: Creates a deep clone of an object.deepMerge: Deeply merges two objects.pick: Picks specific keys from an object.omit: Omits specific keys from an object.flattenObject: Flattens a nested object into a single level.invert: Inverts the keys and values of an object.deepFreeze: Deeply freezes an object, making it immutable.compare: Deeply compares two objects for equality.groupBy: Groups the keys of an object based on a callback function.diff: Finds the difference between two objects.unflattenObject: Sets a value in a nested object by a dot-separated path.compressObject: Compresses a JSON object by converting it to a minified string.decompressObject: Decompresses a JSON string back into an object.compressObjectToBase64: Compresses and encodes a JSON object to a Base64 string.decompressBase64ToObject: Decompresses a Base64-encoded JSON string back to a JSON object.
Request Utility Functions
extractRequestData: Extracts relevant data from an HTTP request object.
Snowflake Utility Functions
generate: Generates a Snowflake ID using a custom epoch.decode: Deconstructs a Snowflake ID into its components.getTimestamp: Extracts the timestamp from a Snowflake ID.
Sorting Algorithms Utility Functions
bubbleSort: Implements Bubble Sort with a time complexity of O(n²) in the worst case.mergeSort: Implements Merge Sort with a time complexity of O(n log n).quickSort: Implements Quick Sort with a time complexity of O(n log n) on average.heapSort: Implements Heap Sort with a time complexity of O(n log n).selectionSort: Implements Selection Sort with a time complexity of O(n²).insertionSort: Implements Insertion Sort, efficient for small or nearly sorted datasets.shellSort: Implements Shell Sort with an average time complexity of O(n log² n).countingSort: Implements Counting Sort, effective for integers with limited range.radixSort: Implements Radix Sort for non-negative integers, processing digits or characters.bucketSort: Implements Bucket Sort, dividing the array into buckets and sorting each.timSort: Combines Merge Sort and Insertion Sort, used in Python and Java.bogoSort: Implements Bogo Sort, an inefficient and impractical sorting algorithm.gnomeSort: Implements Gnome Sort, a variation of Insertion Sort with O(n²) complexity.pancakeSort: Implements Pancake Sort, flipping subarrays to sort.combSort: An improved version of Bubble Sort using larger initial gaps.cocktailShakerSort: A bi-directional Bubble Sort that eliminates turtles.bitonicSort: Optimized for parallel systems, with O(n log² n) complexity.stoogeSort: An academic curiosity with O(n².7095) complexity.
String Utility Functions
capitalizeFirstLetter: Capitalizes the first letter of a string.reverseString: Reverses a string.isPalindrome: Checks if a string is a palindrome.truncateString: Truncates a string to a specified length.toKebabCase: Converts a string to kebab-case.toSnakeCase: Converts a string to snake_case.countOccurrences: Counts the occurrences of a substring.
Validation Utility Functions
isValidEmail: Validates if a string is a valid email address.isValidURL: Validates if a string is a valid URL.isValidPhoneNumber: Validates if a string is a valid phone number.isNumber: Checks if a value is a number.isValidHexColor: Validates if a string is a hexadecimal color.hasMinLength: Checks if a string has a minimum length.isValidJSON: Validates if a string is a JSON.
Contribution Guidelines
How to Contribute
- Branch naming: Use descriptive names like
feat/feature-nameorfix/bug-name. - Pull Requests: Ensure proper tests are included and reviewed before merging.
- Commit Messages: Follow the convention:
feat: Added a new featurefix: Resolved an issuedocs: Updated documentationchore: Performed maintenance tasks
Publish Process
- Update version:
npm version [patch|minor|major] - Publish package:
npm run pub
Support
If you find this library helpful, consider supporting its development:
Pix:
bruno@rmorillo.comCryptocurrencies:
- Dogecoin (DOGE):
DLwW5LFfXV7wN7a7dVV6TX7kiomVnYABXM - Tether (USDT):
0x2b1f5169e3719E0A25850a178db54d8D1C0c12E0 - Bitcoin (BTC):
bc1qk5cakfryrx8dw3w6uqudwkpm9apvd6h5mgl8kg - Ethereum (ETH):
0x2b1f5169e3719E0A25850a178db54d8D1C0c12E0
- Dogecoin (DOGE):
Feel free to contribute or share feedback!
License
This project is licensed under the MIT License. See the LICENSE file for details.
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago