|
- import { Transform } from 'stream'
- import { Digit, NumberSystem, ReadStreamOptions } from './common'
-
- export default class ConverterStream extends Transform {
- private thousandPower: bigint = 0n
-
- constructor(private readonly system: NumberSystem, private readonly options: ReadStreamOptions) {
- super()
- this.thousandPower = 0n
- }
-
- _transform(chunk: Buffer, _encoding: BufferEncoding, callback: Function) {
- const { scale, encoding } = this.options
-
- const [ones = 0, tens = 0, hundreds = 0] = chunk
- .toString(encoding!)
- .split('')
- .filter((s: string) => /[0-9]/.test(s))
- .map((d: string) => Number(d)) // todo use encoding
-
- if (!(ones === 0 && tens === 0 && hundreds === 0)) {
- if (this.thousandPower > 0) {
- this.push(
- this.system.getKiloName[scale!]!(this.thousandPower)
- .split('')
- .reverse()
- .join(''),
- )
- this.push(' ')
- }
- this.push(
- this.system
- .getKiloCount(hundreds as Digit, tens as Digit, ones as Digit)
- .split('')
- .reverse()
- .join(''),
- )
- this.push(' ')
- }
-
- this.thousandPower = this.thousandPower + 1n
- callback()
- }
- }
|