Install via yarn add locutus and import:
import { realpath } from 'locutus/php/filesystem/realpath'.
Or with CommonJS: const { realpath } = require('locutus/php/filesystem/realpath')
Use a bundler that supports tree-shaking so you only ship the functions you actually use.
Vite,
webpack,
Rollup, and
Parcel
all handle this. For server-side use this is less of a concern.
Examples
These examples are extracted from test cases that automatically verify our functions against their native counterparts.
Here's what our current TypeScript equivalent to PHP's realpath looks like.
import nodePath from'path'
exportfunctionrealpath(path: string): string { // 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') { return nodePath.normalize(path) }
let hasProtocol = false letparts: string[] = [] // Save the root, if not given const href = window.location.href// Avoid input failures
// Check if there's a port in path (like 'https://') let normalizedPath = String(path).replace('\\', '/') if (normalizedPath.includes('://')) { hasProtocol = true }
// Ok, there's not a port in path, so let's take the root if (!hasProtocol) { normalizedPath = href.substring(0, href.lastIndexOf('/') + 1) + normalizedPath }
// Explode the given path into it's parts parts = normalizedPath.split('/') // The path is an array now constresolvedParts: string[] = [] // Foreach part make a check for (const part of parts) { // This is'nt really interesting if (part === '.') { continue } // This reduces the realpath if (part === '..') { /* But only if there more than 3 parts in the path-array. * The first three parts are for the uri */ if (resolvedParts.length > 3) { resolvedParts.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 (resolvedParts.length < 2 || part !== '') { resolvedParts.push(part) } } }
// Returns the absloute path as a string return resolvedParts.join('/') }
import nodePath from'path'
exportfunctionrealpath(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') { return nodePath.normalize(path) }
let hasProtocol = false let parts = [] // Save the root, if not given const href = window.location.href// Avoid input failures
// Check if there's a port in path (like 'https://') let normalizedPath = String(path).replace('\\', '/') if (normalizedPath.includes('://')) { hasProtocol = true }
// Ok, there's not a port in path, so let's take the root if (!hasProtocol) { normalizedPath = href.substring(0, href.lastIndexOf('/') + 1) + normalizedPath }
// Explode the given path into it's parts parts = normalizedPath.split('/') // The path is an array now const resolvedParts = [] // Foreach part make a check for (const part of parts) { // This is'nt really interesting if (part === '.') { continue } // This reduces the realpath if (part === '..') { /* But only if there more than 3 parts in the path-array. * The first three parts are for the uri */ if (resolvedParts.length > 3) { resolvedParts.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 (resolvedParts.length < 2 || part !== '') { resolvedParts.push(part) } } }
// Returns the absloute path as a string return resolvedParts.join('/') }
Improve this function
Locutus is a community effort following
The McDonald's Theory:
we ship first iterations, hoping others will improve them.
If you see something that could be better, we'd love your contribution.