All files / src/functions/convertCoords coords2Glyphs.ts

94.87% Statements 37/39
83.33% Branches 10/12
100% Functions 1/1
94.87% Lines 37/39

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 461x   1x 14x   13x 13x 13x 13x   13x 13x 13x 13x   13x 13x 13x 14x   14x 13x 13x 14x 2x 14x 11x 11x 14x   14x 13x 13x   13x 13x 13x 13x 13x 13x 13x 13x   14x 14x  
import { exactGlyphLength, maxGalacticCoordLength } from '@/constants/restrictions';
 
export function coords2Glyphs(coords: string): string {
  if (coords.length !== maxGalacticCoordLength) return coords;
 
  const X_Z_POS_SHIFT = 2049;
  const X_Z_NEG_SHIFT = 2047;
  const Y_POS_SHIFT = 129;
  const Y_NEG_SHIFT = 127;
 
  const x_coords = parseInt(coords.substring(0, 4), 16); // NoSonar X coordinate part
  const y_coords = parseInt(coords.substring(5, 9), 16); // NoSonar Y coordinate part
  const z_coords = parseInt(coords.substring(10, 14), 16); // NoSonar Z coordinate part
  const system_idx = parseInt(coords.substring(15, 19), 16); // NoSonar system index part
 
  let portal_x = 0;
  let portal_y = 0;
  let portal_z = 0;
  if (x_coords < X_Z_NEG_SHIFT) {
    portal_x = x_coords + X_Z_POS_SHIFT;
  } else {
    portal_x = x_coords - X_Z_NEG_SHIFT;
  }
  if (z_coords < X_Z_NEG_SHIFT) {
    portal_z = z_coords + X_Z_POS_SHIFT;
  } else {
    portal_z = z_coords - X_Z_NEG_SHIFT;
  }
  if (y_coords < Y_NEG_SHIFT) {
    portal_y = y_coords + Y_POS_SHIFT;
  } else {
    portal_y = y_coords - Y_NEG_SHIFT;
  }
 
  const glyphs = [
    '0',
    system_idx.toString(16).toUpperCase().padStart(3, '0'),
    portal_y.toString(16).toUpperCase().padStart(2, '0'),
    portal_z.toString(16).toUpperCase().padStart(3, '0'),
    portal_x.toString(16).toUpperCase().padStart(3, '0'),
  ];
  const glyphString = glyphs.join('');
 
  return glyphString.length === exactGlyphLength ? glyphString : '';
}