Skip to content

Commit fa38bfb

Browse files
committed
Total conversion back to Javascript from Typescript.
No need for Typescript for such a small package
1 parent 217ea58 commit fa38bfb

File tree

5 files changed

+56
-24
lines changed

5 files changed

+56
-24
lines changed

changelog.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
Changelog
22
=========
33

4-
0.2.0 (????-??-??)
4+
1.0.0 (????-??-??)
55
------------------
66

77
* Updated to Typescript 5.
8+
* Conversion from Typescript to Javascript.
89

910

1011
0.1.12 (2022-10-22)

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
"name": "html-form-enhancer",
33
"version": "0.1.12",
44
"description": "Adds support for enctype=\"application/json\", more HTTP methods and HTTP status codes to HTML forms",
5-
"export": "html-form-enhancer.mjs",
6-
"main": "dist/html-form-enhancer.js",
5+
"export": "src/html-form-enhancer.mjs",
76
"scripts": {
87
"test": "echo \"Error: no test specified\" && exit 1",
98
"lint": "eslint"
109
},
10+
"type": "module",
1111
"keywords": [
1212
"html",
1313
"json",

src/html-form-enhancer.ts src/html-form-enhancer.js

+20-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
/* eslint no-console: 0 */
21
import { serializeJsonForm } from './serialize-json-form.js';
32

4-
export function enhanceForm(elem: HTMLFormElement) {
3+
/**
4+
* @param {HTMLFormElement} elem
5+
*/
6+
export function enhanceForm(elem) {
57

68
elem.addEventListener('submit', (ev) => {
79

@@ -12,7 +14,10 @@ export function enhanceForm(elem: HTMLFormElement) {
1214

1315
}
1416

15-
export function autoEnhanceForms(doc: Document) {
17+
/**
18+
* @param {Document} doc
19+
*/
20+
export function autoEnhanceForms(doc) {
1621
const forms = doc.getElementsByTagName('form');
1722
for(const form of forms) {
1823

@@ -30,7 +35,10 @@ export function autoEnhanceForms(doc: Document) {
3035

3136
autoEnhanceForms(document);
3237

33-
async function processSubmit(elem: HTMLFormElement) {
38+
/**
39+
* @param {HTMLFormElement} elem
40+
*/
41+
async function processSubmit(elem) {
3442

3543
const method = elem.getAttribute('method')?.toUpperCase() || 'GET';
3644
const encType = elem.getAttribute('enctype')?.toLowerCase() || 'application/x-www-form-urlencoded';
@@ -51,7 +59,9 @@ async function processSubmit(elem: HTMLFormElement) {
5159
} else {
5260

5361
if (!encType || encType === 'application/x-www-form-urlencoded') {
54-
body = new URLSearchParams(Object.fromEntries(new FormData(elem).entries()) as Record<string, string>);
62+
body = new URLSearchParams(
63+
/** @type {any} */(new FormData(elem))
64+
);
5565
} else if (encType === 'application/json' || encType.match(/^application\/(.*)\+json$/)) {
5666
body = JSON.stringify(serializeJsonForm(elem));
5767
}
@@ -63,7 +73,7 @@ async function processSubmit(elem: HTMLFormElement) {
6373
response = await fetch(action, {
6474
method,
6575
headers: {
66-
'Content-Type': encType!,
76+
'Content-Type': encType,
6777
'Accept': 'text/html',
6878
},
6979
body,
@@ -123,7 +133,10 @@ async function processSubmit(elem: HTMLFormElement) {
123133

124134
}
125135

126-
async function replaceBody(response: Response) {
136+
/**
137+
* @param {Response} response
138+
*/
139+
async function replaceBody(response) {
127140

128141
const responseBody = await response.text();
129142
const domParser = new DOMParser();

src/serialize-json-form.ts src/serialize-json-form.js

+30-13
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* https://github.com/defunctzombie/form-serialize/blob/master/LICENSE
55
*
6-
* It's cleaned up, modernized and converted to Typescript, but the original
6+
* It's cleaned up and modernized, but the original
77
* is Copyright (c) 2013 Roman Shtylman and licensed under the MIT license.
88
*/
99

@@ -17,9 +17,10 @@ const brackets = /(\[[^[\]]*\])/g;
1717
/**
1818
* Serializes a form into html-json-forms format.
1919
*
20+
* @param {HTMLFormElement} form
2021
* https://www.w3.org/TR/html-json-forms/
2122
*/
22-
export function serializeJsonForm(form: HTMLFormElement) {
23+
export function serializeJsonForm(form) {
2324

2425
let result = {};
2526

@@ -112,7 +113,11 @@ export function serializeJsonForm(form: HTMLFormElement) {
112113
return result;
113114
}
114115

115-
function parse_keys(str: string): string[] {
116+
/**
117+
* @param {string} str
118+
* @returns {string[]}
119+
*/
120+
function parse_keys(str) {
116121
const keys = [];
117122
const prefix = /^([^[\]]*)/;
118123
const children = new RegExp(brackets);
@@ -130,18 +135,18 @@ function parse_keys(str: string): string[] {
130135
}
131136

132137
/**
133-
* Too hard to type right now
138+
* @param {any} result
139+
* @param {string[]} keys
140+
* @param {any} value
134141
*/
135-
type Result = any;
136-
type Value = string;
137-
138-
function hash_assign(result: Result, keys: string[], value: Value): Result {
142+
function hash_assign(result, keys, value) {
139143
if (keys.length === 0) {
140144
result = value;
141145
return result;
142146
}
143147

144-
const key = keys.shift()!;
148+
const key = keys.shift();
149+
if (!key) return result;
145150
const between = key.match(/^\[(.+?)\]$/);
146151

147152
if (key === '[]') {
@@ -189,8 +194,12 @@ function hash_assign(result: Result, keys: string[], value: Value): Result {
189194
return result;
190195
}
191196

192-
// Object/hash encoding serializer.
193-
function hash_serializer(result: Result, key: string, value: Value) {
197+
/**
198+
* @param {any} result
199+
* @param {string} key
200+
* @param {any} value
201+
*/
202+
function hash_serializer(result, key, value) {
194203
const matches = key.match(brackets);
195204

196205
// Has brackets? Use the recursive assignment function to walk the keys,
@@ -225,7 +234,11 @@ function hash_serializer(result: Result, key: string, value: Value) {
225234
return result;
226235
}
227236

228-
function isValidInputField(elem: Element): elem is HTMLFormElement {
237+
/**
238+
* @param {Element} elem
239+
* @returns {elem is HTMLFormElement}
240+
*/
241+
function isValidInputField(elem) {
229242

230243
const supportedElements = ['input', 'select', 'textarea'];
231244
const unsupportedInputType = ['submit' , 'button' , 'image' , 'reset' , 'file'];
@@ -234,7 +247,11 @@ function isValidInputField(elem: Element): elem is HTMLFormElement {
234247
return false;
235248
}
236249

237-
if (unsupportedInputType.includes((elem as any).type)) {
250+
if (!(elem instanceof HTMLInputElement)) {
251+
return false;
252+
}
253+
254+
if (unsupportedInputType.includes(elem.type)) {
238255
return false;
239256
}
240257
return true;

tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99

1010
"moduleResolution": "node",
1111
"sourceMap": true,
12-
"outDir": "dist",
12+
"noEmit": true,
1313
"baseUrl": ".",
1414
"paths": {
1515
"*": [
1616
"src/types/*"
1717
]
1818
},
19+
"checkJs": true,
1920
"lib": [
2021
"DOM",
2122
"DOM.Iterable",

0 commit comments

Comments
 (0)