@unikue/ts-lang-utils
    Preparing search index...

    Function extractExtension

    • Extracts the file extension from an image source (URL or local file path) and returns it if allowed, otherwise returns the fallback

      Parameters

      • OptionalimageSource: string | null

        The image URL or local file path to extract the extension from

      • allowedExtensions: (string | null | undefined)[] = DEFAULT_EXTENSIONS

        The list of allowed extensions (with leading dot, e.g. ['.jpg', '.png']). Defaults to ['.jpg', '.jpeg', '.png', '.gif', '.webp', '.bmp', '.heic', '.heif']

      • fallback: string = '.jpg'

        The fallback extension with leading dot (defaults to '.jpg')

      Returns string

      the extracted extension if allowed, otherwise the fallback extension

      David Hsing

      extractExtension('https://example.com/image.png');    // '.png'
      extractExtension('http://example.com/image.webp?size=200'); // '.webp'
      extractExtension('/vol/images/photo.svg'); // '.jpg' (svg not in default allowed list)
      extractExtension('image.svg', ['.jpg', '.png', '.svg']); // '.svg'