Skip to content

Commit

Permalink
v3.2.2
Browse files Browse the repository at this point in the history
- `MimeType`:
- Added `isYAML`, `isFont`, `isPDF`, `isDart`, `isOctetStream`,
- Added `isZip`, `isGZip`, `isBZip2`, `isXZ`, `isCompressed`.
- Added `isTar`, `isTarGZip`, `isTarBZip2`, `isTarXZ`, `isTarCompressed`.
  • Loading branch information
gmpassos committed Sep 14, 2024
1 parent edf3ba3 commit 4eed720
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 6 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 3.2.2

- `MimeType`:
- Added `isYAML`, `isFont`, `isPDF`, `isDart`, `isOctetStream`,
- Added `isZip`, `isGZip`, `isBZip2`, `isXZ`, `isCompressed`.
- Added `isTar`, `isTarGZip`, `isTarBZip2`, `isTarXZ`, `isTarCompressed`.

## 3.2.1

- `MimeType`:
Expand Down
78 changes: 74 additions & 4 deletions lib/src/data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -379,15 +379,18 @@ class MimeType {
}
}

/// Returns [true] if this a image MIME-Type.
/// Returns [true] if this an image MIME-Type.
bool get isImage => type == 'image';

/// Returns [true] if this a video MIME-Type.
bool get isVideo => type == 'video';

/// Returns [true] if this a audio MIME-Type.
/// Returns [true] if this an audio MIME-Type.
bool get isAudio => type == 'audio';

/// Returns [true] if this a font MIME-Type.
bool get isFont => type == 'font';

/// Returns the HTML tag name for this MIME-Type.
String? get htmlTag {
if (isImage) {
Expand All @@ -411,13 +414,16 @@ class MimeType {
bool get isImageWebP => isImage && subType == 'webp';

/// Returns [true] if this is `image/svg+xml`.
bool get isImageSVG => isImage && subType == 'svg';
bool get isImageSVG => isImage && subType == 'svg+xml';

bool get isJavascript => subType == 'javascript';

/// Returns [true] if is `application/json`.
bool get isJSON => subType == 'json';

/// Returns [true] if is `application/yaml`.
bool get isYAML => subType == 'yaml';

/// Returns [true] if is `text/*`.
bool get isText => type == 'text';

Expand All @@ -431,14 +437,76 @@ class MimeType {
bool get isFormURLEncoded =>
type == 'application' && subType == 'x-www-form-urlencoded';

/// Returns [true] if is PDF.
bool get isPDF => subType == 'pdf';

/// Returns [true] if is a Dart script/code.
bool get isDart => subType == 'dart';

/// Returns [true] if is Zip.
bool get isZip => subType == 'zip';

/// Returns [true] if is GZip.
bool get isGZip => subType == 'gzip';

/// Returns [true] if is GZip.
bool get isBZip2 => subType == 'bzip2';

/// Returns [true] if is XZ.
bool get isXZ => subType == 'x-xz';

/// Returns [true] if is Tar.
bool get isTar => subType == 'x-tar';

/// Returns [true] if is `tar.gz`.
bool get isTarGZip => subType == 'x-tar+gzip';

/// Returns [true] if is `tar.bz2`.
bool get isTarBZip2 => subType == 'x-tar+bzip2';

/// Returns [true] if is `tar.xz`.
bool get isTarXZ => subType == 'x-tar+xz';

/// Returns [true] if is Tar+Compression.
bool get isTarCompressed => isTarGZip || isTarBZip2 || isTarXZ;

/// Returns [true] if is a compression type.
bool get isCompressed =>
isZip || isGZip || isBZip2 || isXZ || isTarCompressed;

/// Returns [true] if is `application/octet-stream`.
bool get isOctetStream => type == 'application' && subType == 'octet-stream';

/// Returns [true] if type is better represented as [String].
bool get isStringType {
return isText ||
isJSON ||
isJavascript ||
isYAML ||
isFormURLEncoded ||
isXML ||
isXHTML;
isXHTML ||
isDart ||
isImageSVG;
}

/// The preferred [String] [Encoding] for this MIME-Type:
Encoding? get preferredStringEncoding {
if (isCharsetUTF8) {
return utf8;
} else if (isCharsetLATIN1) {
return latin1;
} else if (isStringType) {
return utf8;
} else if (isImage) {
return isImageSVG ? utf8 : latin1;
} else if (isVideo || isAudio || isFont) {
return latin1;
} else if (isOctetStream || isCompressed || isTar || isPDF) {
return latin1;
} else {
return null;
}
}

/// Returns the common file extension for the MIME-Type.
Expand All @@ -448,6 +516,8 @@ class MimeType {
return 'js';
case 'gzip':
return 'gz';
case 'zip':
return 'zip';
case 'svg+xml':
return 'svg';
case 'xhtml+xml':
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: swiss_knife
description: Dart Useful Tools - collections, math, date, uri, json, events, resources, regexp, etc...
version: 3.2.1
version: 3.2.2
homepage: https://github.com/gmpassos/swiss_knife

environment:
Expand All @@ -15,7 +15,7 @@ dev_dependencies:
test: ^1.25.8
dependency_validator: ^3.2.3
collection: ^1.18.0
coverage: ^1.9.0
coverage: ^1.9.2

#dependency_overrides:
# resource_portable:
Expand Down
59 changes: 59 additions & 0 deletions test/swiss_knife_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,10 @@ void main() {
expect(MimeType.parse('png').toString(), equals(MimeType.imagePNG));
expect(MimeType.byExtension('png').toString(), equals(MimeType.imagePNG));

expect(MimeType.parse('image/svg').toString(), equals(MimeType.imageSVG));
expect(MimeType.parse('svg').toString(), equals(MimeType.imageSVG));
expect(MimeType.parse('svg')?.isImageSVG, isTrue);

expect(MimeType.parse('image/gif').toString(), equals(MimeType.imageGIF));
expect(MimeType.parse('gif').toString(), equals(MimeType.imageGIF));
expect(MimeType.byExtension('gif').toString(), equals(MimeType.imageGIF));
Expand Down Expand Up @@ -639,16 +643,23 @@ void main() {
MimeType.parse('yaml').toString(), equals(MimeType.applicationYaml));
expect(
MimeType.parse('yml').toString(), equals(MimeType.applicationYaml));
expect(MimeType.parse('yml')?.isStringType, isTrue);

expect(
MimeType.parse('markdown').toString(), equals(MimeType.textMarkdown));
expect(MimeType.parse('md').toString(), equals(MimeType.textMarkdown));
expect(MimeType.parse('md')?.isStringType, isTrue);

expect(
MimeType.parse('dart').toString(), equals(MimeType.applicationDart));

expect(MimeType.parse('application/octet-stream').toString(),
equals(MimeType.applicationOctetStream));
expect(MimeType.parse('application/octet-stream')?.isOctetStream, isTrue);

expect(MimeType.byExtension('js').toString(),
equals(MimeType.applicationJavaScript));
expect(MimeType.byExtension('js')?.isStringType, isTrue);

expect(MimeType.parse('zip').toString(), equals('application/zip'));
expect(MimeType.byExtension('zip').toString(), equals('application/zip'));
Expand All @@ -661,14 +672,17 @@ void main() {

expect(MimeType.parse('pdf').toString(), equals('application/pdf'));
expect(MimeType.byExtension('pdf').toString(), equals('application/pdf'));
expect(MimeType.byExtension('pdf')?.isPDF, isTrue);

expect(MimeType.parse('xml').toString(), equals('text/xml'));
expect(MimeType.byExtension('xml').toString(), equals('text/xml'));
expect(MimeType.byExtension('xml')?.isStringType, isTrue);

expect(MimeType.byExtension('yaml').toString(),
equals(MimeType.applicationYaml));
expect(MimeType.byExtension('yml').toString(),
equals(MimeType.applicationYaml));
expect(MimeType.byExtension('yml')?.isStringType, isTrue);

expect(
MimeType.byExtension('md').toString(), equals(MimeType.textMarkdown));
Expand Down Expand Up @@ -700,6 +714,51 @@ void main() {

expect(MimeType.byExtension('dart').toString(),
equals(MimeType.applicationDart));
expect(MimeType.byExtension('dart')?.isDart, isTrue);
expect(MimeType.byExtension('dart')?.isStringType, isTrue);

expect(MimeType.parse('application/x-tar')?.isTar, isTrue);
expect(MimeType.parse('application/x-tar+gzip')?.isTarGZip, isTrue);
expect(MimeType.parse('application/x-tar+gzip')?.isTarCompressed, isTrue);

expect(MimeType.parse('application/zip')?.isZip, isTrue);
expect(MimeType.parse('application/gzip')?.isGZip, isTrue);

expect(MimeType.parse('application/zip')?.isCompressed, isTrue);
expect(MimeType.parse('application/gzip')?.isCompressed, isTrue);
});

test('MimeType preferredStringEncoding', () {
expect(
MimeType.parse('text/html')?.preferredStringEncoding, equals(utf8));

expect(MimeType.parse('application/dart')?.preferredStringEncoding,
equals(utf8));

expect(MimeType.parse('application/json')?.preferredStringEncoding,
equals(utf8));

expect(
MimeType.parse('image/svg')?.preferredStringEncoding, equals(utf8));

expect(MimeType.parse('application/x-tar')?.preferredStringEncoding,
equals(latin1));

expect(MimeType.parse('application/pdf')?.preferredStringEncoding,
equals(latin1));

expect(MimeType.parse('application/zip')?.preferredStringEncoding,
equals(latin1));

expect(MimeType.parse('application/gzip')?.preferredStringEncoding,
equals(latin1));

expect(
MimeType.parse('application/octet-stream')?.preferredStringEncoding,
equals(latin1));

expect(
MimeType.parse('image/png')?.preferredStringEncoding, equals(latin1));
});

test('MimeType charset', () {
Expand Down

0 comments on commit 4eed720

Please sign in to comment.