Skip to content

Commit

Permalink
BacnkOCR pure ES6 version.
Browse files Browse the repository at this point in the history
  • Loading branch information
codecop committed Jan 21, 2025
1 parent 9e67afc commit 33b48f7
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 0 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/javascript.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Test JavaScript Jest

on:
push:
paths:
- 'javascript/**'
- '.github/workflows/javascript.yml'
pull_request:
paths:
- 'javascript/**'
- '.github/workflows/javascript.yml'

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Check out the repo
uses: actions/checkout@v2

- name: Set up Node.js
uses: actions/setup-node@v1
with:
node-version: 15.x

- name: Set up dependencies
working-directory: javascript
run: npm install

- name: Test
working-directory: javascript
run: npm test
11 changes: 11 additions & 0 deletions javascript/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.DS_Store
.idea

# Node
node_modules/
npm-debug.log
package-lock.json

dist/
coverage/
.nyc_output/
4 changes: 4 additions & 0 deletions javascript/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @type {import ('@jest/types').Config.InitialOptions} */
module.exports = {

}
12 changes: 12 additions & 0 deletions javascript/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "bank-ocr-refactoring-kata",
"version": "0.0.1",
"scripts": {
"test": "jest",
"test:coverage": "jest --coverage"
},
"devDependencies": {
"@types/jest": "^27.0.0",
"jest": "^27.0.0"
}
}
84 changes: 84 additions & 0 deletions javascript/src/ocr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
const NUMERALS = [
[' _ ',
'| | ',
'|_| ',
' '],
[' ',
' | ',
' | ',
' '],
[' _ ',
' _| ',
'|_ ',
' '],
[' _ ',
' _| ',
' _| ',
' '],
[' ',
'|_| ',
' | ',
' '],
[' _ ',
'|_ ',
' _| ',
' '],
[' _ ',
'|_ ',
'|_| ',
' '],
[' _ ',
' | ',
' | ',
' '],
[' _ ',
'|_| ',
'|_| ',
' '],
[' _ ',
'|_| ',
' _| ',
' ']];

class Ocr {

/**
* @param {string[]} lines
* @returns {string[]}
*/
static parse(lines) {
/** @type {string[]} */
const result = [];
for (let i = 0; i < lines.length; i += 4) {
let work = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '];
for (let pos = 0; pos < 9; ++pos) {
work[pos] = '?';
let got1 = false;
for (let numeral = 0; numeral <= 9; ++numeral) {
let ok = true;
for (let row = 0; row < 4; ++row) {
for (let col = 0; col < 4; ++col) {
if (NUMERALS[numeral][row][col] !== lines[i + row][4 * pos + col])
ok = false;
}
}
if (ok) {
work[pos] = String.fromCharCode(numeral + '0'.charCodeAt(0));
got1 = true;
break;
}
}
if (!got1) {
work[10] = 'I';
work[11] = work[12] = 'L';
}
}
result.push(work.join(''));
}
return result;
}
}

module.exports = {
Ocr
};
63 changes: 63 additions & 0 deletions javascript/tests/ocr.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const { Ocr } = require('../src/ocr');

describe('Ocr', () => {

it('parses_one_record_with_888888888', () => {
expect(Ocr.parse([' _ _ _ _ _ _ _ _ _ ',
'|_| |_| |_| |_| |_| |_| |_| |_| |_| ',
'|_| |_| |_| |_| |_| |_| |_| |_| |_| ',
' '])).
toEqual(['888888888 ']);
});

it('parses_two_records_with_888888888', () => {
expect(Ocr.parse([' _ _ _ _ _ _ _ _ _ ',
'|_| |_| |_| |_| |_| |_| |_| |_| |_| ',
'|_| |_| |_| |_| |_| |_| |_| |_| |_| ',
' ',
' _ _ _ _ _ _ _ _ _ ',
'|_| |_| |_| |_| |_| |_| |_| |_| |_| ',
'|_| |_| |_| |_| |_| |_| |_| |_| |_| ',
' '])).
toEqual(['888888888 ',
'888888888 ']);
});

it('parses_one_record_with_123456790', () => {
expect(Ocr.parse([' _ _ _ _ _ _ _ ',
' | _| _| |_| |_ |_ | |_| | | ',
' | |_ _| | _| |_| | _| |_| ',
' '])).
toEqual(['123456790 ']);
});

it('parses_two_records', () => {
expect(Ocr.parse([' _ _ _ _ _ _ _ ',
' | _| _| |_| |_ |_ | |_| | | ',
' | |_ _| | _| |_| | _| |_| ',
' ',
' _ _ _ _ _ _ _ ',
'|_| |_| | |_| | |_| _| _| _| ',
' _| | | |_| | | |_ |_ |_ ',
' '])).
toEqual(['123456790 ',
'947874222 ']);
});

it('parses_illegal_digit', () => {
expect(Ocr.parse([' _ _ _ _ _ _ _ ',
' | _| |_| _| |_ |_ | |_| | | ',
' | |_ _ | _| |_| | _| |_| ',
' '])).
toEqual(['12??56790 ILL']);
});

// it('checksum_fail_returns_ERR', () => {
// expect(Ocr.parse([' _ _ _ _ _ _ ',
// '|_ |_ |_| _| | | |_| |_| |_ ',
// '|_| |_| | _| | | | _| _| ',
// ' '])).
// toEqual(['664371495 ERR']);
// });

});

0 comments on commit 33b48f7

Please sign in to comment.