Repository Public
1/**2 * Helpers shared by the quantitative-finance, infrastructure and method templates.3 * Pure functions: templates stay deterministic.4 */5import type { Locale, SheetInput } from "@/schema";6import { L } from "../types";78/** When the bibliographic records of these templates were checked against Crossref or the publisher page. */9export const CHECKED_AT = "2026-11-17T17:00:00.000Z";1011/** Raw strings: LaTeX can be written with single backslashes. */12export const r = String.raw;1314const NBSP = " ";1516/**17 * French typography outside inline maths, inline code and link targets: non-breaking spaces inside « » and18 * before : ; ? ! %, and typographic apostrophes.19 */20export function frTypo(s: string): string {21 return s22 .split(/(\$[^$]+\$|`[^`]+`|\]\([^)]*\)|\[\^[a-z0-9-]+\])/)23 .map((seg, i) =>24 i % 2 === 125 ? seg26 : seg27 .replace(/« /g, "«" + NBSP)28 .replace(/ »/g, NBSP + "»")29 .replace(/ ([:;?!%])/g, NBSP + "$1")30 .replace(/(\p{L})'(\p{L})/gu, "$1’$2"),31 )32 .join("");33}3435/** `L(locale)` with French typography applied to the French string. */36export const T = (locale: Locale) => {37 const l = L(locale);38 return (fr: string, en: string) => l(frTypo(fr), en);39};4041export type SourceInput = NonNullable<SheetInput["sources"]>[number];42export type BlockInput = SheetInput["blocks"][number];4344/** A source whose DOI record was fetched from Crossref and whose title, authors, year and container were copied from it. */45export function doiSource(46 id: string,47 type: SourceInput["type"],48 title: string,49 authors: string[],50 year: number,51 container: string | undefined,52 doi: string,53 note?: string,54): SourceInput {55 return { id, type, title, authors, year, ...(container ? { container } : {}), doi, url: `https://doi.org/${doi}`, ...(note ? { note } : {}), status: "resolved", checkedAt: CHECKED_AT };56}5758/** A source without DOI whose publisher or index page was opened and read. */59export function urlSource(60 id: string,61 type: SourceInput["type"],62 title: string,63 authors: string[],64 year: number,65 container: string | undefined,66 url: string,67 note?: string,68): SourceInput {69 return { id, type, title, authors, year, ...(container ? { container } : {}), url, ...(note ? { note } : {}), status: "resolved", checkedAt: CHECKED_AT };70}7172export const round = (x: number, d = 4) => Math.round(x * 10 ** d) / 10 ** d;73