Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed the use of the meta package and modernized the code. #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ build/

# Directory created by dartdoc
doc/api/
.DS_Store
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
4 changes: 2 additions & 2 deletions example/vin_decoder_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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}');
Expand Down
1 change: 0 additions & 1 deletion lib/nhtsa.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/// Support for querying NHTSA database by VIN.
// @dart=2.1
library nhtsa;

export 'src/nhtsa_model.dart';
58 changes: 35 additions & 23 deletions lib/src/nhtsa_model.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<NHTSAVehicleInfo> decodeVin(String vin) async {
static Future<NHTSAVehicleInfo?> decodeVin(String vin) async {
var path = _uriBase + '/DecodeVin/' + vin + '?format=json';
final response = await http.get(Uri.parse(path));

Expand All @@ -22,7 +23,7 @@ class NHTSA {
}

/// Obtain a map of key/value pairs containing known values for a given [vin]
static Future<Map<String, dynamic>> decodeVinValues(String vin) async {
static Future<Map<String, dynamic>?> decodeVinValues(String vin) async {
var path = _uriBase + '/DecodeVinValues/' + vin + '?format=json';
final response = await http.get(Uri.parse(path));

Expand Down Expand Up @@ -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<String, dynamic> json) {
value = json['Value'];
valueId = json['ValueId'];
variable = json['Variable'];
variableId = json['VariableId'];
}
factory NHTSAResult.fromJson(Map<String, dynamic> json) => NHTSAResult(
value: json['Value'] ?? '',
valueId: json['ValueId'],
variable: json['Variable'],
variableId: json['VariableId'],
);

@override
String toString() {
Expand All @@ -80,13 +85,14 @@ class NHTSAVehicleInfo {
List<NHTSAResult> 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<String, dynamic> json) {
count = json['Count'];
message = json['Message'];
searchCriteria = json['SearchCriteria'];
factory NHTSAVehicleInfo.fromJson(Map<String, dynamic> json) {
final results = <NHTSAResult>[];
if (json['Results'] != null) {
json['Results'].forEach((v) {
if (v['Value'] != null &&
Expand All @@ -96,6 +102,13 @@ class NHTSAVehicleInfo {
}
});
}

return NHTSAVehicleInfo(
count: json['Count'],
message: json['Message'],
searchCriteria: json['SearchCriteria'],
results: results,
);
}

static String _normalizeStringValue(String s) {
Expand All @@ -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
Expand Down
16 changes: 7 additions & 9 deletions lib/src/vin_decoder_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,14 +19,14 @@ class VIN {
final bool extended;
Map<String, dynamic> _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;
}
Expand All @@ -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.
Expand Down Expand Up @@ -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()})";
}
Expand All @@ -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;
}

Expand All @@ -100,7 +98,7 @@ class VIN {

Future<void> _fetchExtendedVehicleInfo() async {
if (this._vehicleInfo.isEmpty && extended == true) {
this._vehicleInfo = await NHTSA.decodeVinValues(this.number);
this._vehicleInfo = await NHTSA.decodeVinValues(this.number) ?? {};
}
}

Expand Down
3 changes: 1 addition & 2 deletions lib/vin_decoder.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/// Support for VIN parsing, validation, and generation.
// @dart=2.1
/// Support for VIN parsing, validation, and generation

library vin_decoder;

Expand Down
14 changes: 6 additions & 8 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions test/vin_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import 'package:test/test.dart';
void main() {
group('VIN Generator Test', () {
VINGenerator generator;
VIN vin;
VIN? vin;

setUp(() {
generator = VINGenerator();
vin = VIN(number: generator.generate());
});

test('Validity Test', () {
expect(vin.valid(), isTrue);
expect(vin!.valid(), isTrue);
});
});
}