@uniweb/semantic-parser 1.0.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/.claude/settings.local.json +9 -0
- package/.eslintrc.json +28 -0
- package/LICENSE +674 -0
- package/README.md +395 -0
- package/docs/api.md +352 -0
- package/docs/file-structure.md +50 -0
- package/docs/guide.md +206 -0
- package/docs/mapping-patterns.md +928 -0
- package/docs/text-component-reference.md +515 -0
- package/package.json +41 -0
- package/reference/README.md +195 -0
- package/reference/Text.js +188 -0
- package/src/index.js +35 -0
- package/src/mappers/accessor.js +312 -0
- package/src/mappers/extractors.js +397 -0
- package/src/mappers/helpers.js +234 -0
- package/src/mappers/index.js +28 -0
- package/src/mappers/types.js +495 -0
- package/src/processors/byType.js +129 -0
- package/src/processors/groups.js +330 -0
- package/src/processors/groups_backup.js +379 -0
- package/src/processors/groups_doc.md +179 -0
- package/src/processors/sequence.js +573 -0
- package/src/processors/sequence_backup.js +402 -0
- package/src/utils/role.js +53 -0
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helper utilities for content transformation
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { createExcerpt, stripMarkup } from './types.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Get the first item from an array or return default value
|
|
9
|
+
* @param {Array} arr - Array to get first item from
|
|
10
|
+
* @param {*} defaultValue - Value to return if array is empty or undefined
|
|
11
|
+
* @returns {*} First item or default value
|
|
12
|
+
*/
|
|
13
|
+
function first(arr, defaultValue = null) {
|
|
14
|
+
return arr?.[0] ?? defaultValue;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Get the last item from an array or return default value
|
|
19
|
+
* @param {Array} arr - Array to get last item from
|
|
20
|
+
* @param {*} defaultValue - Value to return if array is empty or undefined
|
|
21
|
+
* @returns {*} Last item or default value
|
|
22
|
+
*/
|
|
23
|
+
function last(arr, defaultValue = null) {
|
|
24
|
+
if (!arr || arr.length === 0) return defaultValue;
|
|
25
|
+
return arr[arr.length - 1];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Transform an array using a mapping function
|
|
30
|
+
* @param {Array} arr - Array to transform
|
|
31
|
+
* @param {Function} transform - Transformation function
|
|
32
|
+
* @returns {Array} Transformed array
|
|
33
|
+
*/
|
|
34
|
+
function transformArray(arr, transform) {
|
|
35
|
+
if (!Array.isArray(arr)) return [];
|
|
36
|
+
return arr.map(transform);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Filter array and optionally transform
|
|
41
|
+
* @param {Array} arr - Array to filter
|
|
42
|
+
* @param {Function} predicate - Filter function
|
|
43
|
+
* @param {Function} transform - Optional transformation function
|
|
44
|
+
* @returns {Array} Filtered (and transformed) array
|
|
45
|
+
*/
|
|
46
|
+
function filterArray(arr, predicate, transform = null) {
|
|
47
|
+
if (!Array.isArray(arr)) return [];
|
|
48
|
+
const filtered = arr.filter(predicate);
|
|
49
|
+
return transform ? filtered.map(transform) : filtered;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Join array of strings with separator
|
|
54
|
+
* @param {Array} arr - Array to join
|
|
55
|
+
* @param {string} separator - Separator string
|
|
56
|
+
* @returns {string} Joined string
|
|
57
|
+
*/
|
|
58
|
+
function joinText(arr, separator = ' ') {
|
|
59
|
+
if (!Array.isArray(arr)) return '';
|
|
60
|
+
return arr.filter(Boolean).join(separator);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Flatten nested array structure
|
|
65
|
+
* @param {Array} arr - Array to flatten
|
|
66
|
+
* @param {number} depth - Depth to flatten (default: 1)
|
|
67
|
+
* @returns {Array} Flattened array
|
|
68
|
+
*/
|
|
69
|
+
function flatten(arr, depth = 1) {
|
|
70
|
+
if (!Array.isArray(arr)) return [];
|
|
71
|
+
return arr.flat(depth);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Check if value exists (not null, undefined, or empty string)
|
|
76
|
+
* @param {*} value - Value to check
|
|
77
|
+
* @returns {boolean} True if value exists
|
|
78
|
+
*/
|
|
79
|
+
function exists(value) {
|
|
80
|
+
return value !== null && value !== undefined && value !== '';
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Get value with fallback
|
|
85
|
+
* @param {*} value - Primary value
|
|
86
|
+
* @param {*} fallback - Fallback value
|
|
87
|
+
* @returns {*} Value or fallback
|
|
88
|
+
*/
|
|
89
|
+
function withDefault(value, fallback) {
|
|
90
|
+
return exists(value) ? value : fallback;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Validate that required fields are present
|
|
95
|
+
* @param {Object} data - Data object to validate
|
|
96
|
+
* @param {Array<string>} required - Array of required field names
|
|
97
|
+
* @returns {Object} Validation result { valid: boolean, missing: Array<string> }
|
|
98
|
+
*/
|
|
99
|
+
function validateRequired(data, required) {
|
|
100
|
+
const missing = required.filter(field => !exists(data[field]));
|
|
101
|
+
return {
|
|
102
|
+
valid: missing.length === 0,
|
|
103
|
+
missing
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Pick specific properties from object
|
|
109
|
+
* @param {Object} obj - Source object
|
|
110
|
+
* @param {Array<string>} keys - Keys to pick
|
|
111
|
+
* @returns {Object} Object with only picked keys
|
|
112
|
+
*/
|
|
113
|
+
function pick(obj, keys) {
|
|
114
|
+
if (!obj) return {};
|
|
115
|
+
return keys.reduce((result, key) => {
|
|
116
|
+
if (key in obj) {
|
|
117
|
+
result[key] = obj[key];
|
|
118
|
+
}
|
|
119
|
+
return result;
|
|
120
|
+
}, {});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Omit specific properties from object
|
|
125
|
+
* @param {Object} obj - Source object
|
|
126
|
+
* @param {Array<string>} keys - Keys to omit
|
|
127
|
+
* @returns {Object} Object without omitted keys
|
|
128
|
+
*/
|
|
129
|
+
function omit(obj, keys) {
|
|
130
|
+
if (!obj) return {};
|
|
131
|
+
const result = { ...obj };
|
|
132
|
+
keys.forEach(key => delete result[key]);
|
|
133
|
+
return result;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Safely access nested property
|
|
138
|
+
* @param {Object} obj - Object to access
|
|
139
|
+
* @param {string} path - Dot-notation path (e.g., 'a.b.c')
|
|
140
|
+
* @param {*} defaultValue - Default value if path doesn't exist
|
|
141
|
+
* @returns {*} Value at path or default
|
|
142
|
+
*/
|
|
143
|
+
function get(obj, path, defaultValue = undefined) {
|
|
144
|
+
if (!obj || !path) return defaultValue;
|
|
145
|
+
|
|
146
|
+
const keys = path.split('.');
|
|
147
|
+
let result = obj;
|
|
148
|
+
|
|
149
|
+
for (const key of keys) {
|
|
150
|
+
if (result === null || result === undefined) {
|
|
151
|
+
return defaultValue;
|
|
152
|
+
}
|
|
153
|
+
result = result[key];
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return result !== undefined ? result : defaultValue;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Compact an array (remove null, undefined, empty string)
|
|
161
|
+
* @param {Array} arr - Array to compact
|
|
162
|
+
* @returns {Array} Compacted array
|
|
163
|
+
*/
|
|
164
|
+
function compact(arr) {
|
|
165
|
+
if (!Array.isArray(arr)) return [];
|
|
166
|
+
return arr.filter(exists);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Create a safe extractor that won't throw on missing data
|
|
171
|
+
* @param {Function} extractFn - Extraction function that might throw
|
|
172
|
+
* @param {*} defaultValue - Default value if extraction fails
|
|
173
|
+
* @returns {Function} Safe extraction function
|
|
174
|
+
*/
|
|
175
|
+
function safe(extractFn, defaultValue = null) {
|
|
176
|
+
return (...args) => {
|
|
177
|
+
try {
|
|
178
|
+
return extractFn(...args) ?? defaultValue;
|
|
179
|
+
} catch (error) {
|
|
180
|
+
return defaultValue;
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Join paragraphs into a single string
|
|
187
|
+
* @param {Array|string} paragraphs - Array of paragraphs or single paragraph
|
|
188
|
+
* @param {string} separator - Separator to use between paragraphs
|
|
189
|
+
* @returns {string} Joined text
|
|
190
|
+
*/
|
|
191
|
+
function joinParagraphs(paragraphs, separator = ' ') {
|
|
192
|
+
if (!Array.isArray(paragraphs)) return paragraphs || '';
|
|
193
|
+
return paragraphs.filter(Boolean).join(separator);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Create an excerpt from paragraphs
|
|
198
|
+
* @param {Array|string} paragraphs - Array of paragraphs or single paragraph
|
|
199
|
+
* @param {Object} options - Excerpt options (maxLength, boundary, ellipsis)
|
|
200
|
+
* @returns {string} Generated excerpt
|
|
201
|
+
*/
|
|
202
|
+
function excerptFromParagraphs(paragraphs, options = {}) {
|
|
203
|
+
return createExcerpt(paragraphs, options);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Count words in text or paragraphs
|
|
208
|
+
* @param {Array|string} text - Text or array of paragraphs
|
|
209
|
+
* @returns {number} Word count
|
|
210
|
+
*/
|
|
211
|
+
function countWords(text) {
|
|
212
|
+
const plain = Array.isArray(text) ? text.join(' ') : text;
|
|
213
|
+
return stripMarkup(plain).split(/\s+/).filter(Boolean).length;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export {
|
|
217
|
+
first,
|
|
218
|
+
last,
|
|
219
|
+
transformArray,
|
|
220
|
+
filterArray,
|
|
221
|
+
joinText,
|
|
222
|
+
flatten,
|
|
223
|
+
exists,
|
|
224
|
+
withDefault,
|
|
225
|
+
validateRequired,
|
|
226
|
+
pick,
|
|
227
|
+
omit,
|
|
228
|
+
get,
|
|
229
|
+
compact,
|
|
230
|
+
safe,
|
|
231
|
+
joinParagraphs,
|
|
232
|
+
excerptFromParagraphs,
|
|
233
|
+
countWords
|
|
234
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Content mapping utilities for transforming parsed content
|
|
3
|
+
* into component-specific formats
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import * as helpers from './helpers.js';
|
|
7
|
+
import * as accessor from './accessor.js';
|
|
8
|
+
import * as extractors from './extractors.js';
|
|
9
|
+
import * as types from './types.js';
|
|
10
|
+
|
|
11
|
+
export {
|
|
12
|
+
// Helper utilities
|
|
13
|
+
helpers,
|
|
14
|
+
|
|
15
|
+
// Path-based accessor
|
|
16
|
+
accessor,
|
|
17
|
+
|
|
18
|
+
// Common extractors
|
|
19
|
+
extractors,
|
|
20
|
+
|
|
21
|
+
// Type system
|
|
22
|
+
types
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
// Re-export all functions for direct access
|
|
26
|
+
export * from './helpers.js';
|
|
27
|
+
export * from './accessor.js';
|
|
28
|
+
export * from './extractors.js';
|