Skip to content

Commit

Permalink
Merge pull request #22 from bcgov/feature/nested-inputs
Browse files Browse the repository at this point in the history
Feature/nested inputs
  • Loading branch information
brysonjbest authored Jul 18, 2024
2 parents a480438 + d441465 commit 590a4df
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 13 deletions.
54 changes: 42 additions & 12 deletions src/utils/csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,47 @@ export const extractKeys = (headers: string[], prefix: string): string[] => {
* @returns An array of formatted variables.
*/
export const formatVariables = (row: string[], keys: string[], startIndex: number, filterEmpty = false): Variable[] => {
return keys
.map((key, index) => {
const value = row[startIndex + index] ? formatValue(row[startIndex + index]) : null;
if (filterEmpty && (value === null || value === undefined || value === '')) {
return undefined;
const result: { [key: string]: any } = {};

keys.forEach((key, index) => {
const value = row[startIndex + index] ? formatValue(row[startIndex + index]) : null;
if (filterEmpty && (value === null || value === undefined || value === '')) {
return;
}

const parts = key.match(/^([^\[]+)(?:\[(\d+)\])?(.*)$/);
if (parts) {
const [, baseKey, arrayIndex, remainingKey] = parts;

const pluralizedKey = `${baseKey}${Number(arrayIndex) > 0 ? 's' : ''}`;

if (!result[pluralizedKey]) {
result[pluralizedKey] = arrayIndex ? [] : {};
}
return {
name: key,
value: value,
type: typeof value,
};
})
.filter((entry) => entry !== undefined);

if (arrayIndex) {
const idx = parseInt(arrayIndex, 10) - 1;
if (!result[pluralizedKey][idx]) {
result[pluralizedKey][idx] = {};
}
if (remainingKey) {
result[pluralizedKey][idx][remainingKey] = value;
} else {
result[pluralizedKey][idx] = value;
}
} else if (remainingKey) {
result[pluralizedKey][remainingKey] = value;
} else {
result[pluralizedKey] = value;
}
} else {
result[key] = value;
}
});

return Object.entries(result).map(([name, value]) => ({
name,
value: Array.isArray(value) ? value.filter((v) => v !== undefined) : value,
type: Array.isArray(value) ? 'array' : typeof value,
}));
};
22 changes: 21 additions & 1 deletion src/utils/handleTrace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,28 @@ export const getPropertyById = (id: string, ruleSchema: RuleSchema, type: 'input
export const mapTraceToResult = (trace: TraceObject, ruleSchema: RuleSchema, type: 'input' | 'output') => {
const result: { [key: string]: any } = {};
const schema = type === 'input' ? ruleSchema.inputs : ruleSchema.resultOutputs;

for (const [key, value] of Object.entries(trace)) {
if (trace[key] && typeof trace[key] === 'object') {
const newArray: any[] = [];
const arrayName = key;
for (const item in trace[key]) {
if (trace[key].hasOwnProperty(item)) {
newArray.push(trace[key][item]);
}
}
newArray.forEach((item, index) => {
index++;
const keyName = `${arrayName.toString().slice(0, -1)}[${index}]`;
if (Object.keys(item).length === 0) {
result[`${keyName}${key}`] = item;
} else {
Object.keys(item).forEach((key) => {
result[`${keyName}${key}`] = item[key];
});
}
});
}

const propertyUnformatted = getPropertyById(key, ruleSchema, type);
const property = propertyUnformatted ? replaceSpecialCharacters(propertyUnformatted, '') : null;
if (property) {
Expand Down
3 changes: 3 additions & 0 deletions src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ export const formatValue = (value: string): boolean | number | string | null =>
return true;
} else if (value.toLowerCase() === 'false') {
return false;
// Check if the value matches the format yyyy-mm-dd
} else if (/\d{4}-\d{2}-\d{2}/.test(value)) {
return value;
}
const numberValue = parseFloat(value);
if (!isNaN(numberValue)) {
Expand Down

0 comments on commit 590a4df

Please sign in to comment.