* Parse the Open Library dimensions format into a proper object. * * @param {string} dimensionsStr The Open Library dimensions. * @returns A object with width and height if valid.
(dimensions)
| 12 | * @returns A object with width and height if valid. |
| 13 | */ |
| 14 | function parseDimensions(dimensions) { |
| 15 | if (!dimensions) { |
| 16 | return null; |
| 17 | } |
| 18 | |
| 19 | const values = dimensions |
| 20 | .replace(/ (centimeters|inches)$/, '') |
| 21 | .split(' x ') |
| 22 | .map(parseFloat) |
| 23 | .filter((dm) => !Number.isNaN(dm)); |
| 24 | |
| 25 | if (values.length !== 3) { |
| 26 | return null; |
| 27 | } |
| 28 | |
| 29 | return { |
| 30 | width: values[1], |
| 31 | height: values[0], |
| 32 | unit: dimensions.includes('centimeters') ? 'CENTIMETER' : 'INCH', |
| 33 | }; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Parse the Open Library publish date into a proper year number. |
no outgoing calls
no test coverage detected