-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: get value from input email multiple as array fp-97 (#115)
* style: edit order in tsconfig fp-97 * feat: get value from input email multiple as array fp-97
- Loading branch information
1 parent
e8daf15
commit 71bc62f
Showing
7 changed files
with
129 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...es/get-form-control-payload/helpers/get-input-email-value/get-input-email-value.helper.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'], | ||
|
@@ -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')]; | ||
|
Oops, something went wrong.