How to use the relative function from path
Find comprehensive JavaScript path.relative code examples handpicked from public code repositorys.
path.relative returns the relative path from one file or directory to another.
GitHub: porterhq/porter
46 47 48 49 50 51 52 53 54 55
}); describe('module.children', function() { it('should have css dependencies parsed', async function() { const mod = porter.packet.files['home.jsx']; assert.deepEqual(mod.children.map(child => path.relative(root, child.fpath)), [ 'node_modules/react-dom/index.js', 'node_modules/react/index.js', 'app/web/home_dep.js', 'app/web/utils/index.js',
7
40
9
+ 15 other calls in file
324 325 326 327 328 329 330 331 332
/* * For both files and ignores, functions are passed the absolute * file path while strings are compared against the relative * file path. */ const relativeFilePath = path.relative(basePath, filePath); // if files isn't an array, throw an error assertNonEmptyFilesArray(config);
0
0
1
+ 3 other calls in file
How does path.relative work?
path.relative() is a method in Node.js that returns a relative path from one absolute path to another absolute path. It takes two absolute paths as input and returns a string representing the relative path from the first path to the second path. The returned string is calculated by calculating the difference between the two paths, and then returning a string that represents the relative path between them.
296 297 298 299 300 301 302 303 304 305 306 307 308
const home = process.env.HOME if (!home) return fileName fileName = path.resolve(fileName) const relFileName = path.relative(home, fileName) if (relFileName[0] === '.') return fileName return path.join('~', relFileName) }
0
0
0
Ai Example
1 2 3 4 5 6 7
const path = require("path"); const absolutePath1 = "/Users/johndoe/Documents"; const absolutePath2 = "/Users/johndoe/Documents/Projects"; const relativePath = path.relative(absolutePath1, absolutePath2); console.log(relativePath);
In this example, path.relative is used to get the relative path from absolutePath1 to absolutePath2. The output will be Projects, since that is the relative path between the two directories.