2021-01-24 20:21:04 +03:00
|
|
|
import { always, cond } from 'ramda';
|
|
|
|
|
2021-01-24 19:37:31 +03:00
|
|
|
export interface QrCodeCapabilities {
|
|
|
|
useSizeInPath: boolean;
|
|
|
|
svgIsSupported: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export type QrCodeFormat = 'svg' | 'png';
|
|
|
|
|
|
|
|
export const buildQrCodeUrl = (
|
|
|
|
shortUrl: string,
|
|
|
|
size: number,
|
|
|
|
format: QrCodeFormat,
|
|
|
|
{ useSizeInPath, svgIsSupported }: QrCodeCapabilities,
|
|
|
|
): string => {
|
|
|
|
const sizeFragment = useSizeInPath ? `/${size}` : `?size=${size}`;
|
|
|
|
const formatFragment = !svgIsSupported ? '' : `format=${format}`;
|
2021-01-24 20:21:04 +03:00
|
|
|
const joinSymbolResolver = cond([
|
|
|
|
[ () => useSizeInPath && svgIsSupported, always('?') ],
|
|
|
|
[ () => !useSizeInPath && svgIsSupported, always('&') ],
|
|
|
|
]);
|
|
|
|
const joinSymbol = joinSymbolResolver() ?? '';
|
2021-01-24 19:37:31 +03:00
|
|
|
|
|
|
|
return `${shortUrl}/qr-code${sizeFragment}${joinSymbol}${formatFragment}`;
|
|
|
|
};
|