Sha256: 8abfc2e569288bc92bd1d56841cd6c04cec602a0ee3f2ee7173df4e229fdd380

Contents?: true

Size: 1.58 KB

Versions: 141

Compression:

Stored size: 1.58 KB

Contents

'use strict';

var PATTERNS = {
  0: [' _ ',
    '| |',
    '|_|',
    '   '],
  1: ['   ',
    '  |',
    '  |',
    '   '],
  2: [' _ ',
    ' _|',
    '|_ ',
    '   '],
  3: [' _ ',
    ' _|',
    ' _|',
    '   '],
  4: ['   ',
    '|_|',
    '  |',
    '   '],
  5: [' _ ',
    '|_ ',
    ' _|',
    '   '],
  6: [' _ ',
    '|_ ',
    '|_|',
    '   '],
  7: [' _ ',
    '  |',
    '  |',
    '   '],
  8: [' _ ',
    '|_|',
    '|_|',
    '   '],
  9: [' _ ',
    '|_|',
    ' _|',
    '   ']
};

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

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

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

function valuesInRow(row) {
  var digits = splitIntoDigits(row);
  return digits.map(getDigit).join('');
}

exports.convert = function (text) {
  var rows = splitIntoRows(text);
  return rows.map(valuesInRow).join(',');
};

Version data entries

141 entries across 141 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/javascript/exercises/ocr-numbers/example.js
trackler-2.2.1.179 tracks/javascript/exercises/ocr-numbers/example.js
trackler-2.2.1.178 tracks/javascript/exercises/ocr-numbers/example.js
trackler-2.2.1.177 tracks/javascript/exercises/ocr-numbers/example.js
trackler-2.2.1.176 tracks/javascript/exercises/ocr-numbers/example.js
trackler-2.2.1.175 tracks/javascript/exercises/ocr-numbers/example.js
trackler-2.2.1.174 tracks/javascript/exercises/ocr-numbers/example.js
trackler-2.2.1.173 tracks/javascript/exercises/ocr-numbers/example.js
trackler-2.2.1.172 tracks/javascript/exercises/ocr-numbers/example.js
trackler-2.2.1.171 tracks/javascript/exercises/ocr-numbers/example.js
trackler-2.2.1.170 tracks/javascript/exercises/ocr-numbers/example.js
trackler-2.2.1.169 tracks/javascript/exercises/ocr-numbers/example.js
trackler-2.2.1.167 tracks/javascript/exercises/ocr-numbers/example.js
trackler-2.2.1.166 tracks/javascript/exercises/ocr-numbers/example.js
trackler-2.2.1.165 tracks/javascript/exercises/ocr-numbers/example.js
trackler-2.2.1.164 tracks/javascript/exercises/ocr-numbers/example.js
trackler-2.2.1.163 tracks/javascript/exercises/ocr-numbers/example.js
trackler-2.2.1.162 tracks/javascript/exercises/ocr-numbers/example.js
trackler-2.2.1.161 tracks/javascript/exercises/ocr-numbers/example.js
trackler-2.2.1.160 tracks/javascript/exercises/ocr-numbers/example.js