@conform-to/dom 0.7.4 → 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,18 +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
31
  export declare function resolve(payload: FormData | URLSearchParams, options?: {
32
32
  ignoreKeys?: string[];
33
- stripEmptyValue?: boolean;
34
33
  }): {};
35
34
  /**
36
35
  * Format the error messages into a validation message
37
36
  */
38
- export declare function getValidationMessage(errors?: string | string[]): string;
37
+ export declare function getValidationMessage(errors?: string[]): string;
39
38
  /**
40
39
  * Retrieve the error messages from the validation message
41
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
  }
@@ -85,31 +85,23 @@ function setValue(target, name, valueFn) {
85
85
  function resolve(payload) {
86
86
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
87
87
  var data = {};
88
- var _loop = function _loop() {
88
+ var _loop = function _loop(value) {
89
89
  var _options$ignoreKeys;
90
90
  if ((_options$ignoreKeys = options.ignoreKeys) !== null && _options$ignoreKeys !== void 0 && _options$ignoreKeys.includes(key)) {
91
91
  return "continue";
92
92
  }
93
- var next = value;
94
- if (options.stripEmptyValue && (typeof next === 'string' ? next === '' : next.name === '' && next.size === 0)) {
95
- // Set the value to undefined instead of skipping it
96
- // to maintain the data structure
97
- next = undefined;
98
- }
99
93
  setValue(data, key, prev => {
100
94
  if (!prev) {
101
- return next;
102
- } else if (!next) {
103
- return prev;
95
+ return value;
104
96
  } else if (Array.isArray(prev)) {
105
- return prev.concat(next);
97
+ return prev.concat(value);
106
98
  } else {
107
- return [prev, next];
99
+ return [prev, value];
108
100
  }
109
101
  });
110
102
  };
111
103
  for (var [key, value] of payload.entries()) {
112
- var _ret = _loop();
104
+ var _ret = _loop(value);
113
105
  if (_ret === "continue") continue;
114
106
  }
115
107
  return data;
@@ -119,17 +111,16 @@ function resolve(payload) {
119
111
  * Format the error messages into a validation message
120
112
  */
121
113
  function getValidationMessage(errors) {
122
- 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 : '';
123
116
  }
124
117
 
125
118
  /**
126
119
  * Retrieve the error messages from the validation message
127
120
  */
128
121
  function getErrors(validationMessage) {
129
- if (!validationMessage) {
130
- return [];
131
- }
132
- 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 : [];
133
124
  }
134
125
 
135
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
  }
@@ -81,31 +81,23 @@ function setValue(target, name, valueFn) {
81
81
  function resolve(payload) {
82
82
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
83
83
  var data = {};
84
- var _loop = function _loop() {
84
+ var _loop = function _loop(value) {
85
85
  var _options$ignoreKeys;
86
86
  if ((_options$ignoreKeys = options.ignoreKeys) !== null && _options$ignoreKeys !== void 0 && _options$ignoreKeys.includes(key)) {
87
87
  return "continue";
88
88
  }
89
- var next = value;
90
- if (options.stripEmptyValue && (typeof next === 'string' ? next === '' : next.name === '' && next.size === 0)) {
91
- // Set the value to undefined instead of skipping it
92
- // to maintain the data structure
93
- next = undefined;
94
- }
95
89
  setValue(data, key, prev => {
96
90
  if (!prev) {
97
- return next;
98
- } else if (!next) {
99
- return prev;
91
+ return value;
100
92
  } else if (Array.isArray(prev)) {
101
- return prev.concat(next);
93
+ return prev.concat(value);
102
94
  } else {
103
- return [prev, next];
95
+ return [prev, value];
104
96
  }
105
97
  });
106
98
  };
107
99
  for (var [key, value] of payload.entries()) {
108
- var _ret = _loop();
100
+ var _ret = _loop(value);
109
101
  if (_ret === "continue") continue;
110
102
  }
111
103
  return data;
@@ -115,17 +107,16 @@ function resolve(payload) {
115
107
  * Format the error messages into a validation message
116
108
  */
117
109
  function getValidationMessage(errors) {
118
- 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 : '';
119
112
  }
120
113
 
121
114
  /**
122
115
  * Retrieve the error messages from the validation message
123
116
  */
124
117
  function getErrors(validationMessage) {
125
- if (!validationMessage) {
126
- return [];
127
- }
128
- 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 : [];
129
120
  }
130
121
 
131
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.4",
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,24 +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
- stripEmptyValue?: boolean;
16
15
  }): Submission<Schema>;
17
16
  export declare function parse<Schema>(payload: FormData | URLSearchParams, options?: {
18
17
  resolve?: (payload: Record<string, any>, intent: string) => Promise<{
19
18
  value?: Schema;
20
- error?: Record<string, string | string[]>;
19
+ error?: Record<string, string[]>;
21
20
  }>;
22
- stripEmptyValue?: boolean;
23
21
  }): Promise<Submission<Schema>>;
24
22
  export declare function parse<Schema>(payload: FormData | URLSearchParams, options?: {
25
23
  resolve?: (payload: Record<string, any>, intent: string) => {
26
24
  value?: Schema;
27
- error?: Record<string, string | string[]>;
25
+ error?: Record<string, string[]>;
28
26
  } | Promise<{
29
27
  value?: Schema;
30
- error?: Record<string, string | string[]>;
28
+ error?: Record<string, string[]>;
31
29
  }>;
32
- stripEmptyValue?: boolean;
33
30
  }): Submission<Schema> | Promise<Submission<Schema>>;
package/parse.js CHANGED
@@ -12,8 +12,7 @@ function parse(payload, options) {
12
12
  var submission = {
13
13
  intent: intent.getIntent(payload),
14
14
  payload: formdata.resolve(payload, {
15
- ignoreKeys: [intent.INTENT],
16
- stripEmptyValue: options === null || options === void 0 ? void 0 : options.stripEmptyValue
15
+ ignoreKeys: [intent.INTENT]
17
16
  }),
18
17
  error: {}
19
18
  };
package/parse.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { objectSpread2 as _objectSpread2 } from './_virtual/_rollupPluginBabelHelpers.mjs';
2
2
  import { resolve, setValue } from './formdata.mjs';
3
- import { getIntent, INTENT, parseIntent, updateList } from './intent.mjs';
3
+ import { getIntent, parseIntent, updateList, INTENT } from './intent.mjs';
4
4
 
5
5
  var VALIDATION_UNDEFINED = '__undefined__';
6
6
  var VALIDATION_SKIPPED = '__skipped__';
@@ -8,8 +8,7 @@ function parse(payload, options) {
8
8
  var submission = {
9
9
  intent: getIntent(payload),
10
10
  payload: resolve(payload, {
11
- ignoreKeys: [INTENT],
12
- stripEmptyValue: options === null || options === void 0 ? void 0 : options.stripEmptyValue
11
+ ignoreKeys: [INTENT]
13
12
  }),
14
13
  error: {}
15
14
  };