Skip to content

Commit af39c12

Browse files
author
luijar
committed
New errata fix
1 parent bdf207b commit af39c12

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

src/ch05/tests.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,15 @@ const validLength = (len, str) => str.length === len;
156156

157157
const find = R.curry((db, id) => db.find(id));
158158

159-
// checkLengthSsn :: String -> Either(String)
160-
const checkLengthSsn = ssn => {
161-
return Either.of(ssn)
162-
.filter(R.partial(validLength, [9]));
163-
};
164-
165159
// safeFindObject :: Store, string -> Either(Object)
166-
const safeFindObject = R.curry((db, id) => Either.fromNullable(find(db, id)));
160+
const safeFindObject = R.curry((db, id) => {
161+
const val = find(db, id);
162+
return val? Either.right(val) : Either.left(`Object not found with ID: ${id}`);
163+
});
164+
165+
// checkLengthSsn :: String -> Either(String)
166+
const checkLengthSsn = ssn =>
167+
validLength(9,ssn) ? Either.right(ssn): Either.left('invalid SSN');
167168

168169
// finStudent :: String -> Either(Student)
169170
const findStudent = safeFindObject(db);
@@ -245,14 +246,12 @@ QUnit.test("Complete showStudent program", function () {
245246
return info;
246247
});
247248

248-
const getOrElse = R.curry((message, container) => {
249-
return container.getOrElse(message);
250-
});
249+
const getOrElse = R.curry((message, container) => container.getOrElse(message));
251250

252251
const showStudent = R.compose(
253-
map(append('#some-element-id')),
252+
map(append('#student-info')),
254253
liftIO,
255-
getOrElse('unable to find a student'),
254+
getOrElse('unable to find student'),
256255
map(csv),
257256
map(R.props(['ssn', 'firstname', 'lastname'])),
258257
chain(findStudent),
@@ -262,6 +261,10 @@ QUnit.test("Complete showStudent program", function () {
262261

263262
let result = showStudent('444-44-4444').run();
264263
assert.equal(result, '444-44-4444,Alonzo,Church');
264+
265+
266+
let result2 = showStudent('xxx-xx-xxxx').run();
267+
assert.equal(result2, 'unable to find student');
265268
});
266269

267270

0 commit comments

Comments
 (0)