summaryrefslogtreecommitdiff
path: root/src/utils/strings.ts
blob: 4aaa91643e50cf05e257d7f402f298aa5556cc21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
export const ellipsize = (str: string, len: number) => (str.length > len ? `${str.slice(0, len - 1)}…` : str);

export const basename = (path: string, stripExt = true) => {
    const lastSlash = path.lastIndexOf("/");
    const lastDot = path.lastIndexOf(".");
    return path.slice(lastSlash + 1, stripExt && lastDot > lastSlash ? lastDot : undefined);
};

export const pathToFileName = (path: string, ext?: string) => {
    const start = /[a-z]+:\/\//.test(path) ? 0 : path.indexOf("/") + 1;
    const dir = path.slice(start, path.lastIndexOf("/")).replaceAll("/", "-");
    return `${dir}-${basename(path, ext !== undefined)}${ext ? `.${ext}` : ""}`;
};