Sha256: f4f7b935259f648f72e8fa4cc8dbe5c3e2d615913dd76e226b98e3befdab3ff0

Contents?: true

Size: 1.64 KB

Versions: 185

Compression:

Stored size: 1.64 KB

Contents

const PATTERNS = {
  0: [' _ ',
    '| |',
    '|_|',
    '   '],
  1: ['   ',
    '  |',
    '  |',
    '   '],
  2: [' _ ',
    ' _|',
    '|_ ',
    '   '],
  3: [' _ ',
    ' _|',
    ' _|',
    '   '],
  4: ['   ',
    '|_|',
    '  |',
    '   '],
  5: [' _ ',
    '|_ ',
    ' _|',
    '   '],
  6: [' _ ',
    '|_ ',
    '|_|',
    '   '],
  7: [' _ ',
    '  |',
    '  |',
    '   '],
  8: [' _ ',
    '|_|',
    '|_|',
    '   '],
  9: [' _ ',
    '|_|',
    ' _|',
    '   '],
};
export default class Parser {
  convert(text) {
    return Parser.splitIntoRows(text).map(Parser.valuesInRow).join(',');
  }

  static valuesInRow(row) {
    return Parser.splitIntoDigits(row).map(Parser.getDigit).join('');
  }

  static splitIntoRows(text) {
    const rows = [];
    const lines = text.split('\n');
    for (let rowNumber = 0; rowNumber < lines.length; rowNumber += 4) {
      let row = '';
      for (let rowLine = 0; rowLine < 4; rowLine++) {
        row += `${lines[rowNumber + rowLine]}\n`;
      }
      rows.push(row.slice(0, -1));
    }
    return rows;
  }

  static splitIntoDigits(row) {
    const digits = [],
      rows = row.split('\n');
    for (let digitNumber = 0; digitNumber < rows[0].length; digitNumber += 3) {
      let digit = '';
      for (let rowNumber = 0; rowNumber < rows.length; rowNumber++) {
        digit += rows[rowNumber].substr(digitNumber, 3);
      }
      digits.push(digit);
    }
    return digits;
  }

  static getDigit(text) {
    for (const digit in PATTERNS) {
      if (PATTERNS.hasOwnProperty(digit)) {
        if (PATTERNS[digit].join('') === text) {
          return digit;
        }
      }
    }
    return '?';
  }

}

Version data entries

185 entries across 185 versions & 1 rubygems

Version Path
trackler-2.2.1.119 tracks/ecmascript/exercises/ocr-numbers/example.js
trackler-2.2.1.118 tracks/ecmascript/exercises/ocr-numbers/example.js
trackler-2.2.1.117 tracks/ecmascript/exercises/ocr-numbers/example.js
trackler-2.2.1.116 tracks/ecmascript/exercises/ocr-numbers/example.js
trackler-2.2.1.115 tracks/ecmascript/exercises/ocr-numbers/example.js
trackler-2.2.1.114 tracks/ecmascript/exercises/ocr-numbers/example.js
trackler-2.2.1.113 tracks/ecmascript/exercises/ocr-numbers/example.js
trackler-2.2.1.111 tracks/ecmascript/exercises/ocr-numbers/example.js
trackler-2.2.1.110 tracks/ecmascript/exercises/ocr-numbers/example.js
trackler-2.2.1.109 tracks/ecmascript/exercises/ocr-numbers/example.js
trackler-2.2.1.108 tracks/ecmascript/exercises/ocr-numbers/example.js
trackler-2.2.1.107 tracks/ecmascript/exercises/ocr-numbers/example.js
trackler-2.2.1.106 tracks/ecmascript/exercises/ocr-numbers/example.js
trackler-2.2.1.105 tracks/ecmascript/exercises/ocr-numbers/example.js
trackler-2.2.1.104 tracks/ecmascript/exercises/ocr-numbers/example.js
trackler-2.2.1.103 tracks/ecmascript/exercises/ocr-numbers/example.js
trackler-2.2.1.102 tracks/ecmascript/exercises/ocr-numbers/example.js
trackler-2.2.1.101 tracks/ecmascript/exercises/ocr-numbers/example.js
trackler-2.2.1.100 tracks/ecmascript/exercises/ocr-numbers/example.js
trackler-2.2.1.99 tracks/ecmascript/exercises/ocr-numbers/example.js