@@ -156,14 +156,15 @@ const validLength = (len, str) => str.length === len;
156
156
157
157
const find = R . curry ( ( db , id ) => db . find ( id ) ) ;
158
158
159
- // checkLengthSsn :: String -> Either(String)
160
- const checkLengthSsn = ssn => {
161
- return Either . of ( ssn )
162
- . filter ( R . partial ( validLength , [ 9 ] ) ) ;
163
- } ;
164
-
165
159
// 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' ) ;
167
168
168
169
// finStudent :: String -> Either(Student)
169
170
const findStudent = safeFindObject ( db ) ;
@@ -245,14 +246,12 @@ QUnit.test("Complete showStudent program", function () {
245
246
return info ;
246
247
} ) ;
247
248
248
- const getOrElse = R . curry ( ( message , container ) => {
249
- return container . getOrElse ( message ) ;
250
- } ) ;
249
+ const getOrElse = R . curry ( ( message , container ) => container . getOrElse ( message ) ) ;
251
250
252
251
const showStudent = R . compose (
253
- map ( append ( '#some-element-id ' ) ) ,
252
+ map ( append ( '#student-info ' ) ) ,
254
253
liftIO ,
255
- getOrElse ( 'unable to find a student' ) ,
254
+ getOrElse ( 'unable to find student' ) ,
256
255
map ( csv ) ,
257
256
map ( R . props ( [ 'ssn' , 'firstname' , 'lastname' ] ) ) ,
258
257
chain ( findStudent ) ,
@@ -262,6 +261,10 @@ QUnit.test("Complete showStudent program", function () {
262
261
263
262
let result = showStudent ( '444-44-4444' ) . run ( ) ;
264
263
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' ) ;
265
268
} ) ;
266
269
267
270
0 commit comments