forked from Yara724/api
Fix steps
This commit is contained in:
@@ -72,3 +72,71 @@ export function legacyAngleDamagedPartFieldKeysToUnset(
|
||||
}
|
||||
return [...new Set(out)];
|
||||
}
|
||||
|
||||
/** Same English title derived from snake_case keys as capture-requirements UI (legacy client storage). */
|
||||
export function legacyDamagedPartTitleEnFromPartKey(partKey: string): string {
|
||||
return String(partKey)
|
||||
.replace(/([a-z])([A-Z])/g, "$1 $2")
|
||||
.replace(/_/g, " ")
|
||||
.trim()
|
||||
.replace(/\b\w/g, (c) => c.toUpperCase());
|
||||
}
|
||||
|
||||
function normalizeDamagedPartKeyLoose(s: string): string {
|
||||
return String(s).toLowerCase().replace(/[\s_-]/g, "");
|
||||
}
|
||||
|
||||
export function getDamagedPartCaptureBlob(
|
||||
damagedPartsData: unknown,
|
||||
partKey: string,
|
||||
): unknown {
|
||||
const pk = String(partKey ?? "").trim();
|
||||
if (!pk || damagedPartsData == null) return undefined;
|
||||
let b = getCaptureBlob(damagedPartsData, pk);
|
||||
if (b) return b;
|
||||
const titleEn = legacyDamagedPartTitleEnFromPartKey(pk);
|
||||
if (titleEn !== pk) {
|
||||
b = getCaptureBlob(damagedPartsData, titleEn);
|
||||
if (b) return b;
|
||||
}
|
||||
const target = normalizeDamagedPartKeyLoose(pk);
|
||||
const keys =
|
||||
damagedPartsData instanceof Map
|
||||
? Array.from((damagedPartsData as Map<string, unknown>).keys())
|
||||
: Object.keys(damagedPartsData as Record<string, unknown>);
|
||||
for (const k of keys) {
|
||||
if (normalizeDamagedPartKeyLoose(k) === target) {
|
||||
b = getCaptureBlob(damagedPartsData, k);
|
||||
if (b) return b;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* True if a capture exists for catalog/selected part key {@code partKey}, including blobs stored
|
||||
* under humanized keys (e.g. {@code Left Backfender} vs {@code left_backfender}).
|
||||
*/
|
||||
export function hasDamagedPartCapture(
|
||||
damagedPartsData: unknown,
|
||||
partKey: string,
|
||||
): boolean {
|
||||
return getDamagedPartCaptureBlob(damagedPartsData, partKey) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Map a client {@code captureKey} to the canonical {@code damage.selectedParts} id when unambiguous.
|
||||
*/
|
||||
export function resolveCanonicalDamagedPartStorageKey(
|
||||
captureKeyRaw: string,
|
||||
selectedParts: string[],
|
||||
): string {
|
||||
const raw = String(captureKeyRaw ?? "").trim();
|
||||
if (!raw) return raw;
|
||||
const parts = (selectedParts ?? []).map((p) => String(p).trim()).filter(Boolean);
|
||||
if (parts.includes(raw)) return raw;
|
||||
const n = normalizeDamagedPartKeyLoose(raw);
|
||||
const matches = parts.filter((p) => normalizeDamagedPartKeyLoose(p) === n);
|
||||
if (matches.length === 1) return matches[0];
|
||||
return raw;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user