Get the name of a number, even if it's stupidly big.
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
Este repositorio está archivado. Puede ver los archivos y clonarlo, pero no puede subir cambios o reportar incidencias ni pedir Pull Requests.

45 líneas
1.2 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. constructor(private readonly system: NumberSystem, private readonly options: ReadStreamOptions) {
  6. super()
  7. this.thousandPower = 0n
  8. }
  9. _transform(chunk: Buffer, _encoding: BufferEncoding, callback: Function) {
  10. const { scale, encoding } = this.options
  11. const [ones = 0, tens = 0, hundreds = 0] = chunk
  12. .toString(encoding!)
  13. .split('')
  14. .filter((s: string) => /[0-9]/.test(s))
  15. .map((d: string) => Number(d)) // todo use encoding
  16. if (!(ones === 0 && tens === 0 && hundreds === 0)) {
  17. if (this.thousandPower > 0) {
  18. this.push(
  19. this.system.getKiloName[scale!]!(this.thousandPower)
  20. .split('')
  21. .reverse()
  22. .join(''),
  23. )
  24. this.push(' ')
  25. }
  26. this.push(
  27. this.system
  28. .getKiloCount(hundreds as Digit, tens as Digit, ones as Digit)
  29. .split('')
  30. .reverse()
  31. .join(''),
  32. )
  33. this.push(' ')
  34. }
  35. this.thousandPower = this.thousandPower + 1n
  36. callback()
  37. }
  38. }