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.

46 wiersze
1.2 KiB

  1. import { Transform } from 'stream'
  2. import NumberSystem from './NumberSystem'
  3. import { ReadStreamOptions } from './common'
  4. export default class ConverterStream extends Transform {
  5. private thousandPower: number = 0
  6. constructor(private readonly system: NumberSystem, private readonly options: ReadStreamOptions) {
  7. super()
  8. this.thousandPower = 0
  9. }
  10. _transform(chunk: any, _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)) // todo use encoding
  17. if (!(ones === 0 && tens === 0 && hundreds === 0)) {
  18. if (this.thousandPower > 0) {
  19. this.push(
  20. this.system.getKiloName[scale!]!(this.thousandPower)
  21. .split('')
  22. .reverse()
  23. .join(''),
  24. )
  25. this.push(' ')
  26. }
  27. this.push(
  28. this.system
  29. .getKiloCount(hundreds, tens, ones)
  30. .split('')
  31. .reverse()
  32. .join(''),
  33. )
  34. this.push(' ')
  35. }
  36. this.thousandPower = this.thousandPower + 1
  37. callback()
  38. }
  39. }