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

Not stripping object after stringify #9

Closed
manatarms opened this issue Mar 25, 2017 · 3 comments
Closed

Not stripping object after stringify #9

manatarms opened this issue Mar 25, 2017 · 3 comments
Labels

Comments

@manatarms
Copy link

manatarms commented Mar 25, 2017

I'm having a super weird issue. I have this object that is has computed keys. I have things that I want to highlight in this object so it has chalk colors on some keys. This works great.
My object

let stats = {
    [user.name]: {
      Name: chalk.cyan(user['name'])
     //other properties
    }
  };

I want to print these values to a file and would like to strip the ANSI. However, the strip function does not seem to be working.

Here is what I'm using to print to file.

let stripped = stripAnsi(JSON.stringify(stats,null,2));
fs.writeFile('./stats.json', stripped, function(err) {
  if (err) {
    return console.log(err);
  }
  console.log('Stats saved to file!');
});

The output looks like this

{
  "John": {
    "Name": "\u001b[36mJohn\u001b[39m"
  }
}

I did some debugging and the string checks in strip-ansi seem to be working. I thought you might have a better idea of what exactly is happening.

Thanks!

@Qix-
Copy link
Member

Qix- commented Mar 27, 2017

From your other ticket:

Because the .stringify function decodes binary characters into their ASCII literal escapes - such as "\u001b" instead of the Unicode character.

Basically, don't do this. This isn't how Chalk is intended to be used, as you should be stripping your inputs to JSON.stringify() - not the outputs.

General rule of thumb: If you're post-processing output based on the input, that's almost guaranteed code smell and you should probably re-think your application logic.

Just strip your inputs to JSON.stringify() - or better yet, don't use chalk on any of the strings at all ;)

@Qix- Qix- closed this as completed Mar 27, 2017
@Qix- Qix- added the wontfix label Mar 27, 2017
@manatarms
Copy link
Author

Thanks :) Makes sense

@Qix-
Copy link
Member

Qix- commented Mar 27, 2017

I should also clarify: the ansi-regex pattern doesn't match the literal characters \, u, 0, 0, 1, b, but instead the actual byte.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants