refactor: normalize location display by stripping state prefixes from cities and uppercasing state codes
This commit is contained in:
@@ -96,10 +96,12 @@ class AgentProfile {
|
||||
}
|
||||
|
||||
String get location {
|
||||
// Direct fields first (state first, then city)
|
||||
if (city != null && state != null) return '$state, $city';
|
||||
if (state != null) return state!;
|
||||
if (city != null) return city!;
|
||||
// Direct fields first (state first, then city) — normalized
|
||||
if (city != null && state != null) {
|
||||
return '${_normalizeState(state!)}, ${_stripStatePrefix(city!)}';
|
||||
}
|
||||
if (state != null) return _normalizeState(state!);
|
||||
if (city != null) return _stripStatePrefix(city!);
|
||||
|
||||
// Fallback: extract from fieldValues (matches web getLocationString)
|
||||
String? fvCity;
|
||||
@@ -107,9 +109,9 @@ class AgentProfile {
|
||||
for (final fv in fieldValues) {
|
||||
final slug = fv.fieldSlug.toLowerCase();
|
||||
if (slug == 'city' || slug == 'city_name') {
|
||||
fvCity = _extractFieldString(fv);
|
||||
fvCity = _extractFieldString(fv, isCity: true);
|
||||
} else if (slug == 'state' || slug == 'state_name') {
|
||||
fvState = _extractFieldString(fv);
|
||||
fvState = _extractFieldString(fv, isCity: false);
|
||||
}
|
||||
}
|
||||
final parts = [fvState, fvCity].where((s) => s != null && s.isNotEmpty);
|
||||
@@ -121,20 +123,50 @@ class AgentProfile {
|
||||
return '';
|
||||
}
|
||||
|
||||
/// Uppercase 2-letter state codes (e.g. "nd" → "ND"); leave full names alone.
|
||||
static String _normalizeState(String raw) {
|
||||
final trimmed = raw.trim();
|
||||
if (trimmed.length == 2) return trimmed.toUpperCase();
|
||||
return trimmed;
|
||||
}
|
||||
|
||||
/// Strip the 2-letter state prefix from a city value ("nd_hurley" → "Hurley",
|
||||
/// "Nd Hurley" → "Hurley"). Matches web's `stripCityPrefix` behavior.
|
||||
static String _stripStatePrefix(String raw) {
|
||||
var s = raw.trim();
|
||||
// Snake-case prefix: nd_hurley
|
||||
s = s.replaceFirst(RegExp(r'^[a-z]{2}_', caseSensitive: false), '');
|
||||
// Title-cased prefix token: "Nd Hurley"
|
||||
s = s.replaceFirst(RegExp(r'^[A-Za-z]{2}\s+'), '');
|
||||
return s;
|
||||
}
|
||||
|
||||
/// Extract a display string from a field value (handles jsonValue arrays).
|
||||
static String? _extractFieldString(AgentFieldValue fv) {
|
||||
/// For city fields, strip the state-code prefix before title-casing.
|
||||
static String? _extractFieldString(AgentFieldValue fv, {required bool isCity}) {
|
||||
String format(String raw) {
|
||||
var s = raw;
|
||||
if (isCity) {
|
||||
// Strip slug state prefix: "nd_hurley" → "hurley"
|
||||
s = s.replaceFirst(RegExp(r'^[a-z]{2}_', caseSensitive: false), '');
|
||||
}
|
||||
final titled = s
|
||||
.split('_')
|
||||
.map((w) => w.isNotEmpty
|
||||
? '${w[0].toUpperCase()}${w.substring(1).toLowerCase()}'
|
||||
: '')
|
||||
.join(' ');
|
||||
if (!isCity && titled.length == 2) return titled.toUpperCase();
|
||||
return titled;
|
||||
}
|
||||
|
||||
if (fv.jsonValue is List && (fv.jsonValue as List).isNotEmpty) {
|
||||
final val = (fv.jsonValue as List).first;
|
||||
if (val is String && val.isNotEmpty) {
|
||||
return val
|
||||
.split('_')
|
||||
.map((w) => w.isNotEmpty
|
||||
? '${w[0].toUpperCase()}${w.substring(1).toLowerCase()}'
|
||||
: '')
|
||||
.join(' ');
|
||||
}
|
||||
if (val is String && val.isNotEmpty) return format(val);
|
||||
}
|
||||
if (fv.textValue != null && fv.textValue!.isNotEmpty) {
|
||||
return format(fv.textValue!);
|
||||
}
|
||||
if (fv.textValue != null && fv.textValue!.isNotEmpty) return fv.textValue;
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user