a set of Javascript supplements utilities and functions
npm install handyutility
A set of Javascript supplements utilities and functions
shell
npm install handyutility
`
Alternatively, you can use the CDN script to include the hu.min.js in your HTML file
`html
`
Alternatively, you can use the CDN script to include the hu.min.js in your HTML file
`html
`
Otherwise, you can download the hu.min.js file from the dist directory and include it in your HTML file, or click here to download it directly
`html
`
Usage
Once you have installed or included the hu.min.js file, you can start using the functions in your code.
`javascript
// Import the handyutility
import Utils from "handyutility";
// explicitly use the handy, operators and promises ...
import { Handy, Operators, Promises } from "handyutility"
Utils.handy.IPify().then((ip) => {
console.log(ip);
});
Handy.IPify().then((ip) => {
console.log(ip);
});
`
Utilities
$3
`javascript
Utils.handy.IPify()
`
Retrieve the IP address of the current machine.
`javascript
const url = new URL("https://www.google.com/search?q=handyscript");
Utils.handy.searchParamsSerializer(url: URL) // {q: "handyscript"}
`
Extract the search params from a URL
> There is more to discover ✨
$3
`javascript
Utils.operators.is(1, 1) // true
Utils.operators.is("hello", "hi") // false
// Objects are compared by their keys recursively
const obj1 = {name: "john", age: 20};
const obj2 = {name: "john", age: 20};
Utils.operators.is(obj1, obj2) // true
// Functions and Regular expressions are compared by their source code
const fn1 = () => console.log("hello");
const fn2 = () => console.log("hi");
Utils.operators.is(fn1, fn2) // false
const reg1 = /hello/;
const reg2 = /hi/;
Utils.operators.is(reg1, reg2) // false
// Dates are compared by their millisecond representation
const date1 = new Date();
const date2 = new Date(date1.getTime());
Utils.operators.is(date1, date2) // true
`
The is function is used to compare values if they are truly equal
> There is more to discover ✨
$3
`typescript
async function simulateAsyncOperation(attempt: number): Promise {
const randomSuccess = Math.random() < 0.5; // Simulate a success or failure randomly
if (randomSuccess) {
return attempt;
} else {
throw new Error( Attempt ${attempt} failed);
}
}
async function main() {
const maxAttempts = 5;
const delayMs = 1000;
try {
const result = await Utils.promises.retry(async () => {
const attempt = await simulateAsyncOperation(1); // Start with the first attempt
if (attempt < maxAttempts) {
throw new Error('Retry');
}
return attempt;
}, maxAttempts, delayMs);
console.log(Operation succeeded on attempt ${result});
} catch (error) {
console.error(Operation failed: ${error.message});
}
}
main();
`
The retry` function facilitates robust handling of transient failures in asynchronous operations, employing exponential backoff with a specified maximum retry limit