diff --git a/package.json b/package.json index e887692..e5b291b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ztimson/utils", - "version": "0.14.11", + "version": "0.14.12", "description": "Utility library", "author": "Zak Timson", "license": "MIT", diff --git a/src/misc.ts b/src/misc.ts index 0ed10d8..f2ab3cb 100644 --- a/src/misc.ts +++ b/src/misc.ts @@ -1,17 +1,5 @@ import {md5} from './string'; -/** - * Convert data into a form encoded format. - * - * @param {any} data - data to convert - * @returns {string} - Ecodeded form data - */ -export function formEncode(data: any): string { - return Object.entries(data).map(([key, value]) => - encodeURIComponent(key) + '=' + encodeURIComponent(value) - ).join('&'); -} - /** * Get profile image from Gravatar * diff --git a/src/objects.ts b/src/objects.ts index a748789..5aecb47 100644 --- a/src/objects.ts +++ b/src/objects.ts @@ -119,6 +119,17 @@ export function flattenObj(obj: any, parent?: any, result: any = {}) { } } +/** + * Convert object to FormData + * @param target - Object to convert + * @return {FormData} - Form object + */ +export function formData(target: any): FormData { + const data = new FormData(); + Object.entries(target).forEach(([key, value]) => data.append(key, value)); + return data; +} + /** * Check that an object has the following values * @@ -185,3 +196,15 @@ export function sanitizedJSON(obj: any, space?: number) { return value; }, space)); } + +/** + * Convert object into URL encoded string + * + * @param {any} data - data to convert + * @returns {string} - Encoded form data + */ +export function urlEncode(data: any): string { + return Object.entries(data).map(([key, value]) => + encodeURIComponent(key) + '=' + encodeURIComponent(value) + ).join('&'); +}