the-basic-utils
v1.0.1
Published
A collective package of basic utils
Downloads
68
Maintainers
Readme
Basic Utilities npm Package
This npm package provides a comprehensive set of utility functions that make your development process easier by providing reusable, well-tested helper methods for common tasks.
Installation
To install this package, run:
npm install the-basic-utilsImporting Utilities
You can import specific functions or the entire module:
const { isEmpty, isJson, deepClone } = require('the-basic-utils');
// or
const utils = require('the-basic-utils');Functions and Usage
1. isEmpty
Checks if a value is empty.
- Input: Any type.
- Output:
trueif the value is empty, otherwisefalse.
console.log(isEmpty('')); // true
console.log(isEmpty({})); // true
console.log(isEmpty([1, 2, 3])); // false2. isJson
Checks if a string is a valid JSON.
- Input:
string - Output:
trueif valid JSON, otherwisefalse.
console.log(isJson('{"key": "value"}')); // true
console.log(isJson('Not JSON')); // false3. deepClone
Deep clones an object or array.
- Input: Object or Array.
- Output: A deep copy of the input.
const obj = { a: 1, b: { c: 2 } };
const clone = deepClone(obj);
console.log(clone); // { a: 1, b: { c: 2 } }4. randomString
Generates a random string of a specified length.
- Input:
length(default: 8). - Output: Random string.
console.log(randomString(10)); // Example: 'aB3dEfGHi1'5. capitalize
Capitalizes the first letter of a string.
- Input:
string - Output: String with the first letter capitalized.
console.log(capitalize('hello')); // Hello6. formatDate
Formats a date to YYYY-MM-DD.
- Input:
Dateobject (default: current date). - Output: Formatted date string.
console.log(formatDate(new Date())); // 2024-12-247. debounce
Debounces a function, delaying its execution.
- Input: Function, delay in milliseconds.
- Output: Debounced function.
const debouncedFunc = debounce(() => console.log('Run!'), 500);
debouncedFunc();8. throttle
Throttles a function, ensuring it's called at most once every specified time.
- Input: Function, delay in milliseconds.
- Output: Throttled function.
const throttledFunc = throttle(() => console.log('Run!'), 500);
throttledFunc();9. isPlainObject
Checks if a value is a plain object.
- Input: Any value.
- Output:
trueif plain object, otherwisefalse.
console.log(isPlainObject({})); // true
console.log(isPlainObject([])); // false10. deepMerge
Merges multiple objects deeply.
- Input: Target object, source objects.
- Output: Merged object.
const obj1 = { a: 1, b: { c: 2 } };
const obj2 = { b: { d: 3 } };
console.log(deepMerge({}, obj1, obj2)); // { a: 1, b: { c: 2, d: 3 } }11. objectToQueryString
Converts an object to a query string.
- Input: Object.
- Output: Query string.
console.log(objectToQueryString({ a: 1, b: 2 })); // 'a=1&b=2'12. compactArray
Removes falsy values from an array.
- Input: Array.
- Output: Filtered array.
console.log(compactArray([0, 1, false, 2, '', 3])); // [1, 2, 3]13. toCamelCase
Converts a string to camelCase.
- Input:
string - Output: Camel-cased string.
console.log(toCamelCase('hello-world')); // helloWorld14. toPascalCase
Converts a string to PascalCase.
- Input:
string - Output: Pascal-cased string.
console.log(toPascalCase('hello-world')); // HelloWorld15. bytesToHumanReadable
Converts bytes to a human-readable format.
- Input: Bytes, decimals (default: 2).
- Output: Human-readable string.
console.log(bytesToHumanReadable(1024)); // '1 KB'16. sleep
Pauses execution for a specified duration.
- Input: Milliseconds.
- Output: Promise.
await sleep(1000); // Pauses for 1 second.17. escapeHtml
Escapes HTML special characters.
- Input: String.
- Output: Escaped string.
console.log(escapeHtml('<div>Test</div>')); // '<div>Test</div>'18. unescapeHtml
Unescapes HTML special characters.
- Input: String.
- Output: Unescaped string.
console.log(unescapeHtml('<div>Test</div>')); // '<div>Test</div>'19. dateDifferenceInDays
Calculates the difference in days between two dates.
- Input: Two date strings or objects.
- Output: Difference in days.
console.log(dateDifferenceInDays('2024-12-24', '2024-12-31')); // 720. groupBy
Groups an array of objects by a property.
- Input: Array, key.
- Output: Grouped object.
const data = [
{ group: 'A', value: 1 },
{ group: 'B', value: 2 },
{ group: 'A', value: 3 }
];
console.log(groupBy(data, 'group'));
// { A: [{ group: 'A', value: 1 }, { group: 'A', value: 3 }], B: [{ group: 'B', value: 2 }] }21. clamp
Clamps a number between a minimum and maximum value.
- Input: Value, min, max.
- Output: Clamped value.
console.log(clamp(5, 1, 10)); // 5
console.log(clamp(-5, 1, 10)); // 122. isPalindrome
Checks if a value is a palindrome.
- Input: String or number.
- Output:
trueif palindrome, otherwisefalse.
console.log(isPalindrome('racecar')); // true
console.log(isPalindrome(12321)); // trueContributing
Contributions are welcome! Please submit issues or pull requests via the GitHub repository.
License
This package is licensed under the MIT License.
