import { validateFullCitation , validateCitationPart , validateCitationID } from './util/validator' ; import { strictCitekeyValidatorRE , looseCitekeyValidatorRE } from './util/regex' ; import { extractLocator } from './util/retrieve-locator' ; import { extractCitations } from './util/extract-citations' ; /**
   * Expose validateCitationID and extractCitations to the outside.
   *
   */ export const util = { 'validateCitationID' : validateCitationID , 'extractCitations' : extractCitations } ; interface interface Citation { prefix : string ; suffix : string ; id : string ; locator : string ; label : string ; 'suppress-author' : boolean ; } param { string } citation param { Boolean } strict returns { Citation [ ] } memberof export function parseSingle ( citation , strict = false ) { if ( validateCitationID ( citation , strict ) && citation [ 0 ] === '@' ) { return [ { prefix : '' , suffix : '' , id : citation . substr ( 1 ) , locator : '' , label : 'page' , 'suppress-author' : false } ] ; } if ( ! validateFullCitation ( citation ) ) throw new Error ( `Invalid Key - Invalid citation passed: ${ citation }.` ) ; let returnCitations = [ ] ; let _citation = citation . substr ( 1 , citation . length - 2 ) . split ( ';' ) ; let invalidPrefixes = [ ] ; for ( let c of _citation ) { if ( c === '' ) continue ; if ( ! validateCitationPart ( c ) ) { invalidPrefixes . push ( c ) ; continue ; } let prefix = '' ; if ( invalidPrefixes . length === 1 ) { prefix = invalidPrefixes + ';' ; } else if ( invalidPrefixes . length > 1 ) { prefix = invalidPrefixes . join ( ';' ) ; } prefix += c . split ( '@' ) [ 0 ] ; prefix = prefix . trim ( ) ; invalidPrefixes = [ ] ; let suppressAuthor = c . indexOf ( '@' ) > 0 && c [ c . indexOf ( '@' ) - 1 ] === '-' ; if ( suppressAuthor ) prefix = prefix . substr ( 0 , prefix . length - 1 ) . trim ( ) ; let commaIndex = c . split ( '@' ) [ 1 ] . indexOf ( ',' ) + 1 ; if ( commaIndex === 0 ) commaIndex = c . split ( '@' ) [ 1 ] . indexOf ( ' ' ) + 1 ; if ( commaIndex <= 0 ) commaIndex = undefined ; let citationKeyPart = c . substr ( c . indexOf ( '@' ) , commaIndex ) ; let extractedKey = null ; if ( strict ) { extractedKey = strictCitekeyValidatorRE . exec ( citationKeyPart ) ; } else { extractedKey = looseCitekeyValidatorRE . exec ( citationKeyPart ) ; } if ( extractedKey === null ) throw new Error ( `Invalid Key - Invalid citation passed: ${ c }` ) ; let citeKey = extractedKey [ 1 ] ; let afterKey = c . split ( '@' ) [ 1 ] . substr ( extractedKey [ 1 ] . length ) . trim ( ) ; let { suffix , locator , label } = extractLocator ( afterKey ) ; returnCitations . push ( { prefix : prefix , suffix : suffix , id : citeKey , locator : locator , label : label , 'suppress-author' : suppressAuthor } ) ; } if ( returnCitations . length === 0 && _citation . length > 0 ) { throw new Error ( `Invalid citation passed: ${ citation }` ) ; } return returnCitations ; } export param { Citation [ ] } citationArray returns { string } export function makeCitation ( citationArray ) { if ( ! Array . isArray ( citationArray ) ) citationArray = [ citationArray ] ; let returnArray = [ ] ; for ( let csl of citationArray ) { let res = '' ; if ( ! csl . hasOwnProperty ( 'id' ) ) throw new Error ( 'Citation had no ID given!' ) ; if ( csl . hasOwnProperty ( 'prefix' ) ) res += csl . prefix + ' ' ; if ( csl . hasOwnProperty ( 'suppress-author' ) && csl [ 'suppress-author' ] ) res += '-' ; res += '@' + csl . id ; if ( csl . hasOwnProperty ( 'label' ) && csl . hasOwnProperty ( 'locator' ) ) res += ', ' + csl . label + ' ' + csl . locator ; if ( csl . hasOwnProperty ( 'suffix' ) ) res += ' ' + csl . suffix ; returnArray . push ( res . trim ( ) ) ; } return `[${ returnArray . join ( '; ' ) }]` ; }