smartpath/ts/smartpath.get.ts

49 lines
1.3 KiB
TypeScript
Raw Permalink Normal View History

2022-03-14 12:26:48 +00:00
import * as plugins from './smartpath.plugins.js'
2018-07-21 21:26:11 +00:00
export type TPathType = 'url' | 'local';
2016-03-20 17:59:30 +00:00
2016-04-04 14:25:17 +00:00
/**
* returns the type of the given path. Can be "url" or "local"
*/
2022-03-14 12:26:48 +00:00
export const type = (pathStringArg: string): TPathType => {
const urlRegex = /http[s|\s]:\/\/.*/i;
2018-07-21 21:26:11 +00:00
if (urlRegex.exec(pathStringArg)) {
return 'url';
} else {
return 'local';
}
};
2016-04-04 14:25:17 +00:00
2022-03-14 12:26:48 +00:00
/**
* gets the dirname from import.meta.url
*/
2022-03-14 12:38:32 +00:00
export const getDirnameFromImportMetaUrl = (importMetaUrlArg: string) => {
return plugins.path.dirname(plugins.url.fileURLToPath(importMetaUrlArg));
2022-03-14 12:26:48 +00:00
}
/**
* returns homedir as absolute path
* @param pathArgument if a pathargument is given, ~ is being replaced with the homedir
* @returns
*/
export const home = (pathArgument?: string) => {
2018-07-21 21:26:11 +00:00
if (pathArgument) {
return pathArgument.replace('~', plugins.os.homedir());
} else {
return plugins.os.homedir();
}
};
2018-07-21 21:26:11 +00:00
export type TSystemArg = 'dynamic' | 'windows' | 'linux' | 'osx';
2022-03-14 12:26:48 +00:00
export const pathLevels = (pathArg: string, systemArg: TSystemArg = 'dynamic') => {
2018-07-21 21:26:11 +00:00
let pathLevelArray: string[];
if (systemArg === 'dynamic') {
pathLevelArray = pathArg.split(plugins.path.sep);
}
return pathLevelArray;
};
2022-03-14 12:26:48 +00:00
export const pathLevelsBackwards = (pathArg: string, systemArg?: TSystemArg) => {
2018-07-21 21:26:11 +00:00
return pathLevels(pathArg, systemArg).reverse();
};