2021-08-25 03:24:40 +02:00
const parseFullName = require ( './parseFullName' )
function parseName ( name ) {
var parts = parseFullName ( name )
var firstName = parts . first
if ( firstName && parts . middle ) firstName += ' ' + parts . middle
return {
first _name : firstName ,
last _name : parts . last
}
}
// Check if this name segment is of the format "Last, First" or "First Last"
// return true is "Last, First"
function checkIsALastName ( name ) {
if ( ! name . includes ( ' ' ) ) return true // No spaces must be a Last name
var parsed = parseFullName ( name )
if ( ! parsed . first ) return true // had spaces but not a first name i.e. "von Mises", must be last name only
return false
}
module . exports = ( author ) => {
if ( ! author ) return null
2021-10-31 20:59:39 +01:00
var splitAuthors = [ ]
// Example &LF: Friedman, Milton & Friedman, Rose
if ( author . includes ( '&' ) ) {
author . split ( '&' ) . forEach ( ( asa ) => splitAuthors = splitAuthors . concat ( asa . split ( ',' ) ) )
} else {
splitAuthors = author . split ( ',' )
}
if ( splitAuthors . length ) splitAuthors = splitAuthors . map ( a => a . trim ( ) )
2021-08-25 03:24:40 +02:00
var authors = [ ]
// 1 author FIRST LAST
2021-10-31 20:59:39 +01:00
if ( splitAuthors . length === 1 ) {
2021-08-25 03:24:40 +02:00
authors . push ( parseName ( author ) )
} else {
2021-10-31 20:59:39 +01:00
var firstChunkIsALastName = checkIsALastName ( splitAuthors [ 0 ] )
var isEvenNum = splitAuthors . length % 2 === 0
2021-08-25 03:24:40 +02:00
if ( ! isEvenNum && firstChunkIsALastName ) {
// console.error('Multi-author LAST,FIRST entry has a straggler (could be roman numerals or a suffix), ignore it', splitByComma[splitByComma.length - 1])
2021-10-31 20:59:39 +01:00
splitAuthors = splitAuthors . slice ( 0 , splitAuthors . length - 1 )
2021-08-25 03:24:40 +02:00
}
if ( firstChunkIsALastName ) {
2021-10-31 20:59:39 +01:00
var numAuthors = splitAuthors . length / 2
2021-08-25 03:24:40 +02:00
for ( let i = 0 ; i < numAuthors ; i ++ ) {
2021-10-31 20:59:39 +01:00
var last = splitAuthors . shift ( )
var first = splitAuthors . shift ( )
2021-08-25 03:24:40 +02:00
authors . push ( {
first _name : first ,
last _name : last
} )
}
} else {
2021-10-31 20:59:39 +01:00
splitAuthors . forEach ( ( segment ) => {
2021-08-25 03:24:40 +02:00
authors . push ( parseName ( segment ) )
} )
}
}
var firstLast = authors . length ? authors . map ( a => a . first _name ? ` ${ a . first _name } ${ a . last _name } ` : a . last _name ) . join ( ', ' ) : ''
var lastFirst = authors . length ? authors . map ( a => a . first _name ? ` ${ a . last _name } , ${ a . first _name } ` : a . last _name ) . join ( ', ' ) : ''
return {
authorFL : firstLast ,
authorLF : lastFirst ,
authorsParsed : authors
}
}