import path from "path" ; import ignore from "ignore" ; export function nonCircularClone < T > ( value ) { return JSON . parse ( JSON . stringify ( value ) ) ; } export function addAll < T > ( set , values ) { for ( const value of values ) set . add ( value ) ; } export function viewToArray ( view ) { return new Uint8Array ( view . buffer , view . byteOffset , view . byteLength ) ; } export function viewToBuffer ( view ) { return view . buffer . slice ( view . byteOffset , view . byteOffset + view . byteLength ) ; } export function base64Encode ( value ) { return Buffer . from ( value , "utf8" ) . toString ( "base64" ) ; } export function base64Decode ( encoded ) { return Buffer . from ( encoded , "base64" ) . toString ( "utf8" ) ; } export function randomHex ( digits = 8 ) { return Array . from ( Array ( digits ) ) . map ( ( ) => Math . floor ( Math . random ( ) * 16 ) . toString ( 16 ) ) . join ( "" ) ; } export interface Matcher { test ( string ) : boolean ; toString ( ) : string ; } export function globsToMatcher ( globs ? ) { const ign = ignore ( ) ; if ( globs ) ign . add ( globs ) ; return { test : ( string ) => ign . ignores ( string ) , toString : ( ) => globs ?. join ( ", " ) ?? "" , } ; } export function kebabCase ( s ) { return s . replace ( /[A-Z]/g , ( sub ) => `-${ sub . toLowerCase ( ) }` ) ; } export function spaceCase ( s ) { s = s . replace ( /(.)([A-Z][a-z]+)/g , "$1 $2" ) ; return s . replace ( /([a-z0-9])([A-Z])/g , "$1 $2" ) ; } export function titleCase ( s ) { return spaceCase ( s ) . split ( " " ) . map ( ( s ) => ( s ? s [ 0 ] . toUpperCase ( ) + s . substring ( 1 ) : s ) ) . join ( " " ) ; } const urlRegexp = /^([a-z]+:)?\/\//i ; export function resolveStoragePersist ( rootPath , persist ? ) { if ( typeof persist === "string" ) { if ( urlRegexp . test ( persist ) ) return persist ; return path . resolve ( rootPath , persist ) ; } return persist ; } const namespaceRegexp = /[/\\:|]/g ; const dotRegexp = /(^|\/|\\)(\.+)(\/|\\|$)/g ; const illegalRegexp = /[?<>*"'^\x00-\x1f\x80-\x9f]/g ; const windowsReservedRegexp = /^(con|prn|aux|nul|com[0-9]|lpt[0-9])(\..*)?$/i ; const leadingRegexp = /^[ /\\]+/ ; const trailingRegexp = /[ /\\]+$/ ; function dotReplacement ( match , g1 , g2 , g3 ) { return `${ g1 }${ "" . padStart ( g2 . length , "_" ) }${ g3 }` ; } function underscoreReplacement ( match ) { return "" . padStart ( match . length , "_" ) ; } export function sanitisePath ( unsafe ) { return unsafe . replace ( namespaceRegexp , path . sep ) . replace ( dotRegexp , dotReplacement ) . replace ( dotRegexp , dotReplacement ) . replace ( illegalRegexp , "_" ) . replace ( windowsReservedRegexp , "_" ) . replace ( leadingRegexp , underscoreReplacement ) . replace ( trailingRegexp , underscoreReplacement ) . substring ( 0 , 255 ) ; }