###Formatted and commented
w => // main function: takes width 'w' as input, returns 'g'
g = ( // g = recursive function with:
b, // - b = number of books
s = `|${'-'.repeat(w * 3)}|\n`, // - s = top/bottom of shell, filled with '-'
r = s.replace( // - r = pattern of the current row of books
RegExp('---', 'g'), // ('b' is updated while building it)
_ => b && b-- ? '|X|' : ' ' // NB: 'r' must be defined in the scope of 'g',
) // otherwise it would be overwritten by
) => // subsequent calls
( //
b ? // if there are remaining books:
g(b) + s // do a recursive call and append shell top
: // else
s // just append shell top
) + r + r + r + s // append book rows and shell bottom
###Test cases