speakUnit (src/units.ts)

airi · packages/speech-transformer-math/src/units.ts · typescript · MIT
voice

Utility that converts SI unit symbols into spoken words using locale data, handling prefixes and powers.

// 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 { UnitLocale } from './locale'

/**
 * Reads unit symbols with the SI rules of the SI brochure and NIST SP 811.
 *
 * A symbol is a prefix and a unit, for example `µm` is micro and meter. A locale pack names each prefix and unit once,
 * and this module composes them, so a unit gets every prefix without one entry for each pair.
 */

/** `da` is the only prefix of two letters, so a symbol tries it before the prefixes of one letter. */
const prefixLengths = [2, 1]
/**
 * Units that take only some prefixes. Without the limit, `ft` reads as femtotonnes and `pt` as picotonnes, and `dB`
 * would be decibytes if decibel had no 

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key