0

I am getting three arguments in my callback, filePath, stat and name. I need to form a object out of it before passing it to my async function.

var config = {};
walk('directory', function(filePath, stat, name) {
    console.log("filePath", filePath);
    config.input = filePath;
    config["output"] = path.join("json", name + ".json");   
});

Below is the output:

FilePath: word\file1.doc
{ input: 'word\\file1.doc', output: 'json\\file1.doc.json' }
FilePath: word\file2.doc
{ input: 'word\\file2.doc', output: 'json\\file2.doc.json' }

Why is there a \ getting appended in my object? Though my FilePath has only a single slash.

Shane
  • 5,517
  • 15
  • 49
  • 79
  • 4
    The values are being shown as they would be written in code, as string literals, with [appropriate escape sequences](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String). The two slashes together represent a single slash in the value. – Jonathan Lonowski Apr 15 '15 at 22:50
  • How are you getting `{ input: 'word\\file2.doc', output: 'json\\file2.doc.json' }` printed to the console? – Kevin B Apr 15 '15 at 22:50
  • @KevinB: When i print the config object, i get the output. i don't need double slash... all i need is a single slash. – Shane Apr 15 '15 at 22:54
  • 2
    @Shane There aren't actually two slashes in the value. That's just how a single slash is being represented when printed to the console. A minimal check would be to compare their `.length`s against the number of characters printed out. – Jonathan Lonowski Apr 15 '15 at 22:56
  • Open the node REPL and compare the output of `console.log('\\bar')` and `console.log({foo: '\\bar'})`. Same string, different output. – Felix Kling Apr 15 '15 at 22:57
  • @JonathanLonowski: Then how come printing FilePath to console prints only one slash and when i add it to object it prints two slashes? – Shane Apr 15 '15 at 22:58
  • Because apparently `console.log` treats strings and objects (or rather strings inside objects) differently. You might have notice for example that the object output includes the quotation marks, while the string output does not. Does that mean your string inside the object literally contains quotation marks? Of course not. There is no standard for `console.log`, so implementors can pretty much do whatever they want. – Felix Kling Apr 15 '15 at 22:59
  • 1
    @Shane `console.log()` via [`util.format()`](https://nodejs.org/api/util.html#util_util_format_format) treats the 1st argument, when it's a string, differently. Try `console.log('%j', value);` and the string will get the same format for it as when logging an object with that string. – Jonathan Lonowski Apr 15 '15 at 23:02
  • @JonathanLonowski Thanks, i understood.... i was bit confused.... got it.. thanks – Shane Apr 15 '15 at 23:05

0 Answers0