From 4eed720654151e271c1eaa26bca9b1b3401900b3 Mon Sep 17 00:00:00 2001 From: gmpassos Date: Sat, 14 Sep 2024 04:58:58 -0300 Subject: [PATCH] v3.2.2 - `MimeType`: - Added `isYAML`, `isFont`, `isPDF`, `isDart`, `isOctetStream`, - Added `isZip`, `isGZip`, `isBZip2`, `isXZ`, `isCompressed`. - Added `isTar`, `isTarGZip`, `isTarBZip2`, `isTarXZ`, `isTarCompressed`. --- CHANGELOG.md | 7 ++++ lib/src/data.dart | 78 ++++++++++++++++++++++++++++++++++++-- pubspec.yaml | 4 +- test/swiss_knife_test.dart | 59 ++++++++++++++++++++++++++++ 4 files changed, 142 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 25ad891..dc81811 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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`: diff --git a/lib/src/data.dart b/lib/src/data.dart index d9fc643..64dc933 100644 --- a/lib/src/data.dart +++ b/lib/src/data.dart @@ -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) { @@ -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'; @@ -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. @@ -448,6 +516,8 @@ class MimeType { return 'js'; case 'gzip': return 'gz'; + case 'zip': + return 'zip'; case 'svg+xml': return 'svg'; case 'xhtml+xml': diff --git a/pubspec.yaml b/pubspec.yaml index 68144ed..a04e3e4 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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: @@ -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: diff --git a/test/swiss_knife_test.dart b/test/swiss_knife_test.dart index 9378695..a16145b 100644 --- a/test/swiss_knife_test.dart +++ b/test/swiss_knife_test.dart @@ -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)); @@ -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')); @@ -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)); @@ -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', () {