Skip to content

Commit

Permalink
feat: get value from input email multiple as array fp-97 (#115)
Browse files Browse the repository at this point in the history
* style: edit order in tsconfig fp-97

* feat: get value from input email multiple as array fp-97
  • Loading branch information
what1s1ove authored Dec 11, 2023
1 parent e8daf15 commit 71bc62f
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 36 deletions.
65 changes: 33 additions & 32 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,35 +63,36 @@ PS. _The library works perfectly with any framework. Just use a valid [HTMLFormE

## Value Correspondence Table

| HTMLElement | Attributes | Included | Value |
| ------------------------------------------------------------------------------------------- | ---------------------------- | -------- | ----------------------------------------------------------------------------------------------- |
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="text"` || `string` |
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="password"` || `string` |
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="email"` || `string` |
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="search"` || `string` |
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="url"` || `string` |
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="tel"` || `string` |
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="color"` || `string` |
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="radio"` || `string` |
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="hidden"` || `string` |
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="number"` || `number` |
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="range"` || `number` |
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="checkbox"` || `boolean` |
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="date"` || [`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) |
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="time"` || [`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) |
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="month"` || [`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) |
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="week"` || [`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) |
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="datetime-local"` || [`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) |
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="file"` || [`File`](https://developer.mozilla.org/en-US/docs/Web/API/File) or `null` |
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="file"` and `multiple` || <code>Array<[File](https://developer.mozilla.org/en-US/docs/Web/API/File)></code> |
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="button"` |||
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="submit"` |||
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="reset"` |||
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="image"` |||
| [HTMLTextAreaElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLTextareaElement) ||| `string` |
| [HTMLSelectElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement) ||| `string` |
| [HTMLSelectElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement) | `multiple` || `Array<string>` |
| [HTMLOutputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLOutputElement) ||| `string` |
| [HTMLFieldsetElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLFieldsetElement) ||| `Object<name: string, value: unknown>` (recursive values of nested elements) |
| [HTMLButtonElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLButtonElement) ||||
| [HTMLObjectElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLObjectElement) ||||
| HTMLElement | Attributes | Included | Return Value |
| ------------------------------------------------------------------------------------------- | ----------------------------- | -------- | ----------------------------------------------------------------------------------------------- |
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="text"` || `string` |
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="password"` || `string` |
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="search"` || `string` |
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="url"` || `string` |
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="tel"` || `string` |
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="color"` || `string` |
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="radio"` || `string` |
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="hidden"` || `string` |
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="email"` || `string` |
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="email"` and `multiple` || `Array<string>` |
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="number"` || `number` |
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="range"` || `number` |
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="checkbox"` || `boolean` |
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="date"` || [`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) |
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="time"` || [`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) |
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="month"` || [`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) |
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="week"` || [`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) |
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="datetime-local"` || [`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) |
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="file"` || [`File`](https://developer.mozilla.org/en-US/docs/Web/API/File) or `null` |
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="file"` and `multiple` || <code>Array<[File](https://developer.mozilla.org/en-US/docs/Web/API/File)></code> |
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="button"` |||
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="submit"` |||
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="reset"` |||
| [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) | `type="image"` |||
| [HTMLTextAreaElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLTextareaElement) ||| `string` |
| [HTMLSelectElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement) ||| `string` |
| [HTMLSelectElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement) | `multiple` || `Array<string>` |
| [HTMLOutputElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLOutputElement) ||| `string` |
| [HTMLFieldsetElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLFieldsetElement) ||| `Object<name: string, value: unknown>` (recursive values of nested elements) |
| [HTMLButtonElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLButtonElement) ||||
| [HTMLObjectElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLObjectElement) ||||
4 changes: 3 additions & 1 deletion src/libs/enums/control-type.enum.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
const ControlType = /** @type {const} */ ({
TEXT: 'text',
PASSWORD: 'password',
EMAIL: 'email',
SEARCH: 'search',
URL: 'url',
TEL: 'tel',
COLOR: 'color',
RADIO: 'radio',
HIDDEN: 'hidden',

EMAIL: 'email',

NUMBER: 'number',
RANGE: 'range',

Expand All @@ -18,6 +19,7 @@ const ControlType = /** @type {const} */ ({
TIME: 'time',
MONTH: 'month',
WEEK: 'week',

DATETIME_LOCAL: 'datetime-local',

FILE: 'file',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
getDatetimeLocalValue,
getFormControlValue,
getInputDateValue,
getInputEmailValue,
getInputFileValue,
getInputNumericValue,
getMultiSelectValues,
Expand Down Expand Up @@ -62,7 +63,6 @@ const getFormControlPayload = (controlNode) => {
switch (controlNode.type) {
case ControlType.TEXT:
case ControlType.PASSWORD:
case ControlType.EMAIL:
case ControlType.SEARCH:
case ControlType.URL:
case ControlType.TEL:
Expand All @@ -83,6 +83,13 @@ const getFormControlPayload = (controlNode) => {
)
);
}
case ControlType.EMAIL: {
return /** @type {T} */ (
getInputEmailValue(
/** @type {HTMLInputElement} */ (controlNode),
)
);
}
case ControlType.NUMBER:
case ControlType.RANGE: {
return /** @type {T} */ (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { getFormControlValue } from '../get-control-value/get-control-value.helper.js';

const EMAIL_SEPARATOR = ',';

/**
* @param {HTMLInputElement} inputNode
* @returns {string | string[]}
*/
const getInputEmailValue = (inputNode) => {
if (inputNode.multiple) {
return inputNode.value
.split(EMAIL_SEPARATOR)
.map((email) => email.trim())
.filter((email) => email !== '');
}

return getFormControlValue(inputNode);
};

export { getInputEmailValue };
1 change: 1 addition & 0 deletions src/packages/get-form-control-payload/helpers/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export { getCheckboxValue } from './get-checkbox-value/get-checkbox-value.helper
export { getFormControlValue } from './get-control-value/get-control-value.helper.js';
export { getDatetimeLocalValue } from './get-datatime-local-value/get-datatime-local-value.helper.js';
export { getInputDateValue } from './get-input-date-value/get-input-date-value.helper.js';
export { getInputEmailValue } from './get-input-email-value/get-input-email-value.helper.js';
export { getInputFileValue } from './get-input-file-value/get-input-file-value.helper.js';
export { getInputNumericValue } from './get-input-numeric-value/get-input-numeric-value.helper.js';
export { getMultiSelectValues } from './get-multi-select-values/get-multi-select-values.helper.js';
64 changes: 63 additions & 1 deletion tests/get-form-control-payload.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ describe('getFormControlPayload should work correctly', () => {
const inputs = /** @type {const} */ ([
[ControlType.TEXT, 'Name'],
[ControlType.PASSWORD, 'top-secret'],
[ControlType.EMAIL, '[email protected]'],
[ControlType.SEARCH, 'apples'],
[ControlType.URL, 'form-payload.com'],
[ControlType.TEL, '10000000000'],
Expand Down Expand Up @@ -127,6 +126,69 @@ describe('getFormControlPayload should work correctly', () => {
}
});

describe('should get value from email inputs correctly', () => {
test('should get value from singular email input correctly', () => {
const email = '[email protected]';

document.body.innerHTML = /* HTML */ `
<form>
<input type="${ControlType.EMAIL}" value="${email}" />
</form>
`;

const control = /** @type {HTMLInputElement} */ (
document.querySelector('input')
);

const controlValue = getFormControlPayload(control);

equal(typeof controlValue, 'string');

equal(controlValue, email);
});

test('should get value from multiple email input correctly', () => {
const emails = [
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
];

document.body.innerHTML = /* HTML */ `
<form>
<input
type="${ControlType.EMAIL}"
value="${emails.join(',')}"
multiple
/>
</form>
`;

const control = /** @type {HTMLInputElement} */ (
document.querySelector('input')
);

const controlValue = getFormControlPayload(control);

equal(Array.isArray(controlValue), true);

equal(
/** @type {string[]} */ (controlValue).length,
emails.length,
);

equal(
/** @type {string[]} */ (controlValue).every((email) => {
return email.trim().length === email.length;
}),
true,
);

deepEqual(controlValue, emails);
});
});

describe('should get value from file inputs correctly', () => {
test('should get value from singular file input correctly', async () => {
const file = [new File(['test-file'], 'test-file')];
Expand Down
Loading

0 comments on commit 71bc62f

Please sign in to comment.