form-payload 0.0.4 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/common/constants/banned-control-types.constant.d.ts +1 -1
  2. package/common/constants/banned-control-types.constant.js +1 -0
  3. package/common/enums/{form → dom}/element-name.enum.d.ts +3 -0
  4. package/common/enums/{form → dom}/element-name.enum.js +3 -0
  5. package/common/enums/dom/index.d.ts +1 -0
  6. package/common/enums/dom/index.js +13 -0
  7. package/common/enums/exception/custom-exception-name.enum.d.ts +4 -0
  8. package/common/enums/exception/custom-exception-name.enum.js +7 -0
  9. package/common/enums/exception/error-message.enum.d.ts +5 -0
  10. package/common/enums/exception/error-message.enum.js +8 -0
  11. package/common/enums/exception/index.d.ts +2 -0
  12. package/common/enums/exception/index.js +14 -0
  13. package/common/enums/form/control-type.enum.d.ts +2 -1
  14. package/common/enums/form/control-type.enum.js +2 -1
  15. package/common/enums/form/index.d.ts +0 -1
  16. package/common/enums/form/index.js +0 -1
  17. package/common/enums/index.d.ts +2 -0
  18. package/common/enums/index.js +2 -0
  19. package/common/types/dom/child-element.type.d.ts +2 -0
  20. package/common/types/{object/custom-record.type.js → dom/child-element.type.js} +0 -0
  21. package/common/types/dom/control-collection.type.d.ts +2 -0
  22. package/common/types/dom/control-collection.type.js +2 -0
  23. package/common/types/dom/control-element.type.d.ts +2 -0
  24. package/common/types/dom/control-element.type.js +2 -0
  25. package/common/types/dom/index.d.ts +3 -0
  26. package/common/types/dom/index.js +15 -0
  27. package/common/types/index.d.ts +1 -0
  28. package/common/types/index.js +1 -0
  29. package/common/types/object/custom-object.type.d.ts +2 -0
  30. package/common/types/object/custom-object.type.js +2 -0
  31. package/common/types/object/index.d.ts +1 -1
  32. package/common/types/object/index.js +1 -1
  33. package/exceptions/form-payload-error/form-payload-error.exception.d.ts +7 -0
  34. package/exceptions/form-payload-error/form-payload-error.exception.js +12 -0
  35. package/exceptions/index.d.ts +1 -0
  36. package/exceptions/index.js +13 -0
  37. package/helpers/get-control-value/get-control-value.helper.d.ts +4 -2
  38. package/helpers/get-control-value/get-control-value.helper.js +33 -4
  39. package/helpers/get-control-value/helpers/check-is-refer-to-another-node/check-is-refer-to-another-node.helper.d.ts +3 -0
  40. package/helpers/{check-is-refer-to-another-node → get-control-value/helpers/check-is-refer-to-another-node}/check-is-refer-to-another-node.helper.js +0 -0
  41. package/helpers/get-control-value/helpers/get-allowed-elements/get-allowed-elements.helper.d.ts +3 -0
  42. package/helpers/{get-allowed-elements → get-control-value/helpers/get-allowed-elements}/get-allowed-elements.helper.js +1 -1
  43. package/helpers/get-control-value/helpers/get-input-file-value/get-input-file-value.helper.d.ts +2 -0
  44. package/helpers/get-control-value/helpers/get-input-file-value/get-input-file-value.helper.js +11 -0
  45. package/helpers/get-control-value/helpers/get-multi-select-values/get-multi-select-values.helper.d.ts +2 -0
  46. package/helpers/get-control-value/helpers/get-multi-select-values/get-multi-select-values.helper.js +7 -0
  47. package/helpers/get-control-value/helpers/index.d.ts +4 -0
  48. package/helpers/get-control-value/helpers/index.js +16 -0
  49. package/helpers/index.d.ts +0 -4
  50. package/helpers/index.js +0 -4
  51. package/index.d.ts +6 -4
  52. package/index.js +7 -3
  53. package/package.json +3 -3
  54. package/readme.md +1 -1
  55. package/common/types/object/custom-record.type.d.ts +0 -2
  56. package/helpers/check-is-refer-to-another-node/check-is-refer-to-another-node.helper.d.ts +0 -2
  57. package/helpers/create-element/create-element.helper.d.ts +0 -2
  58. package/helpers/create-element/create-element.helper.js +0 -12
  59. package/helpers/get-allowed-elements/get-allowed-elements.helper.d.ts +0 -2
  60. package/helpers/get-elements-values/get-elements-values.helper.d.ts +0 -3
  61. package/helpers/get-elements-values/get-elements-values.helper.js +0 -17
  62. package/index.test.d.ts +0 -1
  63. package/index.test.js +0 -96
@@ -1,2 +1,2 @@
1
- declare const BANNED_CONTROL_TYPES: readonly ["button", "reset", "submit"];
1
+ declare const BANNED_CONTROL_TYPES: readonly ["button", "image", "reset", "submit"];
2
2
  export { BANNED_CONTROL_TYPES };
@@ -4,6 +4,7 @@ exports.BANNED_CONTROL_TYPES = void 0;
4
4
  const enums_1 = require("../enums");
5
5
  const BANNED_CONTROL_TYPES = [
6
6
  enums_1.ControlType.BUTTON,
7
+ enums_1.ControlType.IMAGE,
7
8
  enums_1.ControlType.RESET,
8
9
  enums_1.ControlType.SUBMIT,
9
10
  ];
@@ -1,6 +1,9 @@
1
1
  declare const ElementName: {
2
2
  readonly FORM: "form";
3
+ readonly LABEL: "label";
3
4
  readonly INPUT: "input";
5
+ readonly SELECT: "select";
6
+ readonly OPTION: "option";
4
7
  readonly FIELDSET: "fieldset";
5
8
  };
6
9
  export { ElementName };
@@ -3,7 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ElementName = void 0;
4
4
  const ElementName = {
5
5
  FORM: 'form',
6
+ LABEL: 'label',
6
7
  INPUT: 'input',
8
+ SELECT: 'select',
9
+ OPTION: 'option',
7
10
  FIELDSET: 'fieldset',
8
11
  };
9
12
  exports.ElementName = ElementName;
@@ -0,0 +1 @@
1
+ export * from './element-name.enum';
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./element-name.enum"), exports);
@@ -0,0 +1,4 @@
1
+ declare const CustomExceptionName: {
2
+ readonly FORM_PAYLOAD_ERROR: "formPayloadError";
3
+ };
4
+ export { CustomExceptionName };
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CustomExceptionName = void 0;
4
+ const CustomExceptionName = {
5
+ FORM_PAYLOAD_ERROR: 'formPayloadError',
6
+ };
7
+ exports.CustomExceptionName = CustomExceptionName;
@@ -0,0 +1,5 @@
1
+ declare const ErrorMessage: {
2
+ readonly UNKNOWN_CONTROL_TYPE: "Unknown control type";
3
+ readonly BANNED_TYPE: "Banned type control: ";
4
+ };
5
+ export { ErrorMessage };
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ErrorMessage = void 0;
4
+ const ErrorMessage = {
5
+ UNKNOWN_CONTROL_TYPE: 'Unknown control type',
6
+ BANNED_TYPE: 'Banned type control: ',
7
+ };
8
+ exports.ErrorMessage = ErrorMessage;
@@ -0,0 +1,2 @@
1
+ export * from './custom-exception-name.enum';
2
+ export * from './error-message.enum';
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./custom-exception-name.enum"), exports);
14
+ __exportStar(require("./error-message.enum"), exports);
@@ -5,7 +5,9 @@ declare const ControlType: {
5
5
  readonly DATE: "date";
6
6
  readonly DATETIME_LOCAL: "datetime-local";
7
7
  readonly EMAIL: "email";
8
+ readonly FILE: "file";
8
9
  readonly HIDDEN: "hidden";
10
+ readonly IMAGE: "image";
9
11
  readonly MONTH: "month";
10
12
  readonly NUMBER: "number";
11
13
  readonly PASSWORD: "password";
@@ -24,6 +26,5 @@ declare const ControlType: {
24
26
  readonly OUTPUT: "output";
25
27
  readonly SELECT_ONE: "select-one";
26
28
  readonly SELECT_MULTIPLE: "select-multiple";
27
- readonly FILE: "file";
28
29
  };
29
30
  export { ControlType };
@@ -8,7 +8,9 @@ const ControlType = {
8
8
  DATE: 'date',
9
9
  DATETIME_LOCAL: 'datetime-local',
10
10
  EMAIL: 'email',
11
+ FILE: 'file',
11
12
  HIDDEN: 'hidden',
13
+ IMAGE: 'image',
12
14
  MONTH: 'month',
13
15
  NUMBER: 'number',
14
16
  PASSWORD: 'password',
@@ -27,6 +29,5 @@ const ControlType = {
27
29
  OUTPUT: 'output',
28
30
  SELECT_ONE: 'select-one',
29
31
  SELECT_MULTIPLE: 'select-multiple',
30
- FILE: 'file',
31
32
  };
32
33
  exports.ControlType = ControlType;
@@ -1,2 +1 @@
1
1
  export * from './control-type.enum';
2
- export * from './element-name.enum';
@@ -11,4 +11,3 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
11
11
  };
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
13
  __exportStar(require("./control-type.enum"), exports);
14
- __exportStar(require("./element-name.enum"), exports);
@@ -1 +1,3 @@
1
+ export * from './dom';
2
+ export * from './exception';
1
3
  export * from './form';
@@ -10,4 +10,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
10
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
11
  };
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./dom"), exports);
14
+ __exportStar(require("./exception"), exports);
13
15
  __exportStar(require("./form"), exports);
@@ -0,0 +1,2 @@
1
+ declare type ChildElement = HTMLElement | string;
2
+ export type { ChildElement };
@@ -0,0 +1,2 @@
1
+ declare type ControlCollection = HTMLCollection | HTMLFormControlsCollection;
2
+ export type { ControlCollection };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ declare type ControlElement = HTMLInputElement | HTMLSelectElement | HTMLOutputElement | HTMLFieldSetElement;
2
+ export type { ControlElement };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,3 @@
1
+ export * from './child-element.type';
2
+ export * from './control-collection.type';
3
+ export * from './control-element.type';
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./child-element.type"), exports);
14
+ __exportStar(require("./control-collection.type"), exports);
15
+ __exportStar(require("./control-element.type"), exports);
@@ -1 +1,2 @@
1
1
  export * from './object';
2
+ export * from './dom';
@@ -11,3 +11,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
11
11
  };
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
13
  __exportStar(require("./object"), exports);
14
+ __exportStar(require("./dom"), exports);
@@ -0,0 +1,2 @@
1
+ declare type CustomObject<T = unknown> = Record<string, T>;
2
+ export type { CustomObject };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1 +1 @@
1
- export * from './custom-record.type';
1
+ export * from './custom-object.type';
@@ -10,4 +10,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
10
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
11
  };
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
- __exportStar(require("./custom-record.type"), exports);
13
+ __exportStar(require("./custom-object.type"), exports);
@@ -0,0 +1,7 @@
1
+ declare type Constructor = {
2
+ message: string;
3
+ };
4
+ declare class FormPayloadError extends Error {
5
+ constructor({ message }: Constructor);
6
+ }
7
+ export { FormPayloadError };
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FormPayloadError = void 0;
4
+ const enums_1 = require("../../common/enums");
5
+ class FormPayloadError extends Error {
6
+ constructor({ message }) {
7
+ super(message);
8
+ this.message = message;
9
+ this.name = enums_1.CustomExceptionName.FORM_PAYLOAD_ERROR;
10
+ }
11
+ }
12
+ exports.FormPayloadError = FormPayloadError;
@@ -0,0 +1 @@
1
+ export * from './form-payload-error/form-payload-error.exception';
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./form-payload-error/form-payload-error.exception"), exports);
@@ -1,2 +1,4 @@
1
- declare const getControlValue: (controlNode: Element) => unknown;
2
- export { getControlValue };
1
+ import { CustomObject, ControlElement, ControlCollection } from '../../common/types';
2
+ declare const getElementsValues: (controlElements: ControlCollection) => CustomObject;
3
+ declare const getControlValue: (controlNode: ControlElement) => unknown | never;
4
+ export { getElementsValues, getControlValue };
@@ -1,10 +1,30 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getControlValue = void 0;
3
+ exports.getControlValue = exports.getElementsValues = void 0;
4
4
  const enums_1 = require("../../common/enums");
5
- const get_elements_values_helper_1 = require("../get-elements-values/get-elements-values.helper");
5
+ const exceptions_1 = require("../../exceptions");
6
+ const helpers_1 = require("./helpers");
7
+ const getElementsValues = (controlElements) => {
8
+ const allowedElements = helpers_1.getAllowedElements(Array.from(controlElements));
9
+ return allowedElements.reduce((acc, element) => {
10
+ const isReferToAnotherNode = helpers_1.checkIsReferToAnotherNode(element, ...allowedElements);
11
+ if (isReferToAnotherNode) {
12
+ return acc;
13
+ }
14
+ return Object.assign(Object.assign({}, acc), { [element.name]: getControlValue(element) });
15
+ }, {});
16
+ };
17
+ exports.getElementsValues = getElementsValues;
6
18
  const getControlValue = (controlNode) => {
7
19
  switch (controlNode.type) {
20
+ case enums_1.ControlType.BUTTON:
21
+ case enums_1.ControlType.IMAGE:
22
+ case enums_1.ControlType.RESET:
23
+ case enums_1.ControlType.SUBMIT: {
24
+ throw new exceptions_1.FormPayloadError({
25
+ message: `${enums_1.ErrorMessage.BANNED_TYPE}${controlNode.type}`,
26
+ });
27
+ }
8
28
  case enums_1.ControlType.COLOR:
9
29
  case enums_1.ControlType.EMAIL:
10
30
  case enums_1.ControlType.HIDDEN:
@@ -13,9 +33,9 @@ const getControlValue = (controlNode) => {
13
33
  case enums_1.ControlType.SEARCH:
14
34
  case enums_1.ControlType.TEL:
15
35
  case enums_1.ControlType.TEXT:
16
- case enums_1.ControlType.TEXTAREA:
17
36
  case enums_1.ControlType.URL:
18
37
  case enums_1.ControlType.OUTPUT:
38
+ case enums_1.ControlType.TEXTAREA:
19
39
  case enums_1.ControlType.SELECT_ONE: {
20
40
  return controlNode.value;
21
41
  }
@@ -33,9 +53,18 @@ const getControlValue = (controlNode) => {
33
53
  case enums_1.ControlType.CHECKBOX: {
34
54
  return controlNode.checked;
35
55
  }
56
+ case enums_1.ControlType.SELECT_MULTIPLE: {
57
+ return helpers_1.getMultiSelectValues(controlNode);
58
+ }
59
+ case enums_1.ControlType.FILE: {
60
+ return helpers_1.getInputFileValue(controlNode);
61
+ }
36
62
  case enums_1.ControlType.FIELDSET: {
37
- return get_elements_values_helper_1.getElementsValues((Array.from(controlNode.elements)));
63
+ return getElementsValues(controlNode.elements);
38
64
  }
39
65
  }
66
+ throw new exceptions_1.FormPayloadError({
67
+ message: enums_1.ErrorMessage.UNKNOWN_CONTROL_TYPE,
68
+ });
40
69
  };
41
70
  exports.getControlValue = getControlValue;
@@ -0,0 +1,3 @@
1
+ import { ControlElement } from '../../../../common/types';
2
+ declare const checkIsReferToAnotherNode: (currentNode: ControlElement, ...checkNodes: ControlElement[]) => boolean;
3
+ export { checkIsReferToAnotherNode };
@@ -0,0 +1,3 @@
1
+ import { ControlElement } from '../../../../common/types';
2
+ declare const getAllowedElements: (elements: ControlElement[]) => ControlElement[];
3
+ export { getAllowedElements };
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getAllowedElements = void 0;
4
- const constants_1 = require("../../common/constants");
4
+ const constants_1 = require("../../../../common/constants");
5
5
  const checkControlFnMap = {
6
6
  checkHasControlName(element) {
7
7
  return Boolean(element.name);
@@ -0,0 +1,2 @@
1
+ declare const getInputFileValue: (inputNode: HTMLInputElement) => File[] | File | null;
2
+ export { getInputFileValue };
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getInputFileValue = void 0;
4
+ const DEFAULT_FILE_IDX = 0;
5
+ const getInputFileValue = (inputNode) => {
6
+ var _a, _b, _c;
7
+ return inputNode.multiple
8
+ ? Array.from((_a = inputNode.files) !== null && _a !== void 0 ? _a : [])
9
+ : (_c = (_b = inputNode.files) === null || _b === void 0 ? void 0 : _b[DEFAULT_FILE_IDX]) !== null && _c !== void 0 ? _c : null;
10
+ };
11
+ exports.getInputFileValue = getInputFileValue;
@@ -0,0 +1,2 @@
1
+ declare const getMultiSelectValues: (selectNode: HTMLSelectElement) => string[];
2
+ export { getMultiSelectValues };
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getMultiSelectValues = void 0;
4
+ const getMultiSelectValues = (selectNode) => {
5
+ return Array.from(selectNode.selectedOptions, (opt) => opt.value);
6
+ };
7
+ exports.getMultiSelectValues = getMultiSelectValues;
@@ -0,0 +1,4 @@
1
+ export * from './check-is-refer-to-another-node/check-is-refer-to-another-node.helper';
2
+ export * from './get-allowed-elements/get-allowed-elements.helper';
3
+ export * from './get-input-file-value/get-input-file-value.helper';
4
+ export * from './get-multi-select-values/get-multi-select-values.helper';
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./check-is-refer-to-another-node/check-is-refer-to-another-node.helper"), exports);
14
+ __exportStar(require("./get-allowed-elements/get-allowed-elements.helper"), exports);
15
+ __exportStar(require("./get-input-file-value/get-input-file-value.helper"), exports);
16
+ __exportStar(require("./get-multi-select-values/get-multi-select-values.helper"), exports);
@@ -1,5 +1 @@
1
- export * from './check-is-refer-to-another-node/check-is-refer-to-another-node.helper';
2
- export * from './create-element/create-element.helper';
3
- export * from './get-allowed-elements/get-allowed-elements.helper';
4
1
  export * from './get-control-value/get-control-value.helper';
5
- export * from './get-elements-values/get-elements-values.helper';
package/helpers/index.js CHANGED
@@ -10,8 +10,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
10
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
11
  };
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
- __exportStar(require("./check-is-refer-to-another-node/check-is-refer-to-another-node.helper"), exports);
14
- __exportStar(require("./create-element/create-element.helper"), exports);
15
- __exportStar(require("./get-allowed-elements/get-allowed-elements.helper"), exports);
16
13
  __exportStar(require("./get-control-value/get-control-value.helper"), exports);
17
- __exportStar(require("./get-elements-values/get-elements-values.helper"), exports);
package/index.d.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  import { getControlValue } from './helpers';
2
- import { ControlType } from './common/enums';
3
- import { CustomRecord } from './common/types';
4
- declare const getFormValues: <T = CustomRecord<unknown>>(formNode: HTMLFormElement) => T;
5
- export { ControlType, getControlValue, getFormValues };
2
+ import { FormPayloadError } from './exceptions';
3
+ import { ControlType, CustomExceptionName, ErrorMessage } from './common/enums';
4
+ import { CustomObject, ControlElement, ControlCollection } from './common/types';
5
+ declare const getFormValues: <T = CustomObject<unknown>>(nodeWithElements: HTMLFormElement | HTMLFieldSetElement) => T;
6
+ export { ControlType, CustomExceptionName, ErrorMessage, FormPayloadError, getControlValue, getFormValues, };
7
+ export type { ControlElement, ControlCollection };
package/index.js CHANGED
@@ -1,11 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getFormValues = exports.getControlValue = exports.ControlType = void 0;
3
+ exports.getFormValues = exports.getControlValue = exports.FormPayloadError = exports.ErrorMessage = exports.CustomExceptionName = exports.ControlType = void 0;
4
4
  const helpers_1 = require("./helpers");
5
5
  Object.defineProperty(exports, "getControlValue", { enumerable: true, get: function () { return helpers_1.getControlValue; } });
6
+ const exceptions_1 = require("./exceptions");
7
+ Object.defineProperty(exports, "FormPayloadError", { enumerable: true, get: function () { return exceptions_1.FormPayloadError; } });
6
8
  const enums_1 = require("./common/enums");
7
9
  Object.defineProperty(exports, "ControlType", { enumerable: true, get: function () { return enums_1.ControlType; } });
8
- const getFormValues = (formNode) => {
9
- return helpers_1.getElementsValues(Array.from(formNode.elements));
10
+ Object.defineProperty(exports, "CustomExceptionName", { enumerable: true, get: function () { return enums_1.CustomExceptionName; } });
11
+ Object.defineProperty(exports, "ErrorMessage", { enumerable: true, get: function () { return enums_1.ErrorMessage; } });
12
+ const getFormValues = (nodeWithElements) => {
13
+ return helpers_1.getElementsValues(nodeWithElements.elements);
10
14
  };
11
15
  exports.getFormValues = getFormValues;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "form-payload",
3
3
  "description": "Gets form-payload via form.elements",
4
- "version": "0.0.4",
4
+ "version": "0.0.8",
5
5
  "author": "Vladyslav Zubko <zubko.2007@gmail.com>",
6
6
  "homepage": "https://github.com/what1s1ove/form-payload",
7
7
  "repository": {
@@ -18,8 +18,7 @@
18
18
  "input-payload"
19
19
  ],
20
20
  "engines": {
21
- "node": "14.x.x",
22
- "npm": "6.x.x"
21
+ "node": ">=10"
23
22
  },
24
23
  "scripts": {
25
24
  "lint:fs": "ls-lint",
@@ -42,6 +41,7 @@
42
41
  "@typescript-eslint/parser": "4.19.0",
43
42
  "editorconfig-checker": "4.0.1",
44
43
  "eslint": "7.22.0",
44
+ "eslint-import-resolver-alias": "1.1.2",
45
45
  "eslint-plugin-jest-dom": "3.6.5",
46
46
  "jest": "26.6.3",
47
47
  "lint-staged": "10.5.4",
package/readme.md CHANGED
@@ -110,7 +110,7 @@ const SimpleForm = () => {
110
110
  </label>
111
111
  <label>
112
112
  Date
113
- <input name="date" type={ControlType.DATE} defaultValue="2021-03-27" />
113
+ <input name="birthday" type={ControlType.DATE} defaultValue="2021-03-27" />
114
114
  </label>
115
115
  <button type="submit">Submit</button>
116
116
  </form>
@@ -1,2 +0,0 @@
1
- declare type CustomRecord<T = unknown> = Record<string, T>;
2
- export type { CustomRecord };
@@ -1,2 +0,0 @@
1
- declare const checkIsReferToAnotherNode: (currentNode: HTMLInputElement, ...checkNodes: Element[]) => boolean;
2
- export { checkIsReferToAnotherNode };
@@ -1,2 +0,0 @@
1
- declare const createElement: (tagName: string, props?: Partial<HTMLFieldSetElement | HTMLInputElement> | undefined, ...children: HTMLElement[]) => HTMLElement;
2
- export { createElement };
@@ -1,12 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createElement = void 0;
4
- const createElement = (tagName, props, ...children) => {
5
- const element = document.createElement(tagName);
6
- if (props) {
7
- Object.assign(element, props);
8
- }
9
- children.forEach((child) => element.append(child));
10
- return element;
11
- };
12
- exports.createElement = createElement;
@@ -1,2 +0,0 @@
1
- declare const getAllowedElements: (elements: Element[]) => Element[];
2
- export { getAllowedElements };
@@ -1,3 +0,0 @@
1
- import { CustomRecord } from '../../common/types';
2
- declare const getElementsValues: (controlNodeElements: Element[]) => CustomRecord;
3
- export { getElementsValues };
@@ -1,17 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getElementsValues = void 0;
4
- const check_is_refer_to_another_node_helper_1 = require("../check-is-refer-to-another-node/check-is-refer-to-another-node.helper");
5
- const get_allowed_elements_helper_1 = require("../get-allowed-elements/get-allowed-elements.helper");
6
- const get_control_value_helper_1 = require("../get-control-value/get-control-value.helper");
7
- const getElementsValues = (controlNodeElements) => {
8
- const elements = (get_allowed_elements_helper_1.getAllowedElements(Array.from(controlNodeElements)));
9
- return elements.reduce((acc, element, _idx, arr) => {
10
- const isReferToAnotherNode = check_is_refer_to_another_node_helper_1.checkIsReferToAnotherNode(element, ...arr);
11
- if (isReferToAnotherNode) {
12
- return acc;
13
- }
14
- return Object.assign(Object.assign({}, acc), { [element.name]: get_control_value_helper_1.getControlValue(element) });
15
- }, {});
16
- };
17
- exports.getElementsValues = getElementsValues;
package/index.test.d.ts DELETED
@@ -1 +0,0 @@
1
- export {};
package/index.test.js DELETED
@@ -1,96 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const dom_1 = require("@testing-library/dom");
4
- const constants_1 = require("./common/constants");
5
- const helpers_1 = require("./helpers");
6
- const enums_1 = require("./common/enums");
7
- const index_1 = require("./index");
8
- const createFormElement = (...children) => {
9
- return helpers_1.createElement(enums_1.ElementName.FORM, {
10
- name: '',
11
- }, ...children);
12
- };
13
- describe('getFormValues should work correctly', () => {
14
- beforeEach(() => {
15
- document.body.innerHTML = '';
16
- });
17
- test('should skip banned types', () => {
18
- const bannedElements = constants_1.BANNED_CONTROL_TYPES.map((type) => {
19
- return helpers_1.createElement(enums_1.ElementName.INPUT, {
20
- type: type,
21
- name: type,
22
- });
23
- });
24
- document.body.append(createFormElement(...bannedElements));
25
- expect(index_1.getFormValues(dom_1.screen.queryByRole('form'))).toEqual({});
26
- });
27
- test('should skip controls without name', () => {
28
- document.body.append(createFormElement(helpers_1.createElement(enums_1.ElementName.INPUT, {
29
- name: '',
30
- })));
31
- expect(index_1.getFormValues(dom_1.screen.queryByRole('form'))).toEqual({});
32
- });
33
- test('should return the correct object with values', () => {
34
- const FormPayloadKey = {
35
- NAME: 'name',
36
- FRIENDS_COUNT: 'friendsCount',
37
- BIRTHDAY: 'birthday',
38
- };
39
- const formPayload = {
40
- [FormPayloadKey.NAME]: 'Brad',
41
- [FormPayloadKey.FRIENDS_COUNT]: 3,
42
- [FormPayloadKey.BIRTHDAY]: new Date('1992-06-13'),
43
- };
44
- document.body.append(createFormElement(helpers_1.createElement(enums_1.ElementName.INPUT, {
45
- name: FormPayloadKey.NAME,
46
- type: enums_1.ControlType.TEXT,
47
- value: formPayload.name,
48
- }), helpers_1.createElement(enums_1.ElementName.INPUT, {
49
- name: FormPayloadKey.FRIENDS_COUNT,
50
- type: enums_1.ControlType.NUMBER,
51
- valueAsNumber: formPayload.friendsCount,
52
- }), helpers_1.createElement(enums_1.ElementName.INPUT, {
53
- name: FormPayloadKey.BIRTHDAY,
54
- type: enums_1.ControlType.DATE,
55
- valueAsDate: formPayload.birthday,
56
- })));
57
- expect(index_1.getFormValues(dom_1.screen.queryByRole('form'))).toEqual(formPayload);
58
- });
59
- test('should return the correct nested object with values', () => {
60
- const FormPayloadKey = {
61
- NAME: 'name',
62
- FRIENDS_COUNT: 'friendsCount',
63
- BIRTHDAY: 'birthday',
64
- MAIN_FRIEND: 'mainFriend',
65
- MAIN_FRIEND_NAME: 'friendName',
66
- };
67
- const formPayload = {
68
- [FormPayloadKey.NAME]: 'David',
69
- [FormPayloadKey.FRIENDS_COUNT]: 2,
70
- [FormPayloadKey.BIRTHDAY]: new Date('1994-06-13'),
71
- [FormPayloadKey.MAIN_FRIEND]: {
72
- [FormPayloadKey.MAIN_FRIEND_NAME]: 'Jason',
73
- },
74
- };
75
- document.body.append(createFormElement(helpers_1.createElement(enums_1.ElementName.INPUT, {
76
- name: FormPayloadKey.NAME,
77
- type: enums_1.ControlType.TEXT,
78
- value: formPayload.name,
79
- }), helpers_1.createElement(enums_1.ElementName.INPUT, {
80
- name: FormPayloadKey.FRIENDS_COUNT,
81
- type: enums_1.ControlType.NUMBER,
82
- valueAsNumber: formPayload.friendsCount,
83
- }), helpers_1.createElement(enums_1.ElementName.INPUT, {
84
- name: FormPayloadKey.BIRTHDAY,
85
- type: enums_1.ControlType.DATE,
86
- valueAsDate: formPayload.birthday,
87
- }), helpers_1.createElement(enums_1.ElementName.FIELDSET, {
88
- name: FormPayloadKey.MAIN_FRIEND,
89
- }, helpers_1.createElement(enums_1.ElementName.INPUT, {
90
- name: FormPayloadKey.MAIN_FRIEND_NAME,
91
- type: enums_1.ControlType.TEXT,
92
- value: formPayload.mainFriend.friendName,
93
- }))));
94
- expect(index_1.getFormValues(dom_1.screen.queryByRole('form'))).toEqual(formPayload);
95
- });
96
- });