From c5a63a88061e97218a2272551958557e9b60004f Mon Sep 17 00:00:00 2001 From: Larry Aasen Date: Sat, 8 Jun 2024 10:33:29 -0400 Subject: [PATCH] Bumped the minimum Dart version to 3.1.0, removed the use of the meta package, and modernized the code. --- .gitignore | 1 + CHANGELOG.md | 4 +++ example/vin_decoder_example.dart | 4 +-- lib/nhtsa.dart | 1 - lib/src/nhtsa_model.dart | 58 +++++++++++++++++++------------- lib/src/vin_decoder_base.dart | 16 ++++----- lib/vin_decoder.dart | 3 +- pubspec.yaml | 14 ++++---- test/vin_generator_test.dart | 4 +-- 9 files changed, 58 insertions(+), 47 deletions(-) diff --git a/.gitignore b/.gitignore index 2662521..f1c0b12 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ build/ # Directory created by dartdoc doc/api/ +.DS_Store diff --git a/CHANGELOG.md b/CHANGELOG.md index 544bafd..b7fef2c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2.0.0 + +- Bumped the minimum Dart version to 3.1.0, removed the use of the meta package, and modernized the code. + ## 0.2.0 - Expose helpers for querying NHTSA DB and accessing extended vehicle information (requested by @ride4sun, issue #8) diff --git a/example/vin_decoder_example.dart b/example/vin_decoder_example.dart index 96fe069..d3d3775 100644 --- a/example/vin_decoder_example.dart +++ b/example/vin_decoder_example.dart @@ -27,10 +27,10 @@ void main() async { print("Type is ${type}"); var info = await NHTSA.decodeVin(vin.number); - print('Plant Country is ' + info.value('Plant Country')); + print('Plant Country is ${info?.value('Plant Country')}'); var values = await NHTSA.decodeVinValues(vin.number); - print('Manufacturer from NHTSA DB is ' + values['Manufacturer']); + print('Manufacturer from NHTSA DB is ${values?['Manufacturer']}'); var generated = VINGenerator().generate(); print('Randomly Generated VIN is ${generated}'); diff --git a/lib/nhtsa.dart b/lib/nhtsa.dart index 0bddffa..44fd877 100644 --- a/lib/nhtsa.dart +++ b/lib/nhtsa.dart @@ -1,5 +1,4 @@ /// Support for querying NHTSA database by VIN. -// @dart=2.1 library nhtsa; export 'src/nhtsa_model.dart'; diff --git a/lib/src/nhtsa_model.dart b/lib/src/nhtsa_model.dart index af28dfc..94a3638 100644 --- a/lib/src/nhtsa_model.dart +++ b/lib/src/nhtsa_model.dart @@ -1,6 +1,7 @@ +import 'dart:convert'; + import 'package:basic_utils/basic_utils.dart'; import 'package:http/http.dart' as http; -import 'dart:convert'; // NHTSA Results not relevant for a specific vehicle can be either null or N/A const String _RESULT_NOT_APPLICABLE = 'Not Applicable'; @@ -10,7 +11,7 @@ class NHTSA { static const String _uriBase = 'https://vpic.nhtsa.dot.gov/api/vehicles'; /// Obtain information about a given [vin] from the NHTSA DB. - static Future decodeVin(String vin) async { + static Future decodeVin(String vin) async { var path = _uriBase + '/DecodeVin/' + vin + '?format=json'; final response = await http.get(Uri.parse(path)); @@ -22,7 +23,7 @@ class NHTSA { } /// Obtain a map of key/value pairs containing known values for a given [vin] - static Future> decodeVinValues(String vin) async { + static Future?> decodeVinValues(String vin) async { var path = _uriBase + '/DecodeVinValues/' + vin + '?format=json'; final response = await http.get(Uri.parse(path)); @@ -56,15 +57,19 @@ class NHTSAResult { /// The ID number of a given [variable] int variableId; - NHTSAResult({this.value, this.valueId, this.variable, this.variableId}); + NHTSAResult( + {required this.value, + required this.valueId, + required this.variable, + required this.variableId}); /// Create a new [NHTSAResult] instance from a fixed JSON payload - NHTSAResult.fromJson(Map json) { - value = json['Value']; - valueId = json['ValueId']; - variable = json['Variable']; - variableId = json['VariableId']; - } + factory NHTSAResult.fromJson(Map json) => NHTSAResult( + value: json['Value'] ?? '', + valueId: json['ValueId'], + variable: json['Variable'], + variableId: json['VariableId'], + ); @override String toString() { @@ -80,13 +85,14 @@ class NHTSAVehicleInfo { List results = []; NHTSAVehicleInfo( - {this.count, this.message, this.searchCriteria, this.results}); + {required this.count, + required this.message, + required this.searchCriteria, + required this.results}); /// Create a new [NHTSAVehicleInfo] instance from a fixed JSON payload - NHTSAVehicleInfo.fromJson(Map json) { - count = json['Count']; - message = json['Message']; - searchCriteria = json['SearchCriteria']; + factory NHTSAVehicleInfo.fromJson(Map json) { + final results = []; if (json['Results'] != null) { json['Results'].forEach((v) { if (v['Value'] != null && @@ -96,6 +102,13 @@ class NHTSAVehicleInfo { } }); } + + return NHTSAVehicleInfo( + count: json['Count'], + message: json['Message'], + searchCriteria: json['SearchCriteria'], + results: results, + ); } static String _normalizeStringValue(String s) { @@ -104,17 +117,16 @@ class NHTSAVehicleInfo { } /// Lookup the value of a variable by its [variableId] in the NHTSA DB results - String valueFromId(int variableId) { - var result = results.singleWhere((e) => e.variableId == variableId, - orElse: () => null); - return result != null ? _normalizeStringValue(result.value) : null; + String? valueFromId(int variableId) { + final index = + results.indexWhere((element) => element.variableId == variableId); + return index == -1 ? null : _normalizeStringValue(results[index].value); } /// Lookup the value of a named [variable] in the NHTSA DB results - String value(String variable) { - var result = - results.singleWhere((e) => e.variable == variable, orElse: () => null); - return result != null ? _normalizeStringValue(result.value) : null; + String? value(String variable) { + final index = results.indexWhere((element) => element.variable == variable); + return index == -1 ? null : _normalizeStringValue(results[index].value); } @override diff --git a/lib/src/vin_decoder_base.dart b/lib/src/vin_decoder_base.dart index 21d84d1..2385c35 100644 --- a/lib/src/vin_decoder_base.dart +++ b/lib/src/vin_decoder_base.dart @@ -2,8 +2,6 @@ import 'manufacturers.dart'; import 'nhtsa_model.dart'; import 'year_map.dart'; -import 'package:meta/meta.dart'; - class VIN { /// The VIN that the class was instantiated with. final String number; @@ -21,14 +19,14 @@ class VIN { final bool extended; Map _vehicleInfo = {}; - VIN({@required this.number, this.extended = false}) + VIN({required this.number, this.extended = false}) : wmi = normalize(number).substring(0, 3), vds = normalize(number).substring(3, 9), vis = normalize(number).substring(9, 17); /// Carry out VIN validation. A valid [number] must be 17 characters long /// and contain only valid alphanumeric characters. - bool valid([String number]) { + bool valid([String? number]) { String value = normalize(number != null ? number : this.number); return RegExp(r"^[a-zA-Z0-9]+$").hasMatch(value) && value.length == 17; } @@ -39,7 +37,7 @@ class VIN { /// Obtain the encoded manufacturing year in YYYY format. int getYear() { - return yearMap[modelYear()]; + return yearMap[modelYear()] ?? 0; } /// Obtain the 2-character region code for the manufacturing region. @@ -69,13 +67,13 @@ class VIN { String getManufacturer() { // Check for the standard case - a 3 character WMI if (manufacturers.containsKey(this.wmi)) { - return manufacturers[this.wmi]; + return manufacturers[this.wmi] ?? ''; } else { // Some manufacturers only use the first 2 characters for manufacturer // identification, and the third for the class of vehicle. var id = this.wmi.substring(0, 2); if (manufacturers.containsKey(id)) { - return manufacturers[id]; + return manufacturers[id] ?? ''; } else { return "Unknown (WMI: ${this.wmi.toUpperCase()})"; } @@ -85,7 +83,7 @@ class VIN { /// Returns the checksum for the VIN. Note that in the case of the EU region /// checksums are not implemented, so this becomes a no-op. More information /// is provided in ISO 3779:2009. - String getChecksum() { + String? getChecksum() { return (getRegion() != "EU") ? normalize(this.number)[8] : null; } @@ -100,7 +98,7 @@ class VIN { Future _fetchExtendedVehicleInfo() async { if (this._vehicleInfo.isEmpty && extended == true) { - this._vehicleInfo = await NHTSA.decodeVinValues(this.number); + this._vehicleInfo = await NHTSA.decodeVinValues(this.number) ?? {}; } } diff --git a/lib/vin_decoder.dart b/lib/vin_decoder.dart index a596681..fa5012f 100644 --- a/lib/vin_decoder.dart +++ b/lib/vin_decoder.dart @@ -1,5 +1,4 @@ -/// Support for VIN parsing, validation, and generation. -// @dart=2.1 +/// Support for VIN parsing, validation, and generation library vin_decoder; diff --git a/pubspec.yaml b/pubspec.yaml index 858c2d8..ada1569 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -2,19 +2,17 @@ name: vin_decoder description: Dart library for working with Vehicle Identification Numbers (VINs) based on ISO 3779:2009 and World Manufacturer Identifiers (WMIs) based on ISO 3780:2009, enriched by NHTSA data. - -version: 0.1.4+1 +version: 2.0.0 repository: https://github.com/adaptant-labs/vin-decoder-dart issue_tracker: https://github.com/adaptant-labs/vin-decoder-dart/issues environment: - sdk: '>=2.1.0 <3.0.0' + sdk: '>=3.1.0 <4.0.0' dependencies: - meta: ^1.4.0 - basic_utils: ^3.4.0 - http: ^0.13.3 - random_string: ^2.1.0 + basic_utils: ^5.7.0 + http: ^1.2.1 + random_string: ^2.3.1 dev_dependencies: - test: ^1.15.7 + test: ^1.25.7 diff --git a/test/vin_generator_test.dart b/test/vin_generator_test.dart index 9e4b067..f568c05 100644 --- a/test/vin_generator_test.dart +++ b/test/vin_generator_test.dart @@ -4,7 +4,7 @@ import 'package:test/test.dart'; void main() { group('VIN Generator Test', () { VINGenerator generator; - VIN vin; + VIN? vin; setUp(() { generator = VINGenerator(); @@ -12,7 +12,7 @@ void main() { }); test('Validity Test', () { - expect(vin.valid(), isTrue); + expect(vin!.valid(), isTrue); }); }); }