All files / complex-js/methods toString.ts

0% Statements 0/53
0% Branches 0/36
0% Functions 0/11
0% Lines 0/51

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131                                                                                                                                                                                                                                                                     
import { IComplex } from '../internal/complex';
import getAbs from './getAbs';
import getArg from './getArg';
import getImag from './getImag';
import getReal from './getReal';
 
type Coordinates = 'c' | 'p';
type Radix = 'X' | 'x' | 'o' | 'b' | '';
type Specifier = 'r' | 'i' | 'm' | 'a';
interface IFormat {
  minus: boolean;
  plus: boolean;
  pound: boolean;
  precision: number | '';
  radix: Radix;
  specifier: Specifier;
  width: number;
  zero: boolean;
}
 
const fmtCoord = /%([cp])/g;
const fmtParts = /%([#0+-]{0,4})(\d{0,2})((?:\.\d{0,2})?)([Xxob]?)([rima])/g;
 
export default function toString (z: IComplex, format = '%c'): string {
  return format
    .replace(fmtCoord, replaceCoord)
    .replace(fmtParts, replaceParts(z));
}
 
function replaceCoord (_: string, coord: Coordinates): string {
  switch (coord) {
    case 'c': return '%r%+i*i';
    case 'p': return '%m*e**(%a*i)';
  }
}
 
function replaceParts (z: IComplex): (...args: string[]) => string {
  return (_, flag, width, precision, radix, specifier) => {
    return stringify(z, {
      minus: flag.includes('-'),
      plus: flag.includes('+'),
      pound: flag.includes('#'),
      precision: precision && +precision.slice(1),
      radix: radix as Radix,
      specifier: specifier as Specifier,
      width: +width,
      zero: flag.includes('0')
    });
  };
}
 
function stringify (z: IComplex, format: IFormat): string {
  return width(precision(radix(specifier())));
 
  function specifier (): number {
    switch (format.specifier) {
      case 'r': return getReal(z);
      case 'i': return getImag(z);
      case 'm': return getAbs(z);
      case 'a': return getArg(z);
    }
  }
 
  function radix (num: number): string {
    switch (format.radix) {
      case 'X': return num.toString(16).toUpperCase();
      case 'x': return num.toString(16).toLowerCase();
      case 'o': return num.toString(8);
      case 'b': return num.toString(2);
      case '': return num.toString(10);
    }
  }
 
  function precision (str: string): string {
    const target = format.precision;
    const index = str.indexOf('.') + 1;
 
    if (target === '') return str;
 
    if (index === 0) {
      if (target === 0) return str;
 
      return `${str}.${'0'.repeat(target)}`;
    }
 
    const digits = str.length - index;
    const remove = Math.max(digits - target, 0);
    const insert = Math.max(target - digits, 0);
 
    return str.slice(0, str.length - remove) + '0'.repeat(insert);
  }
 
  function width (str: string): string {
    if (format.minus) {
      return plus(pound(str)).padEnd(format.width, ' ');
    }
 
    if (!format.zero) {
      return plus(pound(str)).padStart(format.width, ' ');
    }
 
    if (!str.startsWith('-')) {
      return plus(pound(str.padStart(format.width, '0')));
    }
 
    return plus(pound(`-${str.slice(1).padStart(format.width - 1, '0')}`));
  }
 
  function plus (str: string) {
    if (!format.plus || str.startsWith('-')) {
      return str;
    }
 
    return `+${str}`;
  }
 
  function pound (str: string): string {
    if (!format.pound || !format.radix) {
      return str;
    }
 
    const base = `0${format.radix}`;
 
    if (!str.startsWith('-')) {
      return base + str;
    }
 
    return `-${base + str.slice(1)}`;
  }
}