Skip to content
Merged
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
18 changes: 18 additions & 0 deletions scripts/test_calculators.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,24 @@ if (fs.existsSync(CALC_DIR)) {
// the generic tool, so an empty one means the page should not have shipped.
const tableRows = (html.match(/<tbody>[\s\S]*?<\/tbody>/i) || [''])[0].match(/<tr>/gi) || [];
if (tableRows.length === 0) fails.push(`calculators/${slug}: dose table is empty`);
// Either the page charts absolute doses, or it explains why it cannot.
// A page with neither has a hole where its main reason to exist should be.
const hasChart = /reconstitution chart/i.test(html);
const hasExplainer = /Why there is no units chart/i.test(html);
if (!hasChart && !hasExplainer) {
fails.push(`calculators/${slug}: no units chart and no explanation for its absence`);
}
if (hasChart && hasExplainer) {
fails.push(`calculators/${slug}: shows a chart and also says it has none`);
}
// A charted page must actually contain computed unit figures, not an empty grid.
if (hasChart && !/mL diluent/.test(html)) {
fails.push(`calculators/${slug}: chart has no diluent columns`);
}
// The FAQ and the refusal are the blocks that answer the query and keep the
// page inside the site's scope rule; neither is optional.
if (!/Questions people ask/i.test(html)) fails.push(`calculators/${slug}: no FAQ`);
if (!/will not take a body weight/i.test(html)) fails.push(`calculators/${slug}: no refusal statement`);
const m = html.match(/<script type="module">([\s\S]*?)<\/script>/i);
if (!m) { fails.push(`calculators/${slug}: no inline module script`); continue; }
const els = {};
Expand Down
16 changes: 16 additions & 0 deletions src/components/SourceIds.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
// Renders a claim's source ids as links into the record's own ledger. Lives in
// a component because Astro frontmatter is TypeScript, not JSX, so a helper
// that returns markup cannot be a function up there.
interface Props { ids: string[]; sources: Map<string, any> }
const { ids, sources } = Astro.props;
---
{ids.map((id, i) => {
const s = sources.get(id);
return (
<Fragment>
{i > 0 && <span>, </span>}
{s?.url ? <a href={s.url} rel="nofollow noopener">{id}</a> : <span>{id}</span>}
</Fragment>
);
})}
70 changes: 70 additions & 0 deletions src/lib/calculators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,73 @@ export const HUMAN_TIERS = new Set(['approved-label', 'human-clinical-trial', 'o
export function humanDoseRows(entry: CollectionEntry<'compounds'>): DoseRow[] {
return doseRows(entry).filter((r) => HUMAN_TIERS.has(r.tier));
}

/* ------------------------------------------------------------------ *
* The reconstitution chart
*
* Search intent for "<peptide> calculator" is mostly one question asked
* in different words: how many units is this dose, in this vial, with
* this much water. People want to read that off a table, not type it in,
* which is why every page that ranks for these queries has a chart and
* the first version of these pages did not.
*
* The chart is arithmetic, so it needs no new evidence. What makes it
* specific to a compound is its rows: the absolute doses that studies of
* that compound actually administered, rather than a generic ladder.
* ------------------------------------------------------------------ */

/** Absolute mass doses, in micrograms, as quoted in a record's claims.
* Per-kilogram figures are excluded: they do not convert to a person. */
export function absoluteDosesMcg(entry: CollectionEntry<'compounds'>): number[] {
const claims = ((entry.data as any).attributes?.study_doses ?? []) as any[];
const out = new Set<number>();
for (const c of claims) {
const text = String(c.value ?? '');
// "10 µg/kg", "0.2 mg/kg" and friends are per-weight; skip the whole match.
const re = /(\d+(?:\.\d+)?)\s*(mg|µg|mcg|ug)\b(\s*\/\s*kg)?/gi;
for (const m of text.matchAll(re)) {
if (m[3]) continue;
const n = parseFloat(m[1]);
if (!Number.isFinite(n) || n <= 0) continue;
const mcg = /mg/i.test(m[2]) ? n * 1000 : n;
if (mcg >= 1 && mcg <= 100_000) out.add(Math.round(mcg * 100) / 100);
}
}
return [...out].sort((a, b) => a - b).slice(0, 8);
}

export const VIAL_MG = [2, 5, 10] as const;
export const DILUENT_ML = [1, 2, 3] as const;

export interface ChartCell { ml: number; units: number; }
export interface ChartRow { mcg: number; label: string; cells: ChartCell[]; }

export function formatDose(mcg: number): string {
return mcg >= 1000
? `${(mcg / 1000).toLocaleString('en-US', { maximumFractionDigits: 3 })} mg`
: `${mcg.toLocaleString('en-US', { maximumFractionDigits: 2 })} mcg`;
}

/** Units on a U-100 syringe for each diluent volume, at one vial size. */
export function chartRows(doses: number[], vialMg: number): ChartRow[] {
return doses.map((mcg) => ({
mcg,
label: formatDose(mcg),
cells: DILUENT_ML.map((ml) => {
const concMcgPerMl = (vialMg * 1000) / ml;
const drawMl = mcg / concMcgPerMl;
return { ml, units: Math.round(drawMl * 100 * 100) / 100 };
}),
}));
}

/** Claims from any attribute, shaped like doseRows. */
export function claimRows(entry: CollectionEntry<'compounds'>, key: string): DoseRow[] {
const claims = ((entry.data as any).attributes?.[key] ?? []) as any[];
return claims.map((c) => ({
value: String(c.value ?? ''),
tier: String(c.evidence_label ?? 'editorial'),
sourceIds: (c.source_ids ?? []) as string[],
excerpt: c.source_excerpt,
}));
}
Loading
Loading