Get the name of a number, even if it's stupidly big.
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
Este repositório está arquivado. Pode ver ficheiros e cloná-lo, mas não pode fazer envios ou lançar questões ou pedidos de integração.

48 linhas
1.4 KiB

  1. import { Transform } from 'stream'
  2. import { Digit, NumberSystem, ReadStreamOptions } from './common'
  3. export default class ConverterStream extends Transform {
  4. private thousandPower: bigint = 0n
  5. private buffer = Buffer.from([])
  6. constructor(private readonly system: NumberSystem, private readonly options: ReadStreamOptions) {
  7. super()
  8. this.thousandPower = 0n
  9. }
  10. _transform(chunk: Buffer, _encoding: BufferEncoding, callback: Function) {
  11. const { scale, encoding } = this.options
  12. const [ones = 0, tens = 0, hundreds = 0] = chunk
  13. .toString(encoding!)
  14. .split('')
  15. .filter((s: string) => /[0-9]/.test(s))
  16. .map((d: string) => Number(d) as Digit) // todo use encoding
  17. let kiloName: string | undefined
  18. let kiloCount: string
  19. if (!(ones === 0 && tens === 0 && hundreds === 0)) {
  20. if (this.thousandPower > 0n) {
  21. kiloName = this.system.getKiloName[scale!]!(this.thousandPower)
  22. }
  23. kiloCount = this.system.getKiloCount(hundreds, tens, ones)
  24. this.buffer = Buffer.concat([
  25. Buffer.from(this.system.joinKilo(kiloCount, kiloName), encoding),
  26. Buffer.from(' ', encoding),
  27. this.buffer,
  28. ])
  29. }
  30. this.thousandPower = this.thousandPower + 1n
  31. callback()
  32. }
  33. _flush(callback: Function) {
  34. const { encoding } = this.options
  35. this.push(this.buffer, encoding)
  36. callback()
  37. }
  38. }