Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
timonson committed Mar 14, 2024
1 parent 0e095a5 commit d686463
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 24 deletions.
2 changes: 1 addition & 1 deletion encoding/data_url.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
export function makeDataUrl(mediaType) {
return (data) =>
`data:${mediaType},${
mediaType.includes(";base64;") ? data : encodeURIComponent(data)
mediaType.includes(";base64,") ? data : encodeURIComponent(data)
}`;
}

Expand Down
30 changes: 30 additions & 0 deletions higher_order.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,33 @@ export function identity(x) {
export function constant(x) {
return () => x;
}

/**
* Creates a higher-order function that wraps an asynchronous function with error handling.
*
* @template F, R, E
* @param {F} f - The asynchronous function to be executed with error handling.
* @returns {(args: Parameters<F>) => (ifCaughtReturnValue: E | ((...args: Parameters<F>) => E)) => Promise<R | E>}
*
* The returned higher-order function takes any arguments for the asynchronous function `f` and returns
* another function that accepts an optional value or function to be returned in case of an error.
*
* @example
* // Example usage of the tryCatch function
* async function exampleAsyncFunc(param: number): Promise<string> {
* // Async logic here...
* }
* const safeAsyncFunc = tryCatch(exampleAsyncFunc);
* safeAsyncFunc(42)(() => 'default value').then(result => console.log(result));
*/
export function tryCatch(f) {
return (...args) => async (ifCaughtReturnValue) => {
try {
return await f(...args);
} catch {
return typeof ifCaughtReturnValue === "function"
? await ifCaughtReturnValue(...args)
: ifCaughtReturnValue;
}
};
}
24 changes: 24 additions & 0 deletions response.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Difference between cloning and copying of a `Response`:
* https://community.cloudflare.com/t/whats-the-point-of-response-clone/216456
*/
export function copyResponse(response) {
return new Response(response.body, response);
}

export function getResponseBody(input) {
if (typeof input === "string") {
return input;
} else if (input === undefined || input === null) {
return "";
} else if (
input instanceof Blob || input instanceof ArrayBuffer ||
ArrayBuffer.isView(input) || input instanceof DataView ||
input instanceof URLSearchParams || input instanceof FormData ||
input instanceof ReadableStream
) {
return input;
} else {
return JSON.stringify(input);
}
}
23 changes: 0 additions & 23 deletions strings/mapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,3 @@ export function convertDashToCamel(str) {
export function convertCamelToDash(str) {
return str.replace(/([a-zA-Z0-9])(?=[A-Z])/g, "$1-").toLowerCase();
}

/**
* Takes a string and converts dashes to spaces.
*
* @param {string} str
* @return {string}
*/
export function convertDashesToSpaces(str) {
return str.replace(/-+/g, " ").replace(
/-([a-zA-Z])/g,
(match, group) => ` ${group}`,
);
}

/**
* Takes a string and converts spaces to dashes.
*
* @param {string} str
* @return {string}
*/
export function convertSpacesToDashes(str) {
return str.replace(/\s+/g, "-");
}
14 changes: 14 additions & 0 deletions type.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,17 @@ export function isUrl(input) {
export function isRegExp(input) {
return input instanceof RegExp;
}

/**
* isUrlString.
* @param {unknown} input
* @returns {input is string}
*/
export function isUrlString(input) {
try {
const url = new URL(input);
return true;
} catch {
return false;
}
}

0 comments on commit d686463

Please sign in to comment.