Skip to content
3 changes: 1 addition & 2 deletions src/read/digits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@ const DIGIT_MAP: readonly string[] = [
* Get Vietnamese word for a digit
*/
export function getDigitWord(digit: string): string {
const d = Number(digit)
return DIGIT_MAP[d] || ''
return DIGIT_MAP[Number(digit)] || ''
}
28 changes: 11 additions & 17 deletions src/read/groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import { allFollowingGroupsAreZero } from './utils.ts'
*/
export function calculateGroupTypes(groupCount: number): number[] {
const groupTypes: number[] = []

for (let i = groupCount - 1, type = 0; i >= 0; i--) {
groupTypes[i] = type
type++
if (type === 4) type = 1 // cycle back after billion
}

return groupTypes
}

Expand Down Expand Up @@ -101,21 +103,13 @@ export function processGroup(
const positionFromRight = groups.length - 1 - index
const hasTrailingZeros = allFollowingGroupsAreZero(groups, index)

if (index === 0) {
const nextGroupType = groups.length > 1 ? groupTypes[1] : -1
return processFirstGroup(
group,
type,
nextGroupType,
positionFromRight,
hasTrailingZeros,
)
}

return processSubsequentGroup(
group,
type,
positionFromRight,
hasTrailingZeros,
)
return index === 0
? processFirstGroup(
group,
type,
groups.length > 1 ? groupTypes[1] : -1, // nextGroupType: the type of the next group to determine if we're before billion group
positionFromRight,
hasTrailingZeros,
)
: processSubsequentGroup(group, type, positionFromRight, hasTrailingZeros)
}
28 changes: 11 additions & 17 deletions src/read/three-digits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@ import { getDigitWord } from './digits.ts'
* Read the "hundreds" digit
*/
function readHundreds(first: string, hasHundredsPosition: boolean): string {
if (!hasHundredsPosition) return ''
return `${getDigitWord(first)} trăm`
return !hasHundredsPosition ? '' : `${getDigitWord(first)} trăm`
}

/**
* Read the "tens" digit
*/
function readTens(second: string, hasTensPosition: boolean): string {
if (!hasTensPosition) return ''

if (second === '0') return ' lẻ'
if (second === '1') return ' mười'
return ` ${getDigitWord(second)} mươi`
return !hasTensPosition
? ''
: second === '0'
? ' lẻ'
: second === '1'
? ' mười'
: ` ${getDigitWord(second)} mươi`
}

/**
Expand All @@ -41,6 +42,7 @@ function readOnes(
if (last !== '0') {
return ` ${getDigitWord(last)}`
}

return ''
}

Expand Down Expand Up @@ -94,20 +96,12 @@ export function readFirstGroupBeforeBillion(group: string): string {
* Read the first group in the number sequence (normal case)
*/
export function readFirstGroup(group: string): string {
if (isAllZeros(group)) {
return 'không'
}

return readThreeDigitsCore(group)
return isAllZeros(group) ? 'không' : readThreeDigitsCore(group)
}

/**
* Read the later (non-first) group in the number sequence
*/
export function readSubsequentGroup(group: string): string {
if (isAllZeros(group)) {
return ''
}

return readThreeDigitsCore(group)
return isAllZeros(group) ? '' : readThreeDigitsCore(group)
}
2 changes: 2 additions & 0 deletions src/read/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ export function allFollowingGroupsAreZero(
): boolean {
for (let j = index + 1; j < groups.length; j++) {
const g = groups[j]

if (g !== '000' && g !== '00' && g !== '0') {
return false
}
}

return true
}
Loading