chalk / ansi-regex

Regular expression for matching ANSI escape codes
MIT License
182 stars 78 forks source link

Not matching string #12

Closed manatarms closed 7 years ago

manatarms commented 7 years ago

I thought this might be relevant here too. There is also another issue in https://github.com/chalk/strip-ansi/issues/9 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- commented 7 years ago

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

I will comment on your other ticket as well. You shouldn't be doing things like this.