@brmorillo/utils-test v0.0.13
@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 valueProject 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
encrypt: Encrypts a string value using bcrypt.compare: Compares a string value with an encrypted hash.generateRandomString: Generates a random string using bcrypt.
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.
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.
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.
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
No direct commits to the
mainbranch: All development must be done in separate branches.Branch naming: Use descriptive names for your branches. Examples:
feat/new-featurefix/login-error
Pull Requests:
- Every pull request (PR) must be reviewed and approved before merging into
main. - Ensure you follow the commit message standards.
- Every pull request (PR) must be reviewed and approved before merging into
Commit Messages: Follow the project's commit message conventions (based on Angular):
feat: description of the new featurefix: description of the bug fixdocs: documentation updates
How to Publish
If you want to contribute and publish updates to this library:
Bump the version: Use the following command to update the version according to SemVer:
npm version patch # For small fixes npm version minor # For new features npm version major # For breaking changesPublish the package:
npm publish
Feel free to fork, contribute, and submit pull requests! Any contributions are welcome.
License
This project is licensed under the MIT License. See the LICENSE file for details.