1import { ImageSource } from '../Image.types';
2
3/**
4 * Converts a string in blurhash format (`blurhash:/<hash>/<width>/<height>`
5 * or <hash>/<width>/<height>) into an `ImageSource`.
6 * Note: The blurhash:/ uri scheme is removed, because for backward compatibility reasons,
7 * strings without a scheme are assumed to be `blurhash` by default.
8 *
9 * @return An ImageSource representing the provided blurhash.
10 * */
11export function resolveBlurhashString(str: string): ImageSource {
12  const [blurhash, width, height] = str.replace(/^blurhash:\//, '').split('/');
13  return {
14    uri: blurhash,
15    width: parseInt(width, 10) || 16,
16    height: parseInt(height, 10) || 16,
17  };
18}
19
20/**
21 * Converts a string in thumbhash format (`thumbhash:/<hash>` or `<hash>`)
22 * into an `ImageSource`.
23 * Note: Unlike the `resolveBlurhashString` the `thumbhash:/` scheme has to be present,
24 * as the scheme has to be explicitly stated to be interpreted a `thumbhash` source.
25 *
26 * @return An ImageSource representing the provided thumbhash.
27 * */
28export function resolveThumbhashString(str: string): ImageSource {
29  const hash = str.replace(/^thumbhash:\//, '');
30  return {
31    uri: 'thumbhash:/' + hash,
32  };
33}
34