Skip to content

Commit

Permalink
style: add prefixes to all helpers/enums/constats fp-69 (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
what1s1ove authored Dec 13, 2023
1 parent c8448ae commit 2511c94
Show file tree
Hide file tree
Showing 40 changed files with 523 additions and 459 deletions.
12 changes: 6 additions & 6 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,6 @@ const overridesConfigs = [
'import/no-default-export': ['off'],
},
},
{
files: ['src/libs/enums/control-type.enum.js'],
rules: {
'perfectionist/sort-objects': ['off'],
},
},
{
files: ['lint-staged.config.js'],
rules: {
Expand All @@ -184,6 +178,12 @@ const overridesConfigs = [
'sonarjs/cognitive-complexity': ['off'],
},
},
{
files: ['src/libs/enums/control-element-type.enum.js'],
rules: {
'perfectionist/sort-objects': ['off'],
},
},
];

/** @type {import('eslint').Linter.FlatConfig[]} */
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { ControlType } from './libs/enums/enums.js';
export { ControlElementType } 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';
10 changes: 10 additions & 0 deletions src/libs/constants/banned-control-element-types.constant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ControlElementType } from '../enums/enums.js';

const BANNED_CONTROL_ELEMENT_TYPES = /** @type {const} */ ([
ControlElementType.BUTTON,
ControlElementType.RESET,
ControlElementType.SUBMIT,
ControlElementType.IMAGE,
]);

export { BANNED_CONTROL_ELEMENT_TYPES };
10 changes: 0 additions & 10 deletions src/libs/constants/banned-control-types.constant.js

This file was deleted.

4 changes: 2 additions & 2 deletions src/libs/constants/constants.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { BANNED_CONTROL_TYPES } from './banned-control-types.constant.js';
export { VALUE_AS_ARRAY_CUSTOM_CONTROL_TYPES } from './value-as-array-custom-control-types.constant.js';
export { BANNED_CONTROL_ELEMENT_TYPES } from './banned-control-element-types.constant.js';
export { VALUE_AS_ARRAY_CUSTOM_CONTROL_ELEMENT_TYPES } from './value-as-array-custom-control-element-types.constant.js';
export { VALUE_AS_ARRAY_IDENTIFIER } from './value-as-array-identifier.constant.js';
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ControlElementType } from '../enums/enums.js';

const VALUE_AS_ARRAY_CUSTOM_CONTROL_ELEMENT_TYPES = [
ControlElementType.CHECKBOX,
ControlElementType.FIELDSET,
];

export { VALUE_AS_ARRAY_CUSTOM_CONTROL_ELEMENT_TYPES };

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const ControlType = /** @type {const} */ ({
const ControlElementType = /** @type {const} */ ({
TEXT: 'text',
PASSWORD: 'password',
SEARCH: 'search',
Expand Down Expand Up @@ -39,4 +39,4 @@ const ControlType = /** @type {const} */ ({
FIELDSET: 'fieldset',
});

export { ControlType };
export { ControlElementType };
2 changes: 1 addition & 1 deletion src/libs/enums/enums.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { ControlType } from './control-type.enum.js';
export { ControlElementType } from './control-element-type.enum.js';
114 changes: 60 additions & 54 deletions src/packages/get-form-control-payload/get-form-control-payload.js
Original file line number Diff line number Diff line change
@@ -1,118 +1,124 @@
import { ControlType } from '../../libs/enums/enums.js';
import { ControlElementType } from '../../libs/enums/enums.js';
import { FormPayloadError } from '../../libs/exceptions/exceptions.js';
import {
getCheckboxValue,
getDatetimeLocalValue,
getFieldsetValue,
getFormControlValue,
getInputDateValue,
getInputEmailValue,
getInputFileValue,
getInputNumericValue,
getMultiSelectValues,
getCheckboxControlElementValue,
getControlElementValue,
getDateControlElementValue,
getDatetimeLocaControlElementValue,
getEmailControlElementValue,
getFieldsetControlElementValue,
getFileControlElementValue,
getMultiselectControlElementValue,
getNumericControlElementValue,
} from './helpers/helpers.js';

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

/**
* @template {unknown} T
* @param {HTMLFormOperationalControlElement} controlNode
* @param {HTMLFormOperationalControlElement} controlElement
* @returns {T}
* @throws {FormPayloadError}
*/
const getFormControlPayload = (controlNode) => {
const hasType = 'type' in controlNode;
const getFormControlPayload = (controlElement) => {
const hasType = 'type' in controlElement;

if (!hasType) {
throw new FormPayloadError({
message: 'Control element has no type attribute.',
});
}

switch (controlNode.type) {
case ControlType.TEXT:
case ControlType.PASSWORD:
case ControlType.SEARCH:
case ControlType.URL:
case ControlType.TEL:
case ControlType.COLOR:
case ControlType.RADIO:
case ControlType.HIDDEN:
case ControlType.TEXTAREA:
case ControlType.SELECT_ONE:
case ControlType.OUTPUT: {
switch (controlElement.type) {
case ControlElementType.TEXT:
case ControlElementType.PASSWORD:
case ControlElementType.SEARCH:
case ControlElementType.URL:
case ControlElementType.TEL:
case ControlElementType.COLOR:
case ControlElementType.RADIO:
case ControlElementType.HIDDEN:
case ControlElementType.TEXTAREA:
case ControlElementType.SELECT_ONE:
case ControlElementType.OUTPUT: {
return /** @type {T} */ (
getFormControlValue(
getControlElementValue(
/**
* @type {HTMLInputElement
* | HTMLOutputElement
* | HTMLTextAreaElement
* | HTMLSelectElement}
*/ (controlNode),
*/ (controlElement),
)
);
}
case ControlType.EMAIL: {
case ControlElementType.EMAIL: {
return /** @type {T} */ (
getInputEmailValue(
/** @type {HTMLInputElement} */ (controlNode),
getEmailControlElementValue(
/** @type {HTMLInputElement} */ (controlElement),
)
);
}
case ControlType.NUMBER:
case ControlType.RANGE: {
case ControlElementType.NUMBER:
case ControlElementType.RANGE: {
return /** @type {T} */ (
getInputNumericValue(
/** @type {HTMLInputElement} */ (controlNode),
getNumericControlElementValue(
/** @type {HTMLInputElement} */ (controlElement),
)
);
}
case ControlType.CHECKBOX: {
case ControlElementType.CHECKBOX: {
return /** @type {T} */ (
getCheckboxValue(/** @type {HTMLInputElement} */ (controlNode))
getCheckboxControlElementValue(
/** @type {HTMLInputElement} */ (controlElement),
)
);
}
case ControlType.DATE:
case ControlType.TIME:
case ControlType.MONTH:
case ControlType.WEEK: {
case ControlElementType.DATE:
case ControlElementType.TIME:
case ControlElementType.MONTH:
case ControlElementType.WEEK: {
return /** @type {T} */ (
getInputDateValue(/** @type {HTMLInputElement} */ (controlNode))
getDateControlElementValue(
/** @type {HTMLInputElement} */ (controlElement),
)
);
}
case ControlType.DATETIME_LOCAL: {
case ControlElementType.DATETIME_LOCAL: {
return /** @type {T} */ (
getDatetimeLocalValue(
/** @type {HTMLInputElement} */ (controlNode),
getDatetimeLocaControlElementValue(
/** @type {HTMLInputElement} */ (controlElement),
)
);
}
case ControlType.FILE: {
case ControlElementType.FILE: {
return /** @type {T} */ (
getInputFileValue(/** @type {HTMLInputElement} */ (controlNode))
getFileControlElementValue(
/** @type {HTMLInputElement} */ (controlElement),
)
);
}
case ControlType.SELECT_MULTIPLE: {
case ControlElementType.SELECT_MULTIPLE: {
return /** @type {T} */ (
getMultiSelectValues(
/** @type {HTMLSelectElement} */ (controlNode),
getMultiselectControlElementValue(
/** @type {HTMLSelectElement} */ (controlElement),
)
);
}
case ControlType.FIELDSET: {
case ControlElementType.FIELDSET: {
return /** @type {T} */ (
getFieldsetValue(
getFieldsetControlElementValue(
getFormControlPayload,
/** @type {HTMLFieldSetElement} */ (controlNode),
/** @type {HTMLFieldSetElement} */ (controlElement),
)
);
}
}

throw new FormPayloadError({
message: `Unsupported control element type – ${controlNode.type}.`,
message: `Unsupported control element type – ${controlElement.type}.`,
});
};

export { getFormControlsPayload } from './helpers/helpers.js';
export { getFormControlElementsPayload } from './helpers/helpers.js';
export { getFormControlPayload };
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
VALUE_AS_ARRAY_CUSTOM_CONTROL_TYPES,
VALUE_AS_ARRAY_CUSTOM_CONTROL_ELEMENT_TYPES,
VALUE_AS_ARRAY_IDENTIFIER,
} from '../../../../libs/constants/constants.js';

Expand All @@ -10,12 +10,15 @@ import {
* @returns {boolean}
*/
const checkHasValueAsArray = (element) => {
return (
element.name.endsWith(VALUE_AS_ARRAY_IDENTIFIER) &&
/** @type {readonly string[]} */ (
VALUE_AS_ARRAY_CUSTOM_CONTROL_TYPES
).includes(element.type)
const hasValueAsArrayIdentifier = element.name.endsWith(
VALUE_AS_ARRAY_IDENTIFIER,
);
const isValueAsArrayCustomControlElementType =
/** @type {readonly string[]} */ (
VALUE_AS_ARRAY_CUSTOM_CONTROL_ELEMENT_TYPES
).includes(element.type);

return hasValueAsArrayIdentifier && isValueAsArrayCustomControlElementType;
};

export { checkHasValueAsArray };
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/** @typedef {import('../../../../libs/types/types.js').HTMLFormOperationalControlElement} HTMLFormOperationalControlElement */

/**
* @param {HTMLFormOperationalControlElement} currentElement
* @param {HTMLFormOperationalControlElement[]} checkedElements
* @returns {boolean}
*/
const checkIsReferToAnotherElement = (currentElement, checkedElements) => {
return checkedElements.some((checkedElement) => {
const hasElements =
'elements' in checkedElement && checkedElement.elements.length > 0;

if (!hasElements) {
return false;
}

return [...checkedElement.elements].some((element) =>
element.contains(currentElement),
);
});
};

export { checkIsReferToAnotherElement };

This file was deleted.

Loading

0 comments on commit 2511c94

Please sign in to comment.