kleros/kleros-v2

View on GitHub
kleros-sdk/src/dataMappings/utils/createResultObject.ts

Summary

Maintainability
A
35 mins
Test Coverage
export const createResultObject = (sourceData, seek: string[], populate: string[]) => {
  const result = {};

  const getNestedValue = (obj: any, path: string) => {
    return path.split(".").reduce((acc, part) => {
      if (acc && part.includes("[")) {
        const [key, index] = part.replace(/\]/g, "").split("[");
        return acc[key]?.[index];
      }
      return acc ? acc[part] : undefined;
    }, obj);
  };

  seek.forEach((key, idx) => {
    const foundValue = getNestedValue(sourceData, key);
    if (foundValue !== undefined) {
      result[populate[idx]] = foundValue;
    }
  });

  return result;
};