All files / asm80-core/utils utils.js

100% Statements 101/101
100% Branches 20/20
87.5% Functions 7/8
100% Lines 101/101

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 1011x 1x 1x 1x 1x 1x 2537x 2537x 2537x 2537x 2537x 2537x 2537x 25x 25x 2537x 2537x 2537x 2537x 903x 903x 1634x 1634x 2537x 21142x 21142x 1634x 1634x 1x 1x 1x 1x 1x 1x 1x 1x 2568x 2568x 2568x 21195x 21195x 2568x 1x 1x 1x 1x 1x 1x 1x 1x 60x 60x 2568x 2568x 2568x 2568x 60x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10076x 10076x 10076x 3364x 3364x 10076x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x
/**
 * Normalize line structures by cleaning up whitespace and HTML entities
 * @param {Object[]} xs - Array of line objects with 'line' property
 * @returns {Object[]} Array of normalized line objects
 */
export const norm = (xs) => xs.map((lx) => {
    let l = lx.line;
    // Decode HTML entities
    l = l.replace("&lt;", "<");
    l = l.replace("&gt;", ">");
    
    // Remove trailing spaces
    while (l[l.length - 1] == " ") {
      l = l.substr(0, l.length - 1);
    }
    lx.line = l;
    
    // If line doesn't start with space, return as-is
    if (l[0] != " ") {
      return lx;
    }
    
    // Remove leading spaces but keep one space prefix
    while (l[0] == " ") {
      l = l.substr(1);
    }
    lx.line = " " + l;
    return lx;
  });
 
/**
 * Filter out empty lines from line array
 * @param {Object[]} xs - Array of line objects with 'line' property
 * @returns {Object[]} Array with empty lines removed
 */
export const nonempty = (xs) => xs.filter((lx) => {
    let l = lx.line;
    // Remove leading spaces to check if line has content
    while (l[0] == " ") {
      l = l.substr(1);
    }
    return l.length ? true : false;
  });
 
/**
 * Convert string array to internal line structure with metadata
 * @param {string[]} xs - Array of source code lines
 * @returns {Object[]} Array of line objects with line number and metadata
 */
export const toInternal = (xs) => {
    let numLine = 1;
    return xs.map((line) => ({
      line: line,           // Original line text
      numline: numLine++,   // Line number (1-based)
      addr: null,           // Address in code (filled later)
      bytes: 0,             // Number of bytes for this instruction (filled later)
    }));
  };
 
/**
 * Convert number to hexadecimal string with specified digit count
 * @param {number} n - Number to convert
 * @param {number} d - Number of hex digits
 * @returns {string} Uppercase hexadecimal string with leading zeros
 */
  const toHexN = (n, d) => {
    let s = n.toString(16);
    // Pad with leading zeros
    while (s.length < d) {
      s = "0" + s;
    }
    return s.toUpperCase();
  };
 
/**
 * Convert number to 2-digit hexadecimal string (byte)
 * @param {number} n - Number to convert (0-255)
 * @returns {string} 2-character uppercase hex string
 */
  export const toHex2 = (n) => toHexN(n & 0xff, 2);
 
/**
 * Convert number to 4-digit hexadecimal string (word)
 * @param {number} n - Number to convert (0-65535)
 * @returns {string} 4-character uppercase hex string
 */
  export const toHex4 = (n) => toHexN(n, 4);
 
/**
 * Convert number to 6-digit hexadecimal string (24-bit)
 * @param {number} n - Number to convert (0-16777215)
 * @returns {string} 6-character uppercase hex string
 */
  export const toHex6 = (n) => toHexN(n, 6);
 
/**
 * Convert number to 8-digit hexadecimal string (dword)
 * @param {number} n - Number to convert
 * @returns {string} 8-character uppercase hex string
 */
  export const toHex8 = (n) => toHexN(n, 8);