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

JSON.stringify(segment.value) is not very efficient for generating value preview #125

Open
phplego opened this issue Nov 25, 2022 · 3 comments

Comments

@phplego
Copy link

phplego commented Nov 25, 2022

It causes performance issues with big json data

@hivivo
Copy link
Owner

hivivo commented Nov 29, 2022

Thanks! feel free to create a PR for a better approach.

@phplego
Copy link
Author

phplego commented Dec 3, 2022

I have created this pretty fast preview function. It breaks generation if limit is reached

function previewLimited(obj:any, limit = 100, stringsLimit = 10) {
  let result = '';

  if (typeof obj === 'string') {
    if(obj.length > stringsLimit)
      result += `"${obj.substring(0, stringsLimit)}…"`;
    else
      result += `"${obj}"`;
  } else if (typeof obj === 'boolean') {
    result += `${obj ? 'true' : 'false'}`;
  } else if (typeof obj === 'number') {
    result += `${obj}`;
  } else if (typeof obj === 'object'){
    const isArray = Array.isArray(obj)
    result += isArray ? `(${obj.length})[` : '{';
    for (const key in obj) {
      const value = obj[key];

      result += isArray ? '': `${key}: `;

      if(result.length >= limit)
        return result.substring(0, limit)

      result += previewLimited(value, limit - result.length);
      result += `, `;
    }
    if(result.endsWith(', '))
      result = result.slice(0, -2)
    result += isArray ? ']' : '}';
  }

  if(result.length >= limit)
    return result.substring(0, limit)

  return result;
}

@phplego
Copy link
Author

phplego commented Dec 3, 2022

Pull request: #126

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

2 participants