Skip to content

Commit

Permalink
style: edit folder structure fp-54 (#77)
Browse files Browse the repository at this point in the history
* style: edit folder structure fp-54

* style: clarify functions names fp-54
  • Loading branch information
what1s1ove authored Dec 6, 2023
1 parent aba6c0b commit 53624a4
Show file tree
Hide file tree
Showing 39 changed files with 232 additions and 203 deletions.
12 changes: 6 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ npm install form-payload

```js
// index.js
import { getFormValues, getControlValue } from 'form-payload';
import { getFormPayload, getFormControlPayload } from 'form-payload';

const { general: generalFormNode, mailing: mailingFormNode } = document.forms;

generalFormNode.addEventListener('submit', (evt) => {
evt.preventDefault();

console.log(getFormValues(generalFormNode));
console.log(getFormPayload(generalFormNode));
// {
// name: 'Jon',
// birthday: Sat Mar 27 2021 18:06:42 GMT+0200 (Eastern European Standard Time),
Expand All @@ -69,7 +69,7 @@ generalFormNode.addEventListener('submit', (evt) => {
});

mailingFormNode.addEventListener('change', (evt) => {
console.log(getControlValue(evt.target));
console.log(getFormControlPayload(evt.target));
// '[email protected]'
});
```
Expand All @@ -81,21 +81,21 @@ _It doesn't matter which framework you use, you just need to pass the valid [HTM
### React

```jsx
import { getFormValues, getControlValue, ControlType } from 'form-payload';
import { getFormPayload, getFormControlPayload, ControlType } from 'form-payload';

const SimpleForm = () => {
const handleSubmit = (evt: React.FormEvent<HTMLFormElement>) => {
evt.preventDefault();

console.log(getFormValues(evt.target as HTMLFormElement));
console.log(getFormPayload(evt.target as HTMLFormElement));
// {
// name: 'Jon',
// birthday: Sat Mar 27 2021 18:06:42 GMT+0200 (Eastern European Standard Time),
// }
};

const handleChange = (evt: React.ChangeEvent<HTMLInputElement>) => {
console.log(getControlValue(evt.target));
console.log(getFormControlPayload(evt.target));
// '[email protected]'
};

Expand Down
2 changes: 0 additions & 2 deletions src/common/enums/enums.js

This file was deleted.

5 changes: 0 additions & 5 deletions src/common/types/types.js

This file was deleted.

1 change: 0 additions & 1 deletion src/exceptions/exceptions.js

This file was deleted.

130 changes: 0 additions & 130 deletions src/helpers/get-control-value/get-control-value.helper.js

This file was deleted.

4 changes: 0 additions & 4 deletions src/helpers/helpers.js

This file was deleted.

23 changes: 4 additions & 19 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
import { getElementsValues } from './helpers/helpers.js';

/** @typedef {import('./common/types/types.js').HTMLFormControlElement} HTMLFormControlElement */

/**
* @template {Record<string, unknown>} T
* @param {HTMLFormElement} formNode
* @returns {T}
*/
const getFormValues = (formNode) => {
return getElementsValues(
/** @type {HTMLFormControlElement[]} */ ([...formNode.elements]),
);
};

export { getFormValues };
export { ControlType } from './common/enums/enums.js';
export { FormPayloadError } from './exceptions/exceptions.js';
export { getControlValue } from './helpers/helpers.js';
export { ControlType } from './libs/enums/enums.js';
export { FormPayloadError } from './libs/exceptions/exceptions.js';
export { getFormControlPayload } from './packages/get-form-control-payload/get-form-control-payload.js';
export { getFormPayload } from './packages/get-form-payload/get-form-payload.js';
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions src/libs/exceptions/exceptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { FormPayloadError } from './form-payload-error.exception.js';
File renamed without changes.
120 changes: 120 additions & 0 deletions src/packages/get-form-control-payload/get-form-control-payload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import { ControlType } from '../../libs/enums/enums.js';
import { FormPayloadError } from '../../libs/exceptions/exceptions.js';
import {
checkIsReferToAnotherNode,
getAllowedElements,
getCheckboxValue,
getFormControlValue,
getInputDateValue,
getInputFileValue,
getInputNumericValue,
getMultiSelectValues,
} from './helpers/helpers.js';

/** @typedef {import('../../libs/types/types.js').HTMLFormControlElement} HTMLFormControlElement */
/** @typedef {import('../../libs/types/types.js').HTMLFormOperationalControlElement} HTMLFormOperationalControlElement */

/**
* @template {Record<string, unknown>} T
* @param {...HTMLFormControlElement} controlElements
* @returns {T}
*/
const getFormControlsPayload = (...controlElements) => {
const allowedElements = getAllowedElements(controlElements);

let elementsValues = /** @type {T} */ ({});

for (const element of allowedElements) {
const isReferToAnotherNode = checkIsReferToAnotherNode(
element,
...allowedElements,
);

if (isReferToAnotherNode) {
continue;
}

elementsValues = {
...elementsValues,
[element.name]: getFormControlPayload(element),
};
}

return elementsValues;
};

/**
* @param {HTMLFormOperationalControlElement} controlNode
* @returns {unknown}
* @throws {FormPayloadError}
*/
const getFormControlPayload = (controlNode) => {
switch (controlNode.type) {
case ControlType.COLOR:
case ControlType.EMAIL:
case ControlType.HIDDEN:
case ControlType.PASSWORD:
case ControlType.RADIO:
case ControlType.SEARCH:
case ControlType.TEL:
case ControlType.TEXT:
case ControlType.URL:
case ControlType.OUTPUT:
case ControlType.TEXTAREA:
case ControlType.SELECT_ONE: {
return getFormControlValue(
/**
* @type {HTMLInputElement
* | HTMLOutputElement
* | HTMLTextAreaElement
* | HTMLSelectElement}
*/ (controlNode),
);
}
case ControlType.DATE:
case ControlType.DATETIME_LOCAL:
case ControlType.MONTH:
case ControlType.TIME:
case ControlType.WEEK: {
return getInputDateValue(
/** @type {HTMLInputElement} */ (controlNode),
);
}
case ControlType.NUMBER:
case ControlType.RANGE: {
return getInputNumericValue(
/** @type {HTMLInputElement} */ (controlNode),
);
}
case ControlType.CHECKBOX: {
return getCheckboxValue(
/** @type {HTMLInputElement} */ (controlNode),
);
}
case ControlType.SELECT_MULTIPLE: {
return getMultiSelectValues(
/** @type {HTMLSelectElement} */ (controlNode),
);
}
case ControlType.FILE: {
return getInputFileValue(
/** @type {HTMLInputElement} */ (controlNode),
);
}
case ControlType.FIELDSET: {
const elements = [
.../** @type {HTMLFieldSetElement} */ (controlNode).elements,
];

return getFormControlsPayload(
.../** @type {HTMLFormControlElement[]} */ (elements),
);
}
}

throw new FormPayloadError({
message: 'Unknown control type.',
});
};

export { getFormControlPayload, getFormControlsPayload };
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @typedef {import('../../../../common/types/types.js').HTMLFormOperationalControlElement} HTMLFormOperationalControlElement */
/** @typedef {import('../../../../libs/types/types.js').HTMLFormOperationalControlElement} HTMLFormOperationalControlElement */

/**
* @template {HTMLFormOperationalControlElement} T
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {
BANNED_CONTROL_TYPES,
BANNED_FORM_OPERATIONAL_CONTROL_ELEMENTS,
} from '../../../../common/constants/constants.js';
} from '../../../../libs/constants/constants.js';

/** @typedef {import('../../../../common/types/types.js').HTMLFormControlElement} HTMLFormControlElement */
/** @typedef {import('../../../../common/types/types.js').HTMLFormOperationalControlElement} HTMLFormOperationalControlElement */
/** @typedef {import('../../../../libs/types/types.js').HTMLFormControlElement} HTMLFormControlElement */
/** @typedef {import('../../../../libs/types/types.js').HTMLFormOperationalControlElement} HTMLFormOperationalControlElement */

const checkControlFunctionMap = /** @type {const} */ ({
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @param {HTMLInputElement} element
* @returns {boolean}
*/
const getCheckboxValue = (element) => {
return element.checked;
};

export { getCheckboxValue };
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @param {HTMLInputElement
* | HTMLOutputElement
* | HTMLTextAreaElement
* | HTMLSelectElement} element
* @returns {string}
*/
const getFormControlValue = (element) => {
return element.value;
};

export { getFormControlValue };
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @param {HTMLInputElement} element
* @returns {Date | null}
*/
const getInputDateValue = (element) => {
return element.valueAsDate;
};

export { getInputDateValue };
Loading

0 comments on commit 53624a4

Please sign in to comment.