n2words
Version:
Convert numbers to words in 70+ languages with zero dependencies. Supports BigInt, decimals, and browser/Node.js environments.
321 lines (265 loc) • 9.29 kB
JavaScript
/**
* Indonesian (Indonesia) language converter
*
* CLDR: id-ID | Indonesian as used in Indonesia
*
* Key features:
* - "Se-" prefix for 100 (seratus) and 1000 (seribu)
* - Regular patterns (puluh for tens, ratus for hundreds)
* - Teens with "belas" suffix
* - Indonesian uses "satu juta" (not "sejuta") for millions+
*/
import { parseCardinalValue } from './utils/parse-cardinal.js'
import { parseCurrencyValue } from './utils/parse-currency.js'
import { parseOrdinalValue } from './utils/parse-ordinal.js'
import { checkMax } from './utils/check-max.js'
import { western } from './utils/scale.js'
// ============================================================================
// Vocabulary
// ============================================================================
const ONES = ['', 'satu', 'dua', 'tiga', 'empat', 'lima', 'enam', 'tujuh', 'delapan', 'sembilan']
const TEENS = ['sepuluh', 'sebelas', 'dua belas', 'tiga belas', 'empat belas', 'lima belas', 'enam belas', 'tujuh belas', 'delapan belas', 'sembilan belas']
const TENS = ['', '', 'dua puluh', 'tiga puluh', 'empat puluh', 'lima puluh', 'enam puluh', 'tujuh puluh', 'delapan puluh', 'sembilan puluh']
const HUNDRED_WORD = 'ratus'
const THOUSAND_WORD = 'ribu'
const SCALE_WORDS = ['juta', 'miliar', 'triliun', 'kuadriliun', 'kuantiliun', 'sekstiliun', 'septiliun', 'oktiliun', 'noniliun', 'desiliun']
// Supported magnitude ceiling (checked at the public entry points), derived from the scale table.
export const cardinalMax = western(SCALE_WORDS.length + 1)
export const ordinalMax = western(SCALE_WORDS.length + 1)
export const currencyMax = western(SCALE_WORDS.length + 1)
const ZERO = 'nol'
const NEGATIVE = 'min'
const DECIMAL_SEP = 'koma'
// ============================================================================
// Ordinal Vocabulary
// ============================================================================
const ORDINAL_PREFIX = 'ke'
// First is special: "pertama" (not "kesatu")
const ORDINAL_FIRST = 'pertama'
// ============================================================================
// Currency Vocabulary (Indonesian Rupiah)
// ============================================================================
const RUPIAH = 'rupiah'
// ============================================================================
// Segment Building
// ============================================================================
/**
* Builds the Indonesian words for a 1-3 digit segment (0-999).
* @param {number} n - The segment value (0-999)
* @returns {string} The segment in Indonesian words
*/
function buildSegment(n) {
if (n === 0) return ''
const onesDigit = n % 10
const tensDigit = Math.trunc(n / 10) % 10
const hundredsDigit = Math.trunc(n / 100)
const parts = []
// Hundreds: seratus (100) or N ratus (200-900)
if (hundredsDigit > 0) {
if (hundredsDigit === 1) {
parts.push('se' + HUNDRED_WORD)
}
else {
parts.push(ONES[hundredsDigit] + ' ' + HUNDRED_WORD)
}
}
// Tens and ones
const tensOnes = n % 100
if (tensOnes === 0) {
// Just hundreds
}
else if (tensOnes < 10) {
parts.push(ONES[tensOnes])
}
else if (tensOnes < 20) {
parts.push(TEENS[tensOnes - 10])
}
else if (onesDigit === 0) {
parts.push(TENS[tensDigit])
}
else {
parts.push(TENS[tensDigit] + ' ' + ONES[onesDigit])
}
return parts.join(' ')
}
// ============================================================================
// Conversion Functions
// ============================================================================
/**
* Converts a non-negative integer to Indonesian words.
* @param {bigint} n - The integer value to convert
* @returns {string} The integer in Indonesian words
*/
function integerToWords(n) {
if (n === 0n) return ZERO
if (n < 1000n) {
return buildSegment(Number(n))
}
if (n < 1_000_000n) {
const thousands = Number(n / 1000n)
const remainder = Number(n % 1000n)
let result
if (thousands === 1) {
result = 'se' + THOUSAND_WORD
}
else {
result = buildSegment(thousands) + ' ' + THOUSAND_WORD
}
if (remainder > 0) {
result += ' ' + buildSegment(remainder)
}
return result
}
return buildLargeNumberWords(n)
}
/**
* Builds Indonesian words for large numbers (1,000,000 and above).
* @param {bigint} n - The integer value to convert
* @returns {string} The number in Indonesian words
*/
function buildLargeNumberWords(n) {
const numStr = n.toString()
const len = numStr.length
const segments = []
const segmentSize = 3
const remainderLen = len % segmentSize
let pos = 0
if (remainderLen > 0) {
segments.push(Number(numStr.slice(0, remainderLen)))
pos = remainderLen
}
while (pos < len) {
segments.push(Number(numStr.slice(pos, pos + segmentSize)))
pos += segmentSize
}
const parts = []
let scaleIndex = segments.length - 1
for (let i = 0; i < segments.length; i++) {
const segment = segments[i]
if (segment !== 0) {
if (scaleIndex === 0) {
parts.push(buildSegment(segment))
}
else if (scaleIndex === 1) {
if (segment === 1) {
parts.push('se' + THOUSAND_WORD)
}
else {
parts.push(buildSegment(segment) + ' ' + THOUSAND_WORD)
}
}
else {
// Indonesian: "satu juta" not "sejuta"
const scaleWord = SCALE_WORDS[scaleIndex - 2]
parts.push(buildSegment(segment) + ' ' + scaleWord)
}
}
scaleIndex--
}
return parts.join(' ')
}
/**
* Converts the decimal-part digit string to Indonesian words.
* @param {string} decimalPart - The decimal digits as a string
* @returns {string} The decimal part in Indonesian words
*/
function decimalPartToWords(decimalPart) {
let result = ''
let i = 0
while (i < decimalPart.length && decimalPart[i] === '0') {
if (result) result += ' '
result += ZERO
i++
}
const remainder = decimalPart.slice(i)
if (remainder) {
if (result) result += ' '
result += integerToWords(BigInt(remainder))
}
return result
}
/**
* Converts a numeric value to Indonesian words.
* @param {number | string | bigint} value - The numeric value to convert
* @returns {string} The number in Indonesian words
*/
function toCardinal(value) {
const { isNegative, integerPart, decimalPart } = parseCardinalValue(value)
checkMax(integerPart, cardinalMax, decimalPart)
let result = ''
if (isNegative) {
result = NEGATIVE + ' '
}
result += integerToWords(integerPart)
if (decimalPart) {
result += ' ' + DECIMAL_SEP + ' ' + decimalPartToWords(decimalPart)
}
return result
}
// ============================================================================
// ORDINAL: toOrdinal(value)
// ============================================================================
/**
* Converts a non-negative integer to Indonesian ordinal words.
*
* Indonesian ordinals use "ke-" prefix + cardinal number.
* Special case: "pertama" for 1st (not "kesatu").
* @param {bigint} n - Positive integer to convert
* @returns {string} Indonesian ordinal words
*/
function integerToOrdinal(n) {
// Special case: 1st is "pertama"
if (n === 1n) {
return ORDINAL_FIRST
}
// All others: "ke" + cardinal (no hyphen in Indonesian)
return ORDINAL_PREFIX + integerToWords(n)
}
/**
* Converts a numeric value to Indonesian ordinal words.
* @param {number | string | bigint} value - The numeric value to convert (positive integer)
* @returns {string} The number as ordinal words
* @throws {TypeError} If value is not a valid numeric type
* @throws {RangeError} If value is negative, zero, or has a decimal part
* @example
* toOrdinal(1) // 'pertama'
* toOrdinal(2) // 'kedua'
* toOrdinal(10) // 'kesepuluh'
*/
function toOrdinal(value) {
const integerPart = parseOrdinalValue(value)
checkMax(integerPart, ordinalMax)
return integerToOrdinal(integerPart)
}
// ============================================================================
// CURRENCY: toCurrency(value)
// ============================================================================
/**
* Converts a numeric value to Indonesian currency words (Rupiah).
*
* Indonesian Rupiah has no subunit in modern usage (sen are historical).
* Amounts are rounded to whole rupiah.
* @param {number | string | bigint} value - The currency amount to convert
* @returns {string} The amount in Indonesian currency words
* @throws {TypeError} If value is not a valid numeric type
* @throws {Error} If value is not a valid number format
* @example
* toCurrency(42) // 'empat puluh dua rupiah'
* toCurrency(1000) // 'seribu rupiah'
* toCurrency(-5) // 'min lima rupiah'
*/
function toCurrency(value) {
const { isNegative, dollars: rupiah } = parseCurrencyValue(value)
checkMax(rupiah, currencyMax)
let result = ''
if (isNegative) {
result = NEGATIVE + ' '
}
result += integerToWords(rupiah)
result += ' ' + RUPIAH
return result
}
// ============================================================================
// Exports
// ============================================================================
export { toCardinal, toOrdinal, toCurrency }