@conform-to/dom 0.7.3 → 0.8.0-pre.0

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.
package/formdata.d.ts CHANGED
@@ -24,15 +24,17 @@ export declare function formatPaths(paths: Array<string | number>): string;
24
24
  /**
25
25
  * Assign a value to a target object by following the paths on the name
26
26
  */
27
- export declare function setValue(target: any, name: string, valueFn: (prev?: unknown) => any): void;
27
+ export declare function setValue(target: Record<string, any>, name: string, valueFn: (prev?: unknown) => any): void;
28
28
  /**
29
29
  * Resolves the payload into a plain object based on the JS syntax convention
30
30
  */
31
- export declare function resolve(payload: FormData | URLSearchParams, ignoreKeys?: string[]): {};
31
+ export declare function resolve(payload: FormData | URLSearchParams, options?: {
32
+ ignoreKeys?: string[];
33
+ }): {};
32
34
  /**
33
35
  * Format the error messages into a validation message
34
36
  */
35
- export declare function getValidationMessage(errors?: string | string[]): string;
37
+ export declare function getValidationMessage(errors?: string[]): string;
36
38
  /**
37
39
  * Retrieve the error messages from the validation message
38
40
  */
package/formdata.js CHANGED
@@ -25,20 +25,20 @@ function getFormData(form, submitter) {
25
25
  * ```
26
26
  */
27
27
  function getPaths(name) {
28
- var pattern = /(\w*)\[(\d+)\]/;
29
28
  if (!name) {
30
29
  return [];
31
30
  }
32
- return name.split('.').flatMap(key => {
33
- var matches = pattern.exec(key);
34
- if (!matches) {
35
- return key;
36
- }
37
- if (matches[1] === '') {
38
- return Number(matches[2]);
31
+ return name.split(/\.|(\[\d*\])/).reduce((result, segment) => {
32
+ if (typeof segment !== 'undefined' && segment !== '') {
33
+ if (segment.startsWith('[') && segment.endsWith(']')) {
34
+ var index = segment.slice(1, -1);
35
+ result.push(Number(index));
36
+ } else {
37
+ result.push(segment);
38
+ }
39
39
  }
40
- return [matches[1], Number(matches[2])];
41
- });
40
+ return result;
41
+ }, []);
42
42
  }
43
43
 
44
44
  /**
@@ -72,8 +72,8 @@ function setValue(target, name, valueFn) {
72
72
  while (pointer != null && ++index < length) {
73
73
  var _pointer$key;
74
74
  var key = paths[index];
75
- var next = paths[index + 1];
76
- var newValue = index != lastIndex ? (_pointer$key = pointer[key]) !== null && _pointer$key !== void 0 ? _pointer$key : typeof next === 'number' ? [] : {} : valueFn(pointer[key]);
75
+ var nextKey = paths[index + 1];
76
+ var newValue = index != lastIndex ? (_pointer$key = pointer[key]) !== null && _pointer$key !== void 0 ? _pointer$key : typeof nextKey === 'number' ? [] : {} : valueFn(pointer[key]);
77
77
  pointer[key] = newValue;
78
78
  pointer = pointer[key];
79
79
  }
@@ -82,10 +82,12 @@ function setValue(target, name, valueFn) {
82
82
  /**
83
83
  * Resolves the payload into a plain object based on the JS syntax convention
84
84
  */
85
- function resolve(payload, ignoreKeys) {
85
+ function resolve(payload) {
86
+ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
86
87
  var data = {};
87
88
  var _loop = function _loop(value) {
88
- if (ignoreKeys !== null && ignoreKeys !== void 0 && ignoreKeys.includes(key)) {
89
+ var _options$ignoreKeys;
90
+ if ((_options$ignoreKeys = options.ignoreKeys) !== null && _options$ignoreKeys !== void 0 && _options$ignoreKeys.includes(key)) {
89
91
  return "continue";
90
92
  }
91
93
  setValue(data, key, prev => {
@@ -109,17 +111,16 @@ function resolve(payload, ignoreKeys) {
109
111
  * Format the error messages into a validation message
110
112
  */
111
113
  function getValidationMessage(errors) {
112
- return [].concat(errors !== null && errors !== void 0 ? errors : []).join(String.fromCharCode(31));
114
+ var _errors$join;
115
+ return (_errors$join = errors === null || errors === void 0 ? void 0 : errors.join(String.fromCharCode(31))) !== null && _errors$join !== void 0 ? _errors$join : '';
113
116
  }
114
117
 
115
118
  /**
116
119
  * Retrieve the error messages from the validation message
117
120
  */
118
121
  function getErrors(validationMessage) {
119
- if (!validationMessage) {
120
- return [];
121
- }
122
- return validationMessage.split(String.fromCharCode(31));
122
+ var _validationMessage$sp;
123
+ return (_validationMessage$sp = validationMessage === null || validationMessage === void 0 ? void 0 : validationMessage.split(String.fromCharCode(31))) !== null && _validationMessage$sp !== void 0 ? _validationMessage$sp : [];
123
124
  }
124
125
 
125
126
  exports.formatPaths = formatPaths;
package/formdata.mjs CHANGED
@@ -21,20 +21,20 @@ function getFormData(form, submitter) {
21
21
  * ```
22
22
  */
23
23
  function getPaths(name) {
24
- var pattern = /(\w*)\[(\d+)\]/;
25
24
  if (!name) {
26
25
  return [];
27
26
  }
28
- return name.split('.').flatMap(key => {
29
- var matches = pattern.exec(key);
30
- if (!matches) {
31
- return key;
32
- }
33
- if (matches[1] === '') {
34
- return Number(matches[2]);
27
+ return name.split(/\.|(\[\d*\])/).reduce((result, segment) => {
28
+ if (typeof segment !== 'undefined' && segment !== '') {
29
+ if (segment.startsWith('[') && segment.endsWith(']')) {
30
+ var index = segment.slice(1, -1);
31
+ result.push(Number(index));
32
+ } else {
33
+ result.push(segment);
34
+ }
35
35
  }
36
- return [matches[1], Number(matches[2])];
37
- });
36
+ return result;
37
+ }, []);
38
38
  }
39
39
 
40
40
  /**
@@ -68,8 +68,8 @@ function setValue(target, name, valueFn) {
68
68
  while (pointer != null && ++index < length) {
69
69
  var _pointer$key;
70
70
  var key = paths[index];
71
- var next = paths[index + 1];
72
- var newValue = index != lastIndex ? (_pointer$key = pointer[key]) !== null && _pointer$key !== void 0 ? _pointer$key : typeof next === 'number' ? [] : {} : valueFn(pointer[key]);
71
+ var nextKey = paths[index + 1];
72
+ var newValue = index != lastIndex ? (_pointer$key = pointer[key]) !== null && _pointer$key !== void 0 ? _pointer$key : typeof nextKey === 'number' ? [] : {} : valueFn(pointer[key]);
73
73
  pointer[key] = newValue;
74
74
  pointer = pointer[key];
75
75
  }
@@ -78,10 +78,12 @@ function setValue(target, name, valueFn) {
78
78
  /**
79
79
  * Resolves the payload into a plain object based on the JS syntax convention
80
80
  */
81
- function resolve(payload, ignoreKeys) {
81
+ function resolve(payload) {
82
+ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
82
83
  var data = {};
83
84
  var _loop = function _loop(value) {
84
- if (ignoreKeys !== null && ignoreKeys !== void 0 && ignoreKeys.includes(key)) {
85
+ var _options$ignoreKeys;
86
+ if ((_options$ignoreKeys = options.ignoreKeys) !== null && _options$ignoreKeys !== void 0 && _options$ignoreKeys.includes(key)) {
85
87
  return "continue";
86
88
  }
87
89
  setValue(data, key, prev => {
@@ -105,17 +107,16 @@ function resolve(payload, ignoreKeys) {
105
107
  * Format the error messages into a validation message
106
108
  */
107
109
  function getValidationMessage(errors) {
108
- return [].concat(errors !== null && errors !== void 0 ? errors : []).join(String.fromCharCode(31));
110
+ var _errors$join;
111
+ return (_errors$join = errors === null || errors === void 0 ? void 0 : errors.join(String.fromCharCode(31))) !== null && _errors$join !== void 0 ? _errors$join : '';
109
112
  }
110
113
 
111
114
  /**
112
115
  * Retrieve the error messages from the validation message
113
116
  */
114
117
  function getErrors(validationMessage) {
115
- if (!validationMessage) {
116
- return [];
117
- }
118
- return validationMessage.split(String.fromCharCode(31));
118
+ var _validationMessage$sp;
119
+ return (_validationMessage$sp = validationMessage === null || validationMessage === void 0 ? void 0 : validationMessage.split(String.fromCharCode(31))) !== null && _validationMessage$sp !== void 0 ? _validationMessage$sp : [];
119
120
  }
120
121
 
121
122
  export { formatPaths, getErrors, getFormData, getPaths, getValidationMessage, resolve, setValue };
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "A set of opinionated helpers built on top of the Constraint Validation API",
4
4
  "homepage": "https://conform.guide",
5
5
  "license": "MIT",
6
- "version": "0.7.3",
6
+ "version": "0.8.0-pre.0",
7
7
  "main": "index.js",
8
8
  "module": "index.mjs",
9
9
  "types": "index.d.ts",
package/parse.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  export type Submission<Schema = any> = {
2
2
  intent: string;
3
3
  payload: Record<string, any>;
4
- error: Record<string, string | string[]>;
4
+ error: Record<string, string[]>;
5
5
  value?: Schema | null;
6
6
  };
7
7
  export declare const VALIDATION_UNDEFINED = "__undefined__";
@@ -10,21 +10,21 @@ export declare function parse(payload: FormData | URLSearchParams): Submission;
10
10
  export declare function parse<Schema>(payload: FormData | URLSearchParams, options?: {
11
11
  resolve?: (payload: Record<string, any>, intent: string) => {
12
12
  value?: Schema;
13
- error?: Record<string, string | string[]>;
13
+ error?: Record<string, string[]>;
14
14
  };
15
15
  }): Submission<Schema>;
16
16
  export declare function parse<Schema>(payload: FormData | URLSearchParams, options?: {
17
17
  resolve?: (payload: Record<string, any>, intent: string) => Promise<{
18
18
  value?: Schema;
19
- error?: Record<string, string | string[]>;
19
+ error?: Record<string, string[]>;
20
20
  }>;
21
21
  }): Promise<Submission<Schema>>;
22
22
  export declare function parse<Schema>(payload: FormData | URLSearchParams, options?: {
23
23
  resolve?: (payload: Record<string, any>, intent: string) => {
24
24
  value?: Schema;
25
- error?: Record<string, string | string[]>;
25
+ error?: Record<string, string[]>;
26
26
  } | Promise<{
27
27
  value?: Schema;
28
- error?: Record<string, string | string[]>;
28
+ error?: Record<string, string[]>;
29
29
  }>;
30
30
  }): Submission<Schema> | Promise<Submission<Schema>>;
package/parse.js CHANGED
@@ -11,7 +11,9 @@ var VALIDATION_SKIPPED = '__skipped__';
11
11
  function parse(payload, options) {
12
12
  var submission = {
13
13
  intent: intent.getIntent(payload),
14
- payload: formdata.resolve(payload, [intent.INTENT]),
14
+ payload: formdata.resolve(payload, {
15
+ ignoreKeys: [intent.INTENT]
16
+ }),
15
17
  error: {}
16
18
  };
17
19
  var intent$1 = intent.parseIntent(submission.intent);
package/parse.mjs CHANGED
@@ -7,7 +7,9 @@ var VALIDATION_SKIPPED = '__skipped__';
7
7
  function parse(payload, options) {
8
8
  var submission = {
9
9
  intent: getIntent(payload),
10
- payload: resolve(payload, [INTENT]),
10
+ payload: resolve(payload, {
11
+ ignoreKeys: [INTENT]
12
+ }),
11
13
  error: {}
12
14
  };
13
15
  var intent = parseIntent(submission.intent);