packageDocumentation module import { BooruError , sites } from './Constants' ; import { parse as xml2json } from 'fast-xml-parser' ; param { String } domain return { String ? } export function resolveSite ( domain ) { if ( typeof domain !== 'string' ) { return null ; } domain = domain . toLowerCase ( ) ; for ( const site in sites ) { if ( site === domain || sites [ site ] . domain === domain || sites [ site ] . aliases . includes ( domain ) ) { return site ; } } return null ; } interface XMLPage { html : any ; } interface XMLPosts { post ? : any [ ] ; tag ? : any | any [ ] ; } interface BooruXML { html ? : XMLPage ; '!doctype' ? : XMLPage ; posts : XMLPosts ; } private param { String } xml return { Object [ ] } export function jsonfy ( xml ) { if ( typeof xml === 'object' ) return xml ; const data = xml2json ( xml , { ignoreAttributes : false , attributeNamePrefix : '' , } ) as BooruXML ; if ( data . html || data [ '!doctype' ] ) { const page = data . html || data [ '!doctype' ] ?. html ; const message = [ ] ; if ( page . body . h1 ) { message . push ( page . body . h1 ) ; } if ( page . body . p ) { message . push ( page . body . p [ '#text' ] ) ; } throw new BooruError ( `The Booru sent back an error: '${ message . join ( ': ' ) }'` ) ; } if ( data . posts . post ) { return data . posts . post ; } if ( data . posts . tag ) { return Array . isArray ( data . posts . tag ) ? data . posts . tag : [ data . posts . tag ] ; } return [ ] ; } private param { Array } array return { Array } export function shuffle < T > ( array ) { let currentIndex = array . length ; let temporaryValue ; let randomIndex ; while ( currentIndex !== 0 ) { randomIndex = Math . floor ( Math . random ( ) * currentIndex ) ; currentIndex -= 1 ; temporaryValue = array [ currentIndex ] ; array [ currentIndex ] = array [ randomIndex ] ; array [ randomIndex ] = temporaryValue ; } return array ; } private param { Number } min param { Number } max export function randInt ( min , max ) { min = Math . ceil ( min ) ; max = Math . floor ( max ) ; return Math . floor ( Math . random ( ) * ( max - min + 1 ) ) + min ; } private param { String } site param { Number | String } limit export function validateSearchParams ( site , limit ) { const resolvedSite = resolveSite ( site ) ; if ( typeof limit !== 'number' ) { limit = parseInt ( limit , 10 ) ; } if ( resolvedSite === null ) { throw new BooruError ( 'Site not supported' ) ; } if ( typeof limit !== 'number' || Number . isNaN ( limit ) ) { throw new BooruError ( '`limit` should be an int' ) ; } return { site : resolvedSite , limit } ; } private param { String [ ] } arr1 param { String [ ] } arr2 return { String [ ] } export function compareArrays ( arr1 , arr2 ) { return arr1 . filter ( ( e1 ) => arr2 . some ( ( e2 ) => e1 . toLowerCase ( ) === e2 . toLowerCase ( ) ) ) ; }