Files
Unify/framework/unify/simplePath.js

305 lines
3.6 KiB
JavaScript
Raw Normal View History

2025-12-25 11:16:59 +01:00
/*
Copyright (c) 2020, 2023, The Unified Company.
This code is part of Unify.
This program is free software; you can redistribute it and/or modify
it under the terms of the ESA Software Community License - Strong Copyleft LICENSE,
as published by the ESA.
See the ESA Software Community License - Strong Copyleft LICENSE, for more details.
https://unifyjs.org
*/
export default class simplePath{
useSlash = true;
parts = new Array();
env = false;
beginWithSlash = true;
endWithSlash = true;
absolute = false;
constructor( part ) {
if( part ) {
this.preparePart( part );
this.parts.push( part );
}
}
preparePart( part ) {
if( this.parts.length == 0 ) {
if( part.slice( 0,2 ) == ".." ) {
this.beginWithSlash = false;
}
}
}
clone() {
var clone = new simplePath();
clone.useSlash = this.useSlash;
clone.parts = [...this.parts];
clone.env = this.env;
clone.beginWithSlash = this.beginWithSlash;
clone.endWithSlash = this.endWithSlash;
clone.absolute = this.absolute;
return clone;
}
getEnvironmentNative() {
if( typeof document != "undefined" ) {
return "Browser";
}
if( global ) {
return "Node"
}
}
getEnvironment() {
if( this.getEnvironmentNative() == "Browser" ) {
var env = "Browser";
} else {
var env = process.platform;
}
return env;
}
getSeparator() {
var env = this.getEnvironment();
if( env == "win32" || this.env == "win32" ) {
return '\\';
} else {
return "/";
}
}
slash( path ) {
const isExtendedLengthPath = path.startsWith('\\\\?\\');
if ( isExtendedLengthPath ) {
return path;
}
if( this.getSeparator() == "\\" ) {
return path.replace(/\//g, this.getSeparator());
} else {
return path.replace(/\\/g, this.getSeparator());
}
}
formatPath( part ) {
for (var i = 0; i < 4; i++) {
part = part.replace("//", "/")
}
for (var i = 0; i < 4; i++) {
part = part.replace("\\\\", "\\")
}
if( part.endsWith("/") || part.endsWith("\\") ) {
part = part.slice(0, -1)
}
if( part.startsWith("/") || part.startsWith("\\") ) {
part = part.substring(1);
}
return part;
}
add( part ) {
if( !part ) {
return false;
}
this.preparePart( part );
var formattedPath = this.formatPath( part );
this.parts.push( formattedPath );
}
removeFirstSlash( part ) {
if( part.startsWith("/") || part.startsWith("\\") ) {
part = part.substring(1);
}
return part;
}
assureFirstSlash( part ) {
if( !part.startsWith("/") || !part.startsWith("\\") ) {
part = "/" + part;
}
return part;
}
removeLastSlash( part ) {
if( part.endsWith("/") || part.endsWith("\\") ) {
part = part.slice(0, -1)
}
return part;
}
assureLastSlash( part ) {
if( !part.endsWith("/") || !part.endsWith("\\") ) {
part = part + "/";
}
return part;
}
lastItemHasExtension() {
var length = this.parts.length;
var lastItem = this.parts[ length - 1 ];
if( lastItem.includes(".") ) {
return true;
} else {
return false;
}
}
resolve() {
var parts = this.parts;
var joined = parts.join( this.getSeparator() );
if( this.useSlash ) {
joined = this.slash( joined );
}
if( !this.beginWithSlash ) {
joined = this.removeFirstSlash( joined );
} else {
joined = this.assureFirstSlash( joined );
}
if( !this.endWithSlash || this.lastItemHasExtension() ) {
joined = this.removeLastSlash( joined );
} else {
joined = this.assureLastSlash( joined );
}
if( this.absolute && global ) {
return global.path.resolve( joined );
}
return joined;
}
}