All files / src/lib file-url.ts

100% Statements 18/18
100% Branches 6/6
100% Functions 7/7
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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          4x     4x   4x   141x   4x     4x 34x     4x 9x 9x     5x   4x 9x     4x 38x  
// SPDX-License-Identifier: GPL-3.0-only
// Copyright (C) 2026 Max Boksem. See NOTICE for additional terms under GPLv3 section 7.
 
// A playlist imported from disk is stored as a file:// URL so it can share the
// provider's url field with remote playlists.
const FILE_SCHEME = "file://";
 
/** The extensions the file picker offers, without the dot. */
export const PLAYLIST_EXTENSIONS = ["m3u", "m3u8", "txt"];
 
const PLAYLIST_EXTENSION = new RegExp(`\\.(${PLAYLIST_EXTENSIONS.join("|")})$`, "i");
 
export const isFileUrl = (url: string): boolean => url.startsWith(FILE_SCHEME);
 
export const fileUrlFromPath = (path: string): string => `${FILE_SCHEME}${path}`;
 
/** Returns anything that isn't a file:// URL unchanged. */
export const pathFromFileUrl = (url: string): string =>
	isFileUrl(url) ? url.slice(FILE_SCHEME.length) : url;
 
/** Handles Windows separators too, since the file picker returns native paths. */
export const filenameFromPath = (path: string): string => {
	const parts = path.replace(/\\/g, "/").split("/");
	return parts[parts.length - 1] || path;
};
 
export const isPlaylistFile = (path: string): boolean => PLAYLIST_EXTENSION.test(path);
 
export const nameFromFilename = (filename: string): string =>
	filename.replace(PLAYLIST_EXTENSION, "");
 
/** A full local path is long and says little on a provider card, so files show their name. */
export const displayUrl = (url: string): string =>
	isFileUrl(url) ? filenameFromPath(pathFromFileUrl(url)) : url;