Get the name of a number, even if it's stupidly big.
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
To repozytorium jest zarchiwizowane. Możesz wyświetlać pliki i je sklonować, ale nie możesz do niego przepychać zmian lub otwierać zgłoszeń/Pull Requestów.

45 wiersze
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. }