Skip to content

Commit 12044e2

Browse files
committed
Add BankAccount.parse static method
* Add BankAccount.matchDigit private static method
1 parent 2578f7d commit 12044e2

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

spec/bank_account_spec.js

+103
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,109 @@ describe('BankAccount', () => {
102102
})
103103
})
104104

105+
describe('.parse', () => {
106+
const testCases = [
107+
[
108+
'000000000',
109+
[
110+
' _ _ _ _ _ _ _ _ _ ',
111+
'| || || || || || || || || |',
112+
'|_||_||_||_||_||_||_||_||_|'
113+
]
114+
],
115+
[
116+
'111111111',
117+
[
118+
' ',
119+
' | | | | | | | | |',
120+
' | | | | | | | | |'
121+
]
122+
],
123+
[
124+
'222222222',
125+
[
126+
' _ _ _ _ _ _ _ _ _ ',
127+
' _| _| _| _| _| _| _| _| _|',
128+
'|_ |_ |_ |_ |_ |_ |_ |_ |_ '
129+
]
130+
],
131+
[
132+
'333333333',
133+
[
134+
' _ _ _ _ _ _ _ _ _ ',
135+
' _| _| _| _| _| _| _| _| _|',
136+
' _| _| _| _| _| _| _| _| _|'
137+
]
138+
],
139+
[
140+
'444444444',
141+
[
142+
' ',
143+
'|_||_||_||_||_||_||_||_||_|',
144+
' | | | | | | | | |'
145+
]
146+
],
147+
[
148+
'555555555',
149+
[
150+
' _ _ _ _ _ _ _ _ _ ',
151+
'|_ |_ |_ |_ |_ |_ |_ |_ |_ ',
152+
' _| _| _| _| _| _| _| _| _|'
153+
]
154+
],
155+
[
156+
'666666666',
157+
[
158+
' _ _ _ _ _ _ _ _ _ ',
159+
'|_ |_ |_ |_ |_ |_ |_ |_ |_ ',
160+
'|_||_||_||_||_||_||_||_||_|'
161+
]
162+
],
163+
[
164+
'777777777',
165+
[
166+
' _ _ _ _ _ _ _ _ _ ',
167+
' | | | | | | | | |',
168+
' | | | | | | | | |'
169+
]
170+
],
171+
[
172+
'888888888',
173+
[
174+
' _ _ _ _ _ _ _ _ _ ',
175+
'|_||_||_||_||_||_||_||_||_|',
176+
'|_||_||_||_||_||_||_||_||_|'
177+
]
178+
],
179+
[
180+
'999999999',
181+
[
182+
' _ _ _ _ _ _ _ _ _ ',
183+
'|_||_||_||_||_||_||_||_||_|',
184+
' _| _| _| _| _| _| _| _| _|'
185+
]
186+
],
187+
[
188+
'123456789',
189+
[
190+
' _ _ _ _ _ _ _ ',
191+
' | _| _||_||_ |_ ||_||_|',
192+
' ||_ _| | _||_| ||_| _|'
193+
]
194+
]
195+
]
196+
197+
testCases.forEach(([parsed, text]) => {
198+
describe(`when text is ${parsed}`, () => {
199+
const bankAccount = BankAccount.parse(text.join('\n'))
200+
201+
it('parses the expected bank account', () => {
202+
expect(bankAccount.format()).toBe(parsed)
203+
})
204+
})
205+
})
206+
})
207+
105208
describe('constructor', () => {
106209
const number = '345882865'
107210
const digits = ''

src/bank_account.js

+23
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,29 @@ class BankAccount {
4747
.join('\n')
4848
}
4949

50+
/* Parse the text into a Bank Account
51+
*
52+
* @param {string} text
53+
*
54+
* @returns {BankAccount}
55+
*/
56+
static parse(text) {
57+
const digits = this.split(text)
58+
const number = digits.map(digit => this.matchDigit(digit))
59+
return new this(number, digits)
60+
}
61+
62+
/* Match the digit exactly or return ? for unknown digits
63+
*
64+
* @param {string}
65+
*
66+
* @returns {string}
67+
* @private
68+
*/
69+
static matchDigit(digit) {
70+
return this.DIGITS.indexOf(digit)
71+
}
72+
5073
/* Initialize the Bank Account
5174
*
5275
* @param {(string|string[])} number

0 commit comments

Comments
 (0)