Skip to content

Commit 25df40d

Browse files
committed
feat: value-type array.$keys array.$value and array.$entries will return an array from object
1 parent 67b9cb4 commit 25df40d

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

src/getValue.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -274,27 +274,25 @@ const getValue = (element) => {
274274
if (uppercase || uppercase === '')
275275
value = value.toUpperCase()
276276

277-
if (!Array.isArray(value)) {
278-
if (prefix || suffix)
277+
// Apply prefix and suffix first, before JSON parsing
278+
if (typeof value === 'string' || typeof value === 'number') {
279+
if (prefix || suffix) {
279280
value = prefix + value + suffix;
280-
281-
if (valueType == 'array')
282-
value = [value];
281+
}
283282
}
284283

285-
if (value && (valueType === 'object' || valueType === 'json')) {
284+
// Handle JSON parsing for objects, arrays, or when valueType starts with 'array'
285+
if (value && (valueType === 'object' || valueType === 'json' || valueType.startsWith('array'))) {
286286
try {
287287
value = JSON.parse(value);
288288
} catch (error) {
289-
// Fallback to regex extraction if JSON parsing fails
290289
const jsonRegex = /(\{[\s\S]*\}|\[[\s\S]*\])/;
291290
const match = value.match(jsonRegex);
292291

293292
if (match) {
294293
try {
295294
value = JSON.parse(match[0]);
296295
} catch (e) {
297-
// If parsing still fails, keep the original value or handle the error
298296
console.error('Failed to parse JSON after regex extraction:', e);
299297
}
300298
} else {
@@ -303,6 +301,28 @@ const getValue = (element) => {
303301
}
304302
}
305303

304+
// Now handle array-specific logic if valueType starts with 'array'
305+
if (valueType.startsWith('array')) {
306+
if (!Array.isArray(value)) {
307+
// If the parsed value is an object, apply array conversion based on operators
308+
if (typeof value === 'object') {
309+
if (valueType === 'array.$keys') {
310+
value = Object.keys(value);
311+
} else if (valueType === 'array.$values') {
312+
value = Object.values(value);
313+
} else if (valueType === 'array.$entries') {
314+
value = Object.entries(value);
315+
} else {
316+
// Default behavior: wrap the object in an array
317+
value = [value];
318+
}
319+
} else {
320+
// If it's not an object (i.e., a primitive), wrap the value in an array
321+
value = [value];
322+
}
323+
}
324+
}
325+
306326
return value;
307327

308328
};

0 commit comments

Comments
 (0)