diff --git a/changelog.md b/changelog.md index 7afa9cc..47049d9 100644 --- a/changelog.md +++ b/changelog.md @@ -1,10 +1,11 @@ Changelog ========= -0.2.0 (????-??-??) +1.0.0 (????-??-??) ------------------ * Updated to Typescript 5. +* Conversion from Typescript to Javascript. 0.1.12 (2022-10-22) diff --git a/package.json b/package.json index be5622f..c4508d7 100644 --- a/package.json +++ b/package.json @@ -2,12 +2,12 @@ "name": "html-form-enhancer", "version": "0.1.12", "description": "Adds support for enctype=\"application/json\", more HTTP methods and HTTP status codes to HTML forms", - "export": "html-form-enhancer.mjs", - "main": "dist/html-form-enhancer.js", + "export": "src/html-form-enhancer.mjs", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "lint": "eslint" }, + "type": "module", "keywords": [ "html", "json", diff --git a/src/html-form-enhancer.ts b/src/html-form-enhancer.js similarity index 88% rename from src/html-form-enhancer.ts rename to src/html-form-enhancer.js index f833113..2e68ed4 100644 --- a/src/html-form-enhancer.ts +++ b/src/html-form-enhancer.js @@ -1,7 +1,9 @@ -/* eslint no-console: 0 */ import { serializeJsonForm } from './serialize-json-form.js'; -export function enhanceForm(elem: HTMLFormElement) { +/** + * @param {HTMLFormElement} elem + */ +export function enhanceForm(elem) { elem.addEventListener('submit', (ev) => { @@ -12,7 +14,10 @@ export function enhanceForm(elem: HTMLFormElement) { } -export function autoEnhanceForms(doc: Document) { +/** + * @param {Document} doc + */ +export function autoEnhanceForms(doc) { const forms = doc.getElementsByTagName('form'); for(const form of forms) { @@ -30,7 +35,10 @@ export function autoEnhanceForms(doc: Document) { autoEnhanceForms(document); -async function processSubmit(elem: HTMLFormElement) { +/** + * @param {HTMLFormElement} elem + */ +async function processSubmit(elem) { const method = elem.getAttribute('method')?.toUpperCase() || 'GET'; const encType = elem.getAttribute('enctype')?.toLowerCase() || 'application/x-www-form-urlencoded'; @@ -51,7 +59,9 @@ async function processSubmit(elem: HTMLFormElement) { } else { if (!encType || encType === 'application/x-www-form-urlencoded') { - body = new URLSearchParams(Object.fromEntries(new FormData(elem).entries()) as Record); + body = new URLSearchParams( + /** @type {any} */(new FormData(elem)) + ); } else if (encType === 'application/json' || encType.match(/^application\/(.*)\+json$/)) { body = JSON.stringify(serializeJsonForm(elem)); } @@ -63,7 +73,7 @@ async function processSubmit(elem: HTMLFormElement) { response = await fetch(action, { method, headers: { - 'Content-Type': encType!, + 'Content-Type': encType, 'Accept': 'text/html', }, body, @@ -123,7 +133,10 @@ async function processSubmit(elem: HTMLFormElement) { } -async function replaceBody(response: Response) { +/** + * @param {Response} response + */ +async function replaceBody(response) { const responseBody = await response.text(); const domParser = new DOMParser(); diff --git a/src/serialize-json-form.ts b/src/serialize-json-form.js similarity index 89% rename from src/serialize-json-form.ts rename to src/serialize-json-form.js index 20a34ea..574713e 100644 --- a/src/serialize-json-form.ts +++ b/src/serialize-json-form.js @@ -3,7 +3,7 @@ * * https://github.com/defunctzombie/form-serialize/blob/master/LICENSE * - * It's cleaned up, modernized and converted to Typescript, but the original + * It's cleaned up and modernized, but the original * is Copyright (c) 2013 Roman Shtylman and licensed under the MIT license. */ @@ -17,9 +17,10 @@ const brackets = /(\[[^[\]]*\])/g; /** * Serializes a form into html-json-forms format. * + * @param {HTMLFormElement} form * https://www.w3.org/TR/html-json-forms/ */ -export function serializeJsonForm(form: HTMLFormElement) { +export function serializeJsonForm(form) { let result = {}; @@ -112,7 +113,11 @@ export function serializeJsonForm(form: HTMLFormElement) { return result; } -function parse_keys(str: string): string[] { +/** + * @param {string} str + * @returns {string[]} + */ +function parse_keys(str) { const keys = []; const prefix = /^([^[\]]*)/; const children = new RegExp(brackets); @@ -130,18 +135,18 @@ function parse_keys(str: string): string[] { } /** - * Too hard to type right now + * @param {any} result + * @param {string[]} keys + * @param {any} value */ -type Result = any; -type Value = string; - -function hash_assign(result: Result, keys: string[], value: Value): Result { +function hash_assign(result, keys, value) { if (keys.length === 0) { result = value; return result; } - const key = keys.shift()!; + const key = keys.shift(); + if (!key) return result; const between = key.match(/^\[(.+?)\]$/); if (key === '[]') { @@ -189,8 +194,12 @@ function hash_assign(result: Result, keys: string[], value: Value): Result { return result; } -// Object/hash encoding serializer. -function hash_serializer(result: Result, key: string, value: Value) { +/** + * @param {any} result + * @param {string} key + * @param {any} value + */ +function hash_serializer(result, key, value) { const matches = key.match(brackets); // Has brackets? Use the recursive assignment function to walk the keys, @@ -225,7 +234,11 @@ function hash_serializer(result: Result, key: string, value: Value) { return result; } -function isValidInputField(elem: Element): elem is HTMLFormElement { +/** + * @param {Element} elem + * @returns {elem is HTMLFormElement} + */ +function isValidInputField(elem) { const supportedElements = ['input', 'select', 'textarea']; const unsupportedInputType = ['submit' , 'button' , 'image' , 'reset' , 'file']; @@ -234,7 +247,11 @@ function isValidInputField(elem: Element): elem is HTMLFormElement { return false; } - if (unsupportedInputType.includes((elem as any).type)) { + if (!(elem instanceof HTMLInputElement)) { + return false; + } + + if (unsupportedInputType.includes(elem.type)) { return false; } return true; diff --git a/tsconfig.json b/tsconfig.json index 81757dc..e4fff73 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -9,13 +9,14 @@ "moduleResolution": "node", "sourceMap": true, - "outDir": "dist", + "noEmit": true, "baseUrl": ".", "paths": { "*": [ "src/types/*" ] }, + "checkJs": true, "lib": [ "DOM", "DOM.Iterable",