PHP's realpath in JavaScript

Here’s what our current JavaScript equivalent to PHP's realpath looks like.

module.exports = functionrealpath (path) {
// discuss at: https://locutus.io/php/realpath/
// original by: mk.keck
// improved by: Kevin van Zonneveld (https://kvz.io)
// note 1: Returned path is an url like e.g. 'https://yourhost.tld/path/'
// example 1: realpath('some/dir/.././_supporters/pj_test_supportfile_1.htm')
// returns 1: 'some/_supporters/pj_test_supportfile_1.htm'
if (typeofwindow === 'undefined') {
const nodePath = require('path')
return nodePath.normalize(path)
}
let p = 0
let arr = [] // Save the root, if not given
const r = this.window.location.href // Avoid input failures
// Check if there's a port in path (like 'https://')
path = (path + '').replace('\\', '/')
if (path.indexOf('://') !== -1) {
p = 1
}
// Ok, there's not a port in path, so let's take the root
if (!p) {
path = r.substring(0, r.lastIndexOf('/') + 1) + path
}
// Explode the given path into it's parts
arr = path.split('/') // The path is an array now
path = [] // Foreach part make a check
for (const k in arr) { // This is'nt really interesting
if (arr[k] === '.') {
continue
}
// This reduces the realpath
if (arr[k] === '..') {
/* But only if there more than 3 parts in the path-array.
* The first three parts are for the uri */
if (path.length > 3) {
path.pop()
}
} else {
// This adds parts to the realpath
// But only if the part is not empty or the uri
// (the first three parts ar needed) was not
// saved
if ((path.length < 2) || (arr[k] !== '')) {
path.push(arr[k])
}
}
}
// Returns the absloute path as a string
return path.join('/')
}
[ View on GitHub | Edit on GitHub | Source on GitHub ]

How to use

You you can install via npm install locutus and require it via require('locutus/php/filesystem/realpath'). You could also require the filesystem module in full so that you could access filesystem.realpath instead.

If you intend to target the browser, you can then use a module bundler such as Parcel, webpack, Browserify, or rollup.js. This can be important because Locutus allows modern JavaScript in the source files, meaning it may not work in all browsers without a build/transpile step. Locutus does transpile all functions to ES5 before publishing to npm.

A community effort

Not unlike Wikipedia, Locutus is an ongoing community effort. Our philosophy follows The McDonald’s Theory. This means that we don't consider it to be a bad thing that many of our functions are first iterations, which may still have their fair share of issues. We hope that these flaws will inspire others to come up with better ideas.

This way of working also means that we don't offer any production guarantees, and recommend to use Locutus inspiration and learning purposes only.

Notes

Examples

Please note that these examples are distilled from test cases that automatically verify our functions still work correctly. This could explain some quirky ones.

#codeexpected result
1realpath('some/dir/.././_supporters/pj_test_supportfile_1.htm')'some/_supporters/pj_test_supportfile_1.htm'

« More PHP filesystem functions


Star