Sha256: c629d2394e0f19d8bec64df6060aef4be89bb8fc9f751eeb2dd814f4036f482b

Contents?: true

Size: 1.64 KB

Versions: 211

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 (let digit in PATTERNS) {
      if (PATTERNS.hasOwnProperty(digit)) {
        if (PATTERNS[digit].join('') === text) {
          return digit;
        }
      }
    }
    return '?';
  }

}

Version data entries

211 entries across 211 versions & 1 rubygems

Version Path
trackler-2.2.0.0 tracks/ecmascript/exercises/ocr-numbers/example.js
trackler-2.1.0.55 tracks/ecmascript/exercises/ocr-numbers/example.js
trackler-2.1.0.54 tracks/ecmascript/exercises/ocr-numbers/example.js
trackler-2.1.0.53 tracks/ecmascript/exercises/ocr-numbers/example.js
trackler-2.1.0.52 tracks/ecmascript/exercises/ocr-numbers/example.js
trackler-2.1.0.51 tracks/ecmascript/exercises/ocr-numbers/example.js
trackler-2.1.0.50 tracks/ecmascript/exercises/ocr-numbers/example.js
trackler-2.1.0.49 tracks/ecmascript/exercises/ocr-numbers/example.js
trackler-2.1.0.48 tracks/ecmascript/exercises/ocr-numbers/example.js
trackler-2.1.0.47 tracks/ecmascript/exercises/ocr-numbers/example.js
trackler-2.1.0.46 tracks/ecmascript/exercises/ocr-numbers/example.js
trackler-2.1.0.45 tracks/ecmascript/exercises/ocr-numbers/example.js
trackler-2.1.0.44 tracks/ecmascript/exercises/ocr-numbers/example.js
trackler-2.1.0.43 tracks/ecmascript/exercises/ocr-numbers/example.js
trackler-2.1.0.42 tracks/ecmascript/exercises/ocr-numbers/example.js
trackler-2.1.0.41 tracks/ecmascript/exercises/ocr-numbers/example.js
trackler-2.1.0.40 tracks/ecmascript/exercises/ocr-numbers/example.js
trackler-2.1.0.39 tracks/ecmascript/exercises/ocr-numbers/example.js
trackler-2.1.0.38 tracks/ecmascript/exercises/ocr-numbers/example.js
trackler-2.1.0.37 tracks/ecmascript/exercises/ocr-numbers/example.js