import { IGender } from "../interfaces" ; import SeedRandom from "seedrandom" ; let RandomFunction = Math . random ; export let FantasyContentGeneratorSeed ; param { any [ ] } array export const pick = ( array ) => { return pickMany ( array , 1 ) [ 0 ] ; } ; param { any [ ] } array param { number } count export const pickMany = ( array , count = 1 ) => { const arrayCopy = Array . from ( array ) ; const pickedValues = [ ] ; for ( let i = 0 ; i < count ; i ++ ) { let pickedIndex = rand ( 0 , arrayCopy . length - 1 ) ; pickedValues . push ( arrayCopy [ pickedIndex ] ) ; arrayCopy . splice ( pickedIndex , 1 ) ; } return pickedValues ; } ; export const randomRace = ( ) => { } ; export const randomGender = ( ) : IGender => { return pick ( [ "male" , "female" ] ) ; } ; param { string } string export const parseTemplate = ( string , content = { } ) => { const regex = /{(.+?)}/gm ; const matches = string . match ( regex ) ; const linkedPlaceholderIndexes = { } ; if ( matches ) { matches . forEach ( ( match ) => { const linkedPlaceholderMatches = /{(.+?)::(.+?)}/gm . exec ( match ) ; if ( linkedPlaceholderMatches ) { const rawLinkToken = linkedPlaceholderMatches [ 1 ] ; if ( linkedPlaceholderIndexes [ rawLinkToken ] != null ) { let replacement = linkedPlaceholderMatches [ 2 ] . split ( "/" ) [ linkedPlaceholderIndexes [ rawLinkToken ] ] ; string = string . replace ( match , replacement ) ; } else { const allPlaceholderChunks = linkedPlaceholderMatches [ 2 ] . split ( "/" ) ; const newIndex = rand ( 0 , allPlaceholderChunks . length - 1 ) ; let replacement = allPlaceholderChunks [ newIndex ] ; linkedPlaceholderIndexes [ rawLinkToken ] = newIndex ; string = string . replace ( match , replacement ) ; } } } ) ; matches . forEach ( ( match ) => { if ( match . charAt ( 1 ) === "$" ) { const replacementVarName = match . substring ( 2 , match . length - 1 ) ; string = string . replace ( match , content [ replacementVarName ] ) ; } else { let replacement = pick ( match . substring ( 1 ) . substring ( 0 , match . length - 2 ) . split ( "/" ) ) ; string = string . replace ( match , replacement ) ; } } ) ; } return string ; } ; param { number } min param { number } max param { any } seed export const rand = ( min , max ) => { min = parseInt ( min ) ; max = parseInt ( max ) ; return Math . floor ( RandomFunction ( ) * ( max - min + 1 ) ) + min ; } ; param { number } number param { function } func export const forCount = ( number , func ) => { for ( let i = 0 ; i < number ; i ++ ) { func ( ) ; } } ; export const titleCase = ( string ) => string . replace ( /_/g , " " ) . replace ( /\w\S*/g , function ( txt ) { return txt . charAt ( 0 ) . toUpperCase ( ) + txt . substr ( 1 ) . toLowerCase ( ) ; } ) ; export const firstCharacterUppercase = ( string ) => string . charAt ( 0 ) . toUpperCase ( ) + string . slice ( 1 ) ; export const formatRace = ( race ) => { switch ( race ) { case "halfOrc" : return "Half-Orc" ; case "halfElf" : return "Half-Elf" ; default : return titleCase ( race ) ; } } ; export const generateUUID = ( ) => { var d = new Date ( ) . getTime ( ) ; if ( typeof performance !== "undefined" && typeof performance . now === "function" ) { d += performance . now ( ) ; } return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx" . replace ( /[xy]/g , function ( c ) { var r = ( d + Math . random ( ) * 16 ) % 16 | 0 ; d = Math . floor ( d / 16 ) ; return ( c === "x" ? r : ( r & 0x3 ) | 0x8 ) . toString ( 16 ) ; } ) ; } ; export const withSeed = ( seed , callback ) => { let isFirstPass = FantasyContentGeneratorSeed == null ; FantasyContentGeneratorSeed = seed ; RandomFunction = SeedRandom ( FantasyContentGeneratorSeed ) ; const returnValue = callback ( ) ; if ( isFirstPass ) { FantasyContentGeneratorSeed = null ; RandomFunction = Math . random ; } return returnValue ; } ;