Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A helper function if anyone is trying to convert a regular nested json to the data format specified by the library #428

Open
paulano1 opened this issue Dec 26, 2022 · 0 comments

Comments

@paulano1
Copy link

paulano1 commented Dec 26, 2022

function convertToRawNodeDatum(json: Record<string, string | number | boolean>): RawNodeDatum[] {
  if (json === null || typeof json !== 'object') {
    throw new Error('Input must be a non-null object');
  }

  const rawNodeData: RawNodeDatum[] = [];

  for (const key in json) {
    const value = json[key];
    const rawNodeDatum: RawNodeDatum = { name: key };

    if (Array.isArray(value)) {
      rawNodeDatum.children = value.map(convertToRawNodeDatum).flat();
    } else if (typeof value === 'object') {
      rawNodeDatum.children = convertToRawNodeDatum(value);
    } else {
      rawNodeDatum.attributes = { value: value };
    }

    rawNodeData.push(rawNodeDatum);
  }

  return rawNodeData;
}

'''
@paulano1 paulano1 changed the title A converter helper function if anyone is trying to convert a regular nested json to the data format specified by the library A helper function if anyone is trying to convert a regular nested json to the data format specified by the library Dec 26, 2022
@bkrem bkrem pinned this issue Jan 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant