ylv4tq
Last Updated: February 25, 2016
·
140
· richardgong1987
Psb  1

nodejs path.normalize() confuse

In nodejs office webside there have a demoe,like blow:

var path = require('path'); 
path.normalize('/foo/bar//baz/asdf/quux/..'); 
// => '/foo/bar/baz/asdf'

It' easy to confuse! why it's should be like that

let's me show your,consider:

var path = require('path');
var pa = path.normalize('/foo/bar/baz/asdf/quux/..');
console.log(pa);//=>/foo/bar/baz/asdf

so don't confuse the "//",treat it as only one '/':

var path = require('path');
var pa = path.normalize('/////foo////bar//////////baz//asdf//quux////..');

console.log(pa);//=>/foo/bar/baz/asdf

thus taking care of .., ., and //. For instance:

var path = require('path');
var pa = path.normalize('/a/b/c/../..');

console.log(pa);//=>/a
var path = require('path');
var pa = path.normalize('/a/b/c/..');

console.log(pa);//=>/a/b
Say Thanks
Respond