@reusable-ui/icon
Version:
An icon set component for React app.
42 lines (36 loc) • 1.48 kB
text/typescript
// utilities:
/**
* Merges two specified url to final url.
* @param base The relative or absolute base url.
* @param target The relative or absolute target url.
* @returns A final url.
* If `target` is an absolute url, the `base` discarded.
* Otherwise, the combination of `base` url followed by `target` url.
*/
export const concatUrl = (base: string, target: string): string => {
const dummyUrl = new URL('http://dummy');
if (!base.endsWith('/') || !base.endsWith('\\')) base += '/';
const baseUrl = new URL(base, dummyUrl);
const targetUrl = new URL(target, baseUrl);
const result = targetUrl.href;
if (result.startsWith(dummyUrl.origin)) return result.slice(dummyUrl.origin.length);
return result;
};
/**
* Gets a file format based on the extension of the specified `fileName`.
* @param fileName The name of the file to retrieve.
* @returns
* A `string` represents a file format.
* -or-
* `null` if the format file is unknown.
*/
export const formatOf = (fileName: string): string|null => {
if (!fileName) return null;
const lastDotIndex = fileName.lastIndexOf('.');
if (lastDotIndex < 0) return null; // extension is not found
const extension = fileName.slice(lastDotIndex + 1).toLowerCase();
switch (extension) {
case 'ttf': return 'format("truetype")';
default : return `format("${extension}")`;
} // case
};