Functions that extract dollar amounts from text and verify currency consistency against a reference list.
// Source: https://github.com/moeru-ai/airi | License: MIT | Copyright 2024-PRESENT Neko Ayaka
// Provided via justclaw.it.com; keep this notice with the code.
import type { CurrencyItem } from '../../cases/formula-speech/cases'
import { canonicalize } from './normalize'
/** A dollar amount with a sign before it (`$5`, `US$5`, `USD 5`), or a word after it (`5 dollars`, `5美元`). */
const amount = /(?:\$|USD|US\$)\s*(\d[\d,]*(?:\.\d+)?)|(\d[\d,]*(?:\.\d+)?)\s*(?:dollars?|USD|美元|美金|ドル|달러)/g
/**
* Lists the US dollar amounts in a sentence, sorted.
*
* @example
* dollarAmounts('Apples cost \\$3, and rent is 1,200 dollars')
* // => [3, 1200]
*/
export function dollarAmounts(text: string): number[] {
const normalized = text.replaceAll('\\$', '$').replaceAll('$', '$')
return [...normal
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search for more like it.
Get a free API key