smartpath/ts/smartpath.get.ts

54 lines
1.4 KiB
TypeScript
Raw Normal View History

2022-03-14 14:24:16 +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
/**
2024-04-20 08:13:48 +00:00
* returns the type of the given path.
* Can be "url" or "local"
2016-04-04 14:25:17 +00:00
*/
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 14:15:23 +00:00
export const dirnameFromImportMetaUrl = (importMetaUrlArg: string) => {
2022-03-14 12:38:32 +00:00
return plugins.path.dirname(plugins.url.fileURLToPath(importMetaUrlArg));
2022-03-14 14:24:16 +00:00
};
2022-03-14 12:26:48 +00:00
2024-04-20 08:13:48 +00:00
export const dirname = (pathArg: string) => {
2024-04-20 08:17:51 +00:00
return plugins.path.dirname(pathArg);
2024-04-20 08:13:48 +00:00
}
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
2022-03-14 14:24:16 +00:00
* @returns
2022-03-14 12:26:48 +00:00
*/
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();
};