Adam Rackis Adam Rackis - 3 months ago 27
Javascript Question

Basic JavaScript string equality failing in Gulp

I have this gulp task

gulp.src([`../../**/*.js`].concat(dontCheck.map(p => '!../../' + p)))
.pipe(gulpPrint(path => {
let adjusted = path.replace('..\\..\\', '').replace(/\\/g, '/');

let target = 'account/account.js';
if (adjusted == target){
console.log('FOUND');
} else {
console.log('NOT FOUND', '|' + target + '|', '|' + adjusted + '|', String(target) == String(adjusted), typeof target, typeof adjusted);
}


And I'm getting this maddening output

enter image description here

Why aren't those strings matching? Does
adjusted
have a different unicode encoding? If so how do I reconcile it?

Answer

Unless you did something to highlight the console.log output for us, it appears that adjusted is wrapped in a color control sequence e.g. \e[31m.

This answer might help, which provides this regex to replace control sequences:

/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g