-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath.ts
66 lines (54 loc) · 1.73 KB
/
path.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import {compact, isArray, last} from 'lodash-es';
import path from 'path-browserify';
import {convertToArray} from '../array/index.js';
const kDefaultFolderSeparator = '/';
export function pathJoin(params: {
input: string | string[] | Array<string | string[]>;
separator?: string;
}) {
const {input, separator = kDefaultFolderSeparator} = params;
let pJoined = path.posix.join(
...convertToArray(input).map(arg =>
isArray(arg) ? arg.join(separator) : arg
)
);
if (pJoined.match(/^[./]*$/) || !pJoined) {
return '';
}
if (pJoined[0] !== separator) {
pJoined = separator + pJoined;
}
if (pJoined[pJoined.length - 1] === separator) {
pJoined = pJoined.slice(0, -1);
}
return pJoined;
}
export function pathSplit(params: {
input: string | string[];
separator?: string;
}) {
const {input = '', separator = kDefaultFolderSeparator} = params;
return compact(pathJoin({separator, input: input}).split(separator));
}
export function isPathEmpty(params: {
input: string | string[];
separator?: string;
}) {
const {input, separator = kDefaultFolderSeparator} = params;
const pJoined = pathJoin({separator, input: [input, 'E']});
return pJoined === '/E';
}
export function pathExtension(params: {input: string; separator?: string}) {
const name = last(pathSplit(params));
return path.extname(name || '').replace('.', '');
}
export function pathBasename(params: {input: string; separator?: string}) {
const {separator = kDefaultFolderSeparator} = params;
const name = last(pathSplit(params)) || '';
const ext = pathExtension({separator, input: name});
let basename = path.posix.basename(name, `.${ext}`);
if (basename.match(/^[.]*$/)) {
basename = '';
}
return {basename, ext};
}