mapCourierStatus function
Maps a courier's own status (its name, numeric ID or code, as the courier documents it) to a CourierStatus.
courier is TCS, Leopards, PostEx, M&P, Trax or BlueEx
(case-insensitive; LCS, MnP and Blue Ex are accepted), e.g.
Courier.postEx.value. raw is the status as the courier returned it:
a String, or a non-negative whole number (int, or a double such as
5.0), which matches the same status as its decimal string (5 and
'5', but not '0005'). Anything else gives null.
Matching ignores case and extra spaces. PostEx's Attempt Made: <reason>
history messages map to CourierStatus.attempted.
Only statuses the courier documents publicly are mapped: PostEx's API
guide and one Leopards status. TCS, M&P, Trax and BlueEx publish no list,
so they always return null, as does any status not in the list. No API
is called. Never throws.
Experimental: only PostEx and one Leopards status are mapped so far. The mapping and the CourierStatus values may change in a minor release as other couriers' lists are added.
Implementation
CourierStatus? mapCourierStatus(String courier, Object? raw) {
final name = _courierNames[_statusKey(courier)];
if (name == null) return null;
final String key;
if (raw is num) {
if (!isInteger(raw) || raw < 0) return null;
key = jsNumberToString(raw);
} else if (raw is String) {
key = _statusKey(raw);
} else {
return null;
}
final status = _statuses[name]![key];
if (status != null) return status;
if (name == Courier.postEx && _attemptMade.hasMatch(key)) {
return CourierStatus.attempted;
}
return null;
}