@workbench-kit/field-remap 0.0.1-prototype.0 → 0.0.2-prototype.0.2.4

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.
@@ -1,243 +1,243 @@
1
- import { reformatDateString, splitDateTimeString } from '../domain/mapping/dateFormat.js';
2
- import { applyStringTemplate, isPlainObject } from '../domain/mapping/pathUtils.js';
3
- import type { ValueTransformDefinition } from '../domain/types.js';
4
- import { createValueTransformRegistry } from './createValueTransformRegistry.js';
5
-
6
- export const BUILTIN_TRANSFORM_IDS = {
7
- identity: 'identity',
8
- arrayFirst: 'array:first',
9
- arrayJoin: 'array:join',
10
- stringTrim: 'string:trim',
11
- stringUpper: 'string:upper',
12
- stringLower: 'string:lower',
13
- stringPrefix: 'string:prefix',
14
- stringSuffix: 'string:suffix',
15
- stringTemplate: 'string:template',
16
- dateReformat: 'date:reformat',
17
- datetimeCombine: 'datetime:combine',
18
- datetimeDate: 'datetime:date',
19
- datetimeTime: 'datetime:time',
20
- } as const;
21
-
22
- export const ARRAY_REDUCE_TRANSFORM_IDS = [
23
- BUILTIN_TRANSFORM_IDS.arrayFirst,
24
- BUILTIN_TRANSFORM_IDS.arrayJoin,
25
- ] as const;
26
-
27
- export const STRING_FORMAT_TRANSFORM_IDS = [
28
- BUILTIN_TRANSFORM_IDS.stringTrim,
29
- BUILTIN_TRANSFORM_IDS.stringUpper,
30
- BUILTIN_TRANSFORM_IDS.stringLower,
31
- BUILTIN_TRANSFORM_IDS.stringPrefix,
32
- BUILTIN_TRANSFORM_IDS.stringSuffix,
33
- ] as const;
34
-
35
- function asString(value: unknown): string {
36
- if (value === null || value === undefined) {
37
- return '';
38
- }
39
- return typeof value === 'string' ? value : String(value);
40
- }
41
-
42
- export const builtinValueTransforms: readonly ValueTransformDefinition[] = [
43
- {
44
- id: BUILTIN_TRANSFORM_IDS.identity,
45
- label: 'Pass-through',
46
- description: 'Return the source value unchanged.',
47
- category: 'utility',
48
- inputTypes: [
49
- 'string',
50
- 'number',
51
- 'boolean',
52
- 'date',
53
- 'time',
54
- 'datetime',
55
- 'object',
56
- 'array',
57
- 'unknown',
58
- ],
59
- outputType: 'unknown',
60
- apply: (value) => value,
61
- },
62
- {
63
- id: BUILTIN_TRANSFORM_IDS.arrayFirst,
64
- label: 'Array first',
65
- description: 'Return the first element of an array (or the value when not an array).',
66
- category: 'array',
67
- inputTypes: ['array', 'unknown'],
68
- outputType: 'unknown',
69
- apply: (value) => (Array.isArray(value) ? value[0] : value),
70
- },
71
- {
72
- id: BUILTIN_TRANSFORM_IDS.arrayJoin,
73
- label: 'Array join',
74
- description: 'Join array elements with a separator (default ", ").',
75
- category: 'array',
76
- inputTypes: ['array', 'unknown'],
77
- outputType: 'string',
78
- optionFields: [
79
- {
80
- key: 'separator',
81
- label: 'Separator',
82
- kind: 'string',
83
- },
84
- ],
85
- apply: (value, context) => {
86
- if (!Array.isArray(value)) {
87
- return asString(value);
88
- }
89
- const separator =
90
- typeof context.options?.separator === 'string' ? context.options.separator : ', ';
91
- return value.map((item) => asString(item)).join(separator);
92
- },
93
- },
94
- {
95
- id: BUILTIN_TRANSFORM_IDS.stringTrim,
96
- label: 'Trim',
97
- description: 'Trim leading and trailing whitespace.',
98
- category: 'string',
99
- inputTypes: ['string', 'number', 'unknown'],
100
- outputType: 'string',
101
- apply: (value) => asString(value).trim(),
102
- },
103
- {
104
- id: BUILTIN_TRANSFORM_IDS.stringUpper,
105
- label: 'Uppercase',
106
- description: 'Convert text to uppercase.',
107
- category: 'string',
108
- inputTypes: ['string', 'number', 'unknown'],
109
- outputType: 'string',
110
- apply: (value) => asString(value).toUpperCase(),
111
- },
112
- {
113
- id: BUILTIN_TRANSFORM_IDS.stringLower,
114
- label: 'Lowercase',
115
- description: 'Convert text to lowercase.',
116
- category: 'string',
117
- inputTypes: ['string', 'number', 'unknown'],
118
- outputType: 'string',
119
- apply: (value) => asString(value).toLowerCase(),
120
- },
121
- {
122
- id: BUILTIN_TRANSFORM_IDS.stringPrefix,
123
- label: 'Prefix',
124
- description: 'Prepend a fixed string.',
125
- category: 'string',
126
- inputTypes: ['string', 'number', 'unknown'],
127
- outputType: 'string',
128
- optionFields: [{ key: 'value', label: 'Prefix', kind: 'string' }],
129
- apply: (value, context) => {
130
- const prefix = typeof context.options?.value === 'string' ? context.options.value : '';
131
- return `${prefix}${asString(value)}`;
132
- },
133
- },
134
- {
135
- id: BUILTIN_TRANSFORM_IDS.stringSuffix,
136
- label: 'Suffix',
137
- description: 'Append a fixed string.',
138
- category: 'string',
139
- inputTypes: ['string', 'number', 'unknown'],
140
- outputType: 'string',
141
- optionFields: [{ key: 'value', label: 'Suffix', kind: 'string' }],
142
- apply: (value, context) => {
143
- const suffix = typeof context.options?.value === 'string' ? context.options.value : '';
144
- return `${asString(value)}${suffix}`;
145
- },
146
- },
147
- {
148
- id: BUILTIN_TRANSFORM_IDS.stringTemplate,
149
- label: 'Template',
150
- description: 'Fill {path} placeholders from an object (e.g. "{first} {last}").',
151
- category: 'string',
152
- inputTypes: ['object', 'unknown'],
153
- outputType: 'string',
154
- optionFields: [{ key: 'template', label: 'Template', kind: 'string' }],
155
- apply: (value, context) => {
156
- const template =
157
- typeof context.options?.template === 'string' ? context.options.template : '';
158
- if (!template) {
159
- return isPlainObject(value) ? JSON.stringify(value) : asString(value);
160
- }
161
- return applyStringTemplate(template, isPlainObject(value) ? value : undefined);
162
- },
163
- },
164
- {
165
- id: BUILTIN_TRANSFORM_IDS.dateReformat,
166
- label: 'Date reformat',
167
- description: 'Reformat a date string (token formats: YYYY, MM, DD).',
168
- category: 'date',
169
- inputTypes: ['string', 'unknown'],
170
- outputType: 'string',
171
- optionFields: [
172
- { key: 'inputFormat', label: 'Input format', kind: 'string' },
173
- { key: 'outputFormat', label: 'Output format', kind: 'string' },
174
- ],
175
- apply: (value, context) => {
176
- const inputFormat =
177
- typeof context.options?.inputFormat === 'string' ? context.options.inputFormat : 'YYYYMMDD';
178
- const outputFormat =
179
- typeof context.options?.outputFormat === 'string'
180
- ? context.options.outputFormat
181
- : 'YYYY-MM-DD';
182
- const text = asString(value);
183
- return reformatDateString(text, inputFormat, outputFormat) ?? text;
184
- },
185
- },
186
- {
187
- id: BUILTIN_TRANSFORM_IDS.datetimeCombine,
188
- label: 'Combine date+time',
189
- description: 'Join object fields into a datetime string (default keys: date, time).',
190
- category: 'date',
191
- inputTypes: ['object', 'unknown'],
192
- outputType: 'datetime',
193
- optionFields: [
194
- { key: 'dateKey', label: 'Date key', kind: 'string' },
195
- { key: 'timeKey', label: 'Time key', kind: 'string' },
196
- { key: 'separator', label: 'Separator', kind: 'string' },
197
- ],
198
- apply: (value, context) => {
199
- if (!isPlainObject(value)) {
200
- return asString(value);
201
- }
202
- const dateKey =
203
- typeof context.options?.dateKey === 'string' ? context.options.dateKey : 'date';
204
- const timeKey =
205
- typeof context.options?.timeKey === 'string' ? context.options.timeKey : 'time';
206
- const separator =
207
- typeof context.options?.separator === 'string' ? context.options.separator : 'T';
208
- return `${asString(value[dateKey])}${separator}${asString(value[timeKey])}`;
209
- },
210
- },
211
- {
212
- id: BUILTIN_TRANSFORM_IDS.datetimeDate,
213
- label: 'Date part',
214
- description: 'Take the date part from a datetime string.',
215
- category: 'date',
216
- inputTypes: ['string', 'datetime', 'unknown'],
217
- outputType: 'date',
218
- apply: (value) => splitDateTimeString(asString(value))?.date ?? asString(value),
219
- },
220
- {
221
- id: BUILTIN_TRANSFORM_IDS.datetimeTime,
222
- label: 'Time part',
223
- description: 'Take the time part from a datetime string.',
224
- category: 'date',
225
- inputTypes: ['string', 'datetime', 'unknown'],
226
- outputType: 'time',
227
- apply: (value) => splitDateTimeString(asString(value))?.time ?? '',
228
- },
229
- ];
230
-
231
- export type CreateBuiltinValueTransformRegistryOptions = Record<string, never>;
232
-
233
- /** Builtin registry. Hosts may `register()` additional transforms. */
234
- export function createBuiltinValueTransformRegistry(
235
- _options?: CreateBuiltinValueTransformRegistryOptions,
236
- ) {
237
- return createValueTransformRegistry(builtinValueTransforms);
238
- }
239
-
240
- /** @deprecated Empty — kept for import stability while hosts migrate. */
241
- export const TIME_FORMAT_TRANSFORM_IDS = [] as const;
242
- /** @deprecated Empty — kept for import stability while hosts migrate. */
243
- export const DATE_STYLE_TRANSFORM_IDS = [] as const;
1
+ import { reformatDateString, splitDateTimeString } from '../domain/mapping/dateFormat.js';
2
+ import { applyStringTemplate, isPlainObject } from '../domain/mapping/pathUtils.js';
3
+ import type { ValueTransformDefinition } from '../domain/types.js';
4
+ import { createValueTransformRegistry } from './createValueTransformRegistry.js';
5
+
6
+ export const BUILTIN_TRANSFORM_IDS = {
7
+ identity: 'identity',
8
+ arrayFirst: 'array:first',
9
+ arrayJoin: 'array:join',
10
+ stringTrim: 'string:trim',
11
+ stringUpper: 'string:upper',
12
+ stringLower: 'string:lower',
13
+ stringPrefix: 'string:prefix',
14
+ stringSuffix: 'string:suffix',
15
+ stringTemplate: 'string:template',
16
+ dateReformat: 'date:reformat',
17
+ datetimeCombine: 'datetime:combine',
18
+ datetimeDate: 'datetime:date',
19
+ datetimeTime: 'datetime:time',
20
+ } as const;
21
+
22
+ export const ARRAY_REDUCE_TRANSFORM_IDS = [
23
+ BUILTIN_TRANSFORM_IDS.arrayFirst,
24
+ BUILTIN_TRANSFORM_IDS.arrayJoin,
25
+ ] as const;
26
+
27
+ export const STRING_FORMAT_TRANSFORM_IDS = [
28
+ BUILTIN_TRANSFORM_IDS.stringTrim,
29
+ BUILTIN_TRANSFORM_IDS.stringUpper,
30
+ BUILTIN_TRANSFORM_IDS.stringLower,
31
+ BUILTIN_TRANSFORM_IDS.stringPrefix,
32
+ BUILTIN_TRANSFORM_IDS.stringSuffix,
33
+ ] as const;
34
+
35
+ function asString(value: unknown): string {
36
+ if (value === null || value === undefined) {
37
+ return '';
38
+ }
39
+ return typeof value === 'string' ? value : String(value);
40
+ }
41
+
42
+ export const builtinValueTransforms: readonly ValueTransformDefinition[] = [
43
+ {
44
+ id: BUILTIN_TRANSFORM_IDS.identity,
45
+ label: 'Pass-through',
46
+ description: 'Return the source value unchanged.',
47
+ category: 'utility',
48
+ inputTypes: [
49
+ 'string',
50
+ 'number',
51
+ 'boolean',
52
+ 'date',
53
+ 'time',
54
+ 'datetime',
55
+ 'object',
56
+ 'array',
57
+ 'unknown',
58
+ ],
59
+ outputType: 'unknown',
60
+ apply: (value) => value,
61
+ },
62
+ {
63
+ id: BUILTIN_TRANSFORM_IDS.arrayFirst,
64
+ label: 'Array first',
65
+ description: 'Return the first element of an array (or the value when not an array).',
66
+ category: 'array',
67
+ inputTypes: ['array', 'unknown'],
68
+ outputType: 'unknown',
69
+ apply: (value) => (Array.isArray(value) ? value[0] : value),
70
+ },
71
+ {
72
+ id: BUILTIN_TRANSFORM_IDS.arrayJoin,
73
+ label: 'Array join',
74
+ description: 'Join array elements with a separator (default ", ").',
75
+ category: 'array',
76
+ inputTypes: ['array', 'unknown'],
77
+ outputType: 'string',
78
+ optionFields: [
79
+ {
80
+ key: 'separator',
81
+ label: 'Separator',
82
+ kind: 'string',
83
+ },
84
+ ],
85
+ apply: (value, context) => {
86
+ if (!Array.isArray(value)) {
87
+ return asString(value);
88
+ }
89
+ const separator =
90
+ typeof context.options?.separator === 'string' ? context.options.separator : ', ';
91
+ return value.map((item) => asString(item)).join(separator);
92
+ },
93
+ },
94
+ {
95
+ id: BUILTIN_TRANSFORM_IDS.stringTrim,
96
+ label: 'Trim',
97
+ description: 'Trim leading and trailing whitespace.',
98
+ category: 'string',
99
+ inputTypes: ['string', 'number', 'unknown'],
100
+ outputType: 'string',
101
+ apply: (value) => asString(value).trim(),
102
+ },
103
+ {
104
+ id: BUILTIN_TRANSFORM_IDS.stringUpper,
105
+ label: 'Uppercase',
106
+ description: 'Convert text to uppercase.',
107
+ category: 'string',
108
+ inputTypes: ['string', 'number', 'unknown'],
109
+ outputType: 'string',
110
+ apply: (value) => asString(value).toUpperCase(),
111
+ },
112
+ {
113
+ id: BUILTIN_TRANSFORM_IDS.stringLower,
114
+ label: 'Lowercase',
115
+ description: 'Convert text to lowercase.',
116
+ category: 'string',
117
+ inputTypes: ['string', 'number', 'unknown'],
118
+ outputType: 'string',
119
+ apply: (value) => asString(value).toLowerCase(),
120
+ },
121
+ {
122
+ id: BUILTIN_TRANSFORM_IDS.stringPrefix,
123
+ label: 'Prefix',
124
+ description: 'Prepend a fixed string.',
125
+ category: 'string',
126
+ inputTypes: ['string', 'number', 'unknown'],
127
+ outputType: 'string',
128
+ optionFields: [{ key: 'value', label: 'Prefix', kind: 'string' }],
129
+ apply: (value, context) => {
130
+ const prefix = typeof context.options?.value === 'string' ? context.options.value : '';
131
+ return `${prefix}${asString(value)}`;
132
+ },
133
+ },
134
+ {
135
+ id: BUILTIN_TRANSFORM_IDS.stringSuffix,
136
+ label: 'Suffix',
137
+ description: 'Append a fixed string.',
138
+ category: 'string',
139
+ inputTypes: ['string', 'number', 'unknown'],
140
+ outputType: 'string',
141
+ optionFields: [{ key: 'value', label: 'Suffix', kind: 'string' }],
142
+ apply: (value, context) => {
143
+ const suffix = typeof context.options?.value === 'string' ? context.options.value : '';
144
+ return `${asString(value)}${suffix}`;
145
+ },
146
+ },
147
+ {
148
+ id: BUILTIN_TRANSFORM_IDS.stringTemplate,
149
+ label: 'Template',
150
+ description: 'Fill {path} placeholders from an object (e.g. "{first} {last}").',
151
+ category: 'string',
152
+ inputTypes: ['object', 'unknown'],
153
+ outputType: 'string',
154
+ optionFields: [{ key: 'template', label: 'Template', kind: 'string' }],
155
+ apply: (value, context) => {
156
+ const template =
157
+ typeof context.options?.template === 'string' ? context.options.template : '';
158
+ if (!template) {
159
+ return isPlainObject(value) ? JSON.stringify(value) : asString(value);
160
+ }
161
+ return applyStringTemplate(template, isPlainObject(value) ? value : undefined);
162
+ },
163
+ },
164
+ {
165
+ id: BUILTIN_TRANSFORM_IDS.dateReformat,
166
+ label: 'Date reformat',
167
+ description: 'Reformat a date string (token formats: YYYY, MM, DD).',
168
+ category: 'date',
169
+ inputTypes: ['string', 'unknown'],
170
+ outputType: 'string',
171
+ optionFields: [
172
+ { key: 'inputFormat', label: 'Input format', kind: 'string' },
173
+ { key: 'outputFormat', label: 'Output format', kind: 'string' },
174
+ ],
175
+ apply: (value, context) => {
176
+ const inputFormat =
177
+ typeof context.options?.inputFormat === 'string' ? context.options.inputFormat : 'YYYYMMDD';
178
+ const outputFormat =
179
+ typeof context.options?.outputFormat === 'string'
180
+ ? context.options.outputFormat
181
+ : 'YYYY-MM-DD';
182
+ const text = asString(value);
183
+ return reformatDateString(text, inputFormat, outputFormat) ?? text;
184
+ },
185
+ },
186
+ {
187
+ id: BUILTIN_TRANSFORM_IDS.datetimeCombine,
188
+ label: 'Combine date+time',
189
+ description: 'Join object fields into a datetime string (default keys: date, time).',
190
+ category: 'date',
191
+ inputTypes: ['object', 'unknown'],
192
+ outputType: 'datetime',
193
+ optionFields: [
194
+ { key: 'dateKey', label: 'Date key', kind: 'string' },
195
+ { key: 'timeKey', label: 'Time key', kind: 'string' },
196
+ { key: 'separator', label: 'Separator', kind: 'string' },
197
+ ],
198
+ apply: (value, context) => {
199
+ if (!isPlainObject(value)) {
200
+ return asString(value);
201
+ }
202
+ const dateKey =
203
+ typeof context.options?.dateKey === 'string' ? context.options.dateKey : 'date';
204
+ const timeKey =
205
+ typeof context.options?.timeKey === 'string' ? context.options.timeKey : 'time';
206
+ const separator =
207
+ typeof context.options?.separator === 'string' ? context.options.separator : 'T';
208
+ return `${asString(value[dateKey])}${separator}${asString(value[timeKey])}`;
209
+ },
210
+ },
211
+ {
212
+ id: BUILTIN_TRANSFORM_IDS.datetimeDate,
213
+ label: 'Date part',
214
+ description: 'Take the date part from a datetime string.',
215
+ category: 'date',
216
+ inputTypes: ['string', 'datetime', 'unknown'],
217
+ outputType: 'date',
218
+ apply: (value) => splitDateTimeString(asString(value))?.date ?? asString(value),
219
+ },
220
+ {
221
+ id: BUILTIN_TRANSFORM_IDS.datetimeTime,
222
+ label: 'Time part',
223
+ description: 'Take the time part from a datetime string.',
224
+ category: 'date',
225
+ inputTypes: ['string', 'datetime', 'unknown'],
226
+ outputType: 'time',
227
+ apply: (value) => splitDateTimeString(asString(value))?.time ?? '',
228
+ },
229
+ ];
230
+
231
+ export type CreateBuiltinValueTransformRegistryOptions = Record<string, never>;
232
+
233
+ /** Builtin registry. Hosts may `register()` additional transforms. */
234
+ export function createBuiltinValueTransformRegistry(
235
+ _options?: CreateBuiltinValueTransformRegistryOptions,
236
+ ) {
237
+ return createValueTransformRegistry(builtinValueTransforms);
238
+ }
239
+
240
+ /** @deprecated Empty — kept for import stability while hosts migrate. */
241
+ export const TIME_FORMAT_TRANSFORM_IDS = [] as const;
242
+ /** @deprecated Empty — kept for import stability while hosts migrate. */
243
+ export const DATE_STYLE_TRANSFORM_IDS = [] as const;