Skip to content

Commit bcb629c

Browse files
fixup the month conversion tool
1 parent 76eeb21 commit bcb629c

File tree

2 files changed

+31
-29
lines changed

2 files changed

+31
-29
lines changed

src/components/Publications/Entry/Periodical.jsx

+17-28
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,23 @@ import { asMonth, quoteTitle } from '../../../utils'
22
import Authors from './Authors'
33
import PropTypes from 'prop-types'
44

5-
const Periodical = ({ entry }) => {
6-
const title = quoteTitle(entry.title, [
7-
entry.journal,
8-
entry.volume,
9-
entry.number,
10-
entry.pages,
11-
entry.month,
12-
entry.year,
13-
])
14-
const journal = entry.journal ? <i>{entry.journal}, </i> : ''
15-
const volume = entry.volume ? `vol. ${entry.volume}, ` : ''
16-
const number = entry.number ? `no. ${entry.number}, ` : ''
17-
const pages = entry.pages ? `pp. ${entry.pages}, ` : ''
18-
const month = entry.month ? '' : '' //`${asMonth(entry.month)} ` : ''
19-
const year = entry.year ? entry.year : ''
20-
const stop = entry.journal || entry.volume || entry.number || entry.pages || entry.month || entry.year ? '.' : ''
21-
return (
22-
<>
23-
<Authors>{entry.author}</Authors>, {title} {journal}
24-
{volume}
25-
{number}
26-
{pages}
27-
{month}
28-
{year}
29-
{stop}
30-
</>
31-
)
32-
}
5+
const Periodical = ({ entry }) => (
6+
<>
7+
<Authors>{entry.author}</Authors>,{' '}
8+
{quoteTitle(entry.title, [entry.journal, entry.volume, entry.number, entry.pages, entry.month, entry.year])}{' '}
9+
{entry.journal && (
10+
<>
11+
<i>{entry.journal}</i>,{' '}
12+
</>
13+
)}
14+
{entry.volume && <>vol. {entry.volume}, </>}
15+
{entry.number && <>no. {entry.number}, </>}
16+
{entry.pages && <>pp. {entry.pages}, </>}
17+
{entry.month && <>{asMonth(entry.month)} </>}
18+
{entry.year && <>{entry.year}</>}
19+
{(entry.journal || entry.volume || entry.number || entry.pages || entry.month || entry.year) && <>.</>}
20+
</>
21+
)
3322

3423
Periodical.propTypes = {
3524
entry: PropTypes.shape({

src/utils/asMonth.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1+
const months = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec']
2+
3+
/**
4+
* Little utility to parse the bibtex month to how we want to display it. It could have been a simpler map, but this
5+
* lets us use `Intl.DateFormat` for more future control on how it should be displayed
6+
*
7+
* @param {string} bibMonth
8+
* @returns {string}
9+
*/
110
const asMonth = (bibMonth) => {
2-
return Intl.DateTimeFormat('en', { month: 'long' }).format(new Date(`1/${bibMonth}/2000`))
11+
const monthIdx = months.indexOf(bibMonth)
12+
if (monthIdx < 0) {
13+
throw new Error(`Month value '${bibMonth}' could not be converted to a month`)
14+
}
15+
return Intl.DateTimeFormat('en', { month: 'long' }).format(new Date(2000, monthIdx, 1))
316
}
417

518
export default asMonth

0 commit comments

Comments
 (0)