mirror of
https://github.com/bitbaum/substrata.git
synced 2026-12-04 08:39:02 +01:00
* fix(graph): a flow's commodity is part of its relation id Uranium and refined copper from Kazakhstan to China in the same year are two relations, but the id was from|type|to|asOf, so the second was skipped as a duplicate. One rule now (lib/graph/id.ts) is shared by the graph validator and the candidate path; ids without a stated commodity are unchanged. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018WZnBnaLP6RhwFkLxRTKgE * style: prettier on run-task Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018WZnBnaLP6RhwFkLxRTKgE --------- Co-authored-by: Claude <noreply@anthropic.com>
28 lines
970 B
TypeScript
28 lines
970 B
TypeScript
import type { Relation } from './types';
|
|
|
|
/** A commodity as it appears in an id: lowercase words joined by hyphens. */
|
|
export function commoditySlug(commodity: string): string {
|
|
return commodity
|
|
.toLowerCase()
|
|
.replace(/[^a-z0-9]+/g, '-')
|
|
.replace(/^-|-$/g, '');
|
|
}
|
|
|
|
/**
|
|
* The one rule for a relation's stable id.
|
|
*
|
|
* A flow names what moved: uranium and refined copper from the same country to
|
|
* the same partner in the same year are two relations, so the commodity is part
|
|
* of the id whenever the source states one. A relation without a stated
|
|
* commodity keeps the id it always had.
|
|
*/
|
|
export function relationId(
|
|
r: Pick<Relation, 'from' | 'type' | 'to' | 'asOf'> & {
|
|
volume?: Pick<NonNullable<Relation['volume']>, 'commodity'>;
|
|
},
|
|
): string {
|
|
const base = `${r.from}|${r.type}|${r.to}|${r.asOf}`;
|
|
const commodity = r.volume?.commodity ? commoditySlug(r.volume.commodity) : '';
|
|
return commodity ? `${base}|${commodity}` : base;
|
|
}
|