buli
2023-07-14 5b5c1e16deaa3a9d6d0ed1ffca390655ed103df7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
 * Formats a list of sObjects returned by an Apex method call
 * @param {SObject[]} sObjects
 * @return {Object[]} formattedObjects, ready to use by lightning-datatable
 */
export function formatApexSObjects(sObjects) {
    try {
        return sObjects.map(formatApexSObject);
    } catch (err) {
        return [];
    }
}
 
function formatApexSObject(sObject) {
    return flatten(sObject, '');
}
 
function flatten(source, prefix) {
    const target = {};
    Object.keys(source).forEach((key) => {
        const value = source[key];
        const field = `${prefix}${key}`;
        if (typeof value === 'object') {
            const nested = flatten(value, `${field}.`);
            Object.assign(target, nested);
        } else {
            target[field] = value;
        }
    });
    return target;
}