-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaccount.js
181 lines (163 loc) · 4.02 KB
/
account.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
'use strict';
function Account(number, rawAccountText) {
this.number = number;
this.rawAccountText = rawAccountText;
}
Account.RAW_TO_VALUE = {};
Account.RAW_TO_VALUE[
" _ " +
"| |" +
"|_|"
] = "0";
Account.RAW_TO_VALUE[
" " +
" |" +
" |"
] = "1";
Account.RAW_TO_VALUE[
" _ " +
" _|" +
"|_ "
] = "2";
Account.RAW_TO_VALUE[
" _ " +
" _|" +
" _|"
] = "3";
Account.RAW_TO_VALUE[
" " +
"|_|" +
" |"
] = "4";
Account.RAW_TO_VALUE[
" _ " +
"|_ " +
" _|"
] = "5";
Account.RAW_TO_VALUE[
" _ " +
"|_ " +
"|_|"
] = "6";
Account.RAW_TO_VALUE[
" _ " +
" |" +
" |"
] = "7";
Account.RAW_TO_VALUE[
" _ " +
"|_|" +
"|_|"
] = "8";
Account.RAW_TO_VALUE[
" _ " +
"|_|" +
" _|"
] = "9";
Account.VALUE_TO_RAW = {}
Object.keys(Account.RAW_TO_VALUE).forEach(
function(raw) {
Account.VALUE_TO_RAW[Account.RAW_TO_VALUE[raw]] = raw;
}
);
var RAW_CHARACTER_WIDTH = 3;
Account.parse = function(rawAccountText){
var parsedAccount = "";
for (var digitPlace = 0; digitPlace < 9; digitPlace++) {
parsedAccount += Account.RAW_TO_VALUE[extractRawDigit(digitPlace, rawAccountText)] || "?"
}
return new Account(parsedAccount, rawAccountText);
}
/*
Given a raw digit, it returns the possible alternates which are only off by
one stroke.
*/
Account.alternateDigits = function(incommingRawDigit) {
var alternates = [];
Object.keys(Account.RAW_TO_VALUE).forEach(function(rawDigit) {
var diffCount = 0;
for(var i = 0; i < rawDigit.length; i++) {
if(incommingRawDigit.substr(i, 1) != rawDigit.substr(i, 1)) {
diffCount++;
}
if(diffCount > 1) {
break;
}
}
if(diffCount === 1) {
alternates.push(Account.RAW_TO_VALUE[rawDigit]);
}
});
return alternates;
}
/*
Returns true if all numbers in the account are readable.
*/
Account.prototype.isLegible = function() {
return this.number.indexOf("?") < 0;
}
/*
Determines if the account number is valid based off of a simple checksum.
*/
Account.prototype.isValid = function() {
if(!this.isLegible()) { return false; }
var sum = this.number.split('').reduce(
function(previous,current,index) {
return (9 - index) * current + previous;
}, 0);
return (sum % 11) === 0;
}
/*
Returns a formatted string version of the account. The suffixes "ILL" and
"ERR" are added to illegible and invalid accounts respectively.
*/
Account.prototype.format = function() {
var suffix = "";
if(!this.isLegible()) {
suffix = " ILL";
} else if(!this.isValid()) {
suffix = " ERR";
}
return this.number + suffix;
}
/*
Returns the original raw OCR'ed digit for the zero based position.
*/
Account.prototype.rawDigit = function(position) {
return extractRawDigit(position, this.rawAccountText);
}
/*
For invalid or illegible accounts, this calculates possible alternative account numbers.
*/
Account.prototype.alternates = function() {
var alternates = [];
if(this.isValid()) { return alternates; }
for(var currentPos = 0; currentPos <= 9; currentPos++) {
var altDigits = Account.alternateDigits(extractRawDigit(currentPos, this.rawAccountText));
altDigits.forEach(function (altDigit) {
var accountNum = this.number.substr(0, currentPos) + altDigit + this.number.substr(currentPos + 1);
var possibleAccount = new Account(accountNum);
if(possibleAccount.isValid()) {
alternates.push(possibleAccount.number);
}
}, this);
}
return alternates;
}
function extractRawDigit(position, accountText) {
var accountLines = accountText.split("\n");
var extractedRawDigit = "";
[0,1,2].forEach(function(lineNum) {
var start = position * RAW_CHARACTER_WIDTH;
var end = (position + 1) * RAW_CHARACTER_WIDTH;
extractedRawDigit += accountLines[lineNum].slice(start, end);
});
return extractedRawDigit;
};
/*
Returns a string of a raw digit string with newlines for pretty printing.
*/
function prettyRawDigit(rawDigit) {
return [rawDigit.slice(0, 3), rawDigit.slice(3, 6), rawDigit.slice(6, 9)].join("\n");
}
module.exports = Account;