Get the name of a number, even if it's stupidly big.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

46 rivejä
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. }