-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
205 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/** @type {import ('@jest/types').Config.InitialOptions} */ | ||
module.exports = { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
// }); | ||
|
||
}); |