All files / src/functions/convertGlyphs glyphs2Coords.ts

100% Statements 44/44
100% Branches 12/12
100% Functions 1/1
100% Lines 44/44

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 451x 1x 1x 16x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 16x 1x 16x 14x 14x 15x 16x 2x 16x 13x 13x 15x 15x 1x 15x 14x 14x 15x 15x 15x 15x 15x 15x 15x 15x  
import { exactGlyphLength } from '@/constants/restrictions';
 
export function glyphs2Coords(glyphs: string) {
  if (glyphs.length !== exactGlyphLength) return '';
 
  const X_Z_POS_SHIFT = 2049;
  const X_Z_NEG_SHIFT = 2047;
  const Y_POS_SHIFT = 129;
  const Y_NEG_SHIFT = 127;
 
  const x_glyphs = parseInt(glyphs.substring(9, 12), 16); // NoSonar X coordinate part
  const y_glyphs = parseInt(glyphs.substring(4, 6), 16); // NoSonar Y coordinate part
  const z_glyphs = parseInt(glyphs.substring(6, 9), 16); // NoSonar Z coordinate part
  const system_idx = glyphs.substring(1, 4); // NoSonar system index part
 
  let coords_x = 0;
  let coords_y = 0;
  let coords_z = 0;
 
  if (x_glyphs >= X_Z_POS_SHIFT) {
    coords_x = x_glyphs - X_Z_POS_SHIFT;
  } else {
    coords_x = x_glyphs + X_Z_NEG_SHIFT;
  }
 
  if (z_glyphs >= X_Z_POS_SHIFT) {
    coords_z = z_glyphs - X_Z_POS_SHIFT;
  } else {
    coords_z = z_glyphs + X_Z_NEG_SHIFT;
  }
 
  if (y_glyphs >= Y_POS_SHIFT) {
    coords_y = y_glyphs - Y_POS_SHIFT;
  } else {
    coords_y = y_glyphs + Y_NEG_SHIFT;
  }
 
  const coordData = [coords_x, coords_y, coords_z];
  const coordinates = coordData.map((coord) => coord.toString(16).toUpperCase().padStart(4, '0'));
 
  coordinates[3] = system_idx.padStart(4, '0').toUpperCase(); // NoSonar the 4 is to bump it to a length of 4
 
  return coordinates.join(':');
}