DEV Community

Ramu Narasinga
Ramu Narasinga

Posted on • Edited on • Originally published at thinkthroo.com

flattenObjectKeys() function in Refine source code.

In this article, we will review a function, flattenObjectKeys, in Refine source code.

const isNested = (obj: any) => typeof obj === "object" && obj !== null;
const isArray = (obj: any) => Array.isArray(obj);

export const flattenObjectKeys = (obj: any, prefix = "") => {
  if (!isNested(obj)) {
    return {
      [prefix]: obj,
    };
  }

  return Object.keys(obj).reduce(
    (acc, key) => {
      const currentPrefix = prefix.length ? `${prefix}.` : "";

      if (isNested(obj[key]) && Object.keys(obj[key]).length) {
        if (isArray(obj[key]) && obj[key].length) {
          obj[key].forEach((item: unknown[], index: number) => {
            Object.assign(
              acc,
              flattenObjectKeys(item, `${currentPrefix + key}.${index}`),
            );
          });
        } else {
          Object.assign(acc, flattenObjectKeys(obj[key], currentPrefix + key));
        }
        // Even if it's a nested object, it should be treated as a key as well
        acc[currentPrefix + key] = obj[key];
      } else {
        acc[currentPrefix + key] = obj[key];
      }
      return acc;
    },
    {} as Record<string, unknown>,
  );
};
Enter fullscreen mode Exit fullscreen mode

This code snippet is picked from the file shown in the below image:

Image description

This is recursive function that calls it self to create an object with flattened keys. I was going to copy this over and provide a sample object but I found that such an example already exists in the test case.

it("should flatten an object with nested objects and arrays", () => {
    const obj = {
      a: 1,
      b: {
        c: 2,
        d: [3, 4],
      },
      e: {
        f: {
          g: 5,
        },
      },
    };

    const flattenedObj = flattenObjectKeys(obj);

    expect(flattenedObj).toEqual({
      a: 1,
      b: {
        c: 2,
        d: [3, 4],
      },
      "b.c": 2,
      "b.d": [3, 4],
      "b.d.0": 3,
      "b.d.1": 4,
      e: {
        f: {
          g: 5,
        },
      },
      "e.f": {
        g: 5,
      },
      "e.f.g": 5,
    });
  });
Enter fullscreen mode Exit fullscreen mode

You get the idea, right? the object below:

{
    a: 1,
    b: {
      c: 2,
      d: [3, 4],
    },
    e: {
      f: {
        g: 5,
      },
    },
};
Enter fullscreen mode Exit fullscreen mode

gets flattened to something like below:

{
  a: 1,
  b: {
    c: 2,
    d: [3, 4],
  },
  "b.c": 2,
  "b.d": [3, 4],
  "b.d.0": 3,
  "b.d.1": 4,
  e: {
    f: {
      g: 5,
    },
  },
  "e.f": {
    g: 5,
  },
  "e.f.g": 5,
}
Enter fullscreen mode Exit fullscreen mode

For more test cases, check out this file — flatten-object-keys/index.spec.ts

About me:

Hey, my name is Ramu Narasinga. I study large open-source projects and create content about their codebase architecture and best practices, sharing it through articles, videos.

I am open to work on interesting projects. Send me an email at ramu.narasinga@gmail.com

My Github —  https://github.com/ramu-narasinga

My website —  https://ramunarasinga.com

My Youtube channel —  https://www.youtube.com/@ramu-narasinga

Learning platform —  https://thinkthroo.com

Codebase Architecture —  https://app.thinkthroo.com/architecture

Best practices —  https://app.thinkthroo.com/best-practices

Production-grade projects —  https://app.thinkthroo.com/production-grade-projects

References:

  1. https://github.com/refinedev/refine/blob/main/packages/core/src/definitions/helpers/flatten-object-keys/index.ts

Tiugo image

Modular, Fast, and Built for Developers

CKEditor 5 gives you full control over your editing experience. A modular architecture means you get high performance, fewer re-renders and a setup that scales with your needs.

Start now

Top comments (0)

Retry later
👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere “thank you” often brightens someone’s day—share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay