Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
- [Dependency management](#dependency-management)
- [More details about dependency management](#more-details-about-dependency-management)
- [Utils](#utils)
- [Image file names](#image-file-names)
- [Internationalization](#internationalization)
- [Translations](#translations)
- [Using translations](#using-translations)
Expand Down Expand Up @@ -370,6 +371,19 @@ Text(controller.textFromApi);

# Utils

## Image file names

`GetUtils.isImage` and the `String.isImageFileName` extension recognize JPG,
JPEG, PNG, GIF, BMP, and WebP file extensions, ignoring case:

```dart
GetUtils.isImage('photo.webp'); // true
'photo.WEBP'.isImageFileName; // true
```

These helpers check only the file extension at the end of the string, not the
file contents. URL query strings and fragments are not removed before checking.

## Internationalization

### Translations
Expand Down
2 changes: 1 addition & 1 deletion lib/get_utils/src/extensions/string_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extension GetStringUtils on String {
/// Discover if the String is a vector
bool get isVectorFileName => GetUtils.isVector(this);

/// Discover if the String is a ImageFileName
/// Checks for a JPG, JPEG, PNG, GIF, BMP, or WebP file extension, ignoring case.
bool get isImageFileName => GetUtils.isImage(this);

/// Discover if the String is a AudioFileName
Expand Down
7 changes: 5 additions & 2 deletions lib/get_utils/src/get_utils/get_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,18 @@ class GetUtils {
ext.endsWith(".3gp");
}

/// Checks if string is an image file.
/// Checks whether [filePath] ends with a supported image file extension.
///
/// Supports JPG, JPEG, PNG, GIF, BMP, and WebP, ignoring case.
static bool isImage(String filePath) {
final ext = filePath.toLowerCase();

return ext.endsWith(".jpg") ||
ext.endsWith(".jpeg") ||
ext.endsWith(".png") ||
ext.endsWith(".gif") ||
ext.endsWith(".bmp");
ext.endsWith(".bmp") ||
ext.endsWith(".webp");
}

/// Checks if string is an audio file.
Expand Down
26 changes: 26 additions & 0 deletions test/utils/extensions/string_extensions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,32 @@ void main() {
expect(svgPath.isImageFileName, false);
});

test('var.isImageFileName recognizes WebP file names and paths', () {
for (final path in [
'logo.webp',
'logo.WEBP',
'logo.WeBp',
'/images/logo.webp',
r'C:\images\logo.webp',
]) {
expect(path.isImageFileName, isTrue, reason: path);
}
});

test(
'var.isImageFileName rejects strings without a final WebP extension',
() {
for (final path in [
'webp',
'logo.webp.exe',
'logo.webp?size=100',
'logo.webp#preview',
]) {
expect(path.isImageFileName, isFalse, reason: path);
}
},
);

test('var.isAudioFileName', () {
const mp3Path = "logo.mp3";
const wavPath = "logo.wav";
Expand Down
59 changes: 59 additions & 0 deletions test/utils/get_utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,65 @@ class EmptyClass {}
void main() {
dynamic newId(dynamic e) => e;

group('GetUtils.isImage', () {
test('recognizes every capitalization of the WebP extension', () {
for (var mask = 0; mask < 16; mask++) {
const letters = 'webp';
final extension = List.generate(letters.length, (index) {
final letter = letters[index];
return mask & (1 << index) == 0 ? letter : letter.toUpperCase();
}).join();
final path = 'photo.$extension';
expect(GetUtils.isImage(path), isTrue, reason: path);
}
});

test('recognizes WebP at the end of file names and paths', () {
for (final path in [
'photo.webp',
'.webp',
'photo.preview.webp',
'my photo.webp',
'/images/photo.webp',
r'C:\images\photo.webp',
'images/عکس.webp',
'https://example.com/images/photo.webp',
]) {
expect(GetUtils.isImage(path), isTrue, reason: path);
}
});

test('preserves support for existing image extensions', () {
for (final extension in ['jpg', 'jpeg', 'png', 'gif', 'bmp']) {
for (final variant in [extension, extension.toUpperCase()]) {
final path = 'photo.$variant';
expect(GetUtils.isImage(path), isTrue, reason: path);
}
}
});

test('requires a supported extension at the end of the string', () {
for (final path in [
'',
'photo',
'webp',
'photowebp',
'photo.webp2',
'photo.webp.exe',
'/images.webp/photo',
'photo.webp ',
'photo.webp\n',
'photo.webp?size=100',
'photo.webp#preview',
'photo.svg',
'photo.avif',
'photo.txt',
]) {
expect(GetUtils.isImage(path), isFalse, reason: path);
}
});
});

test('null isNullOrBlank should be true for null', () {
expect(GetUtils.isNullOrBlank(null), true);
});
Expand Down