import http from 'http' ; export function uniq < T > ( data ) { return Array . from ( new Set ( data ) ) ; } export function countElement < T > ( arr , element ) { let num = 0 ; arr . forEach ( ( i ) => { if ( i === element ) num += 1 ; } ) ; return num ; } function isPortFree ( port ) { return new Promise ( ( resolve ) => { const server = http . createServer ( ) . listen ( port , ( ) => { server . close ( ) ; resolve ( true ) ; } ) . on ( 'error' , ( ) => { resolve ( false ) ; } ) ; } ) ; } export function timeout ( ms ) { return new Promise ( resolve => setTimeout ( resolve , ms ) ) ; } export async function tryPort ( start = 4000 ) { if ( await isPortFree ( start ) ) return start ; return tryPort ( start + 1 ) ; } export function getFileExt ( path ) { return path . slice ( path . lastIndexOf ( '.' ) + 1 ) . toLowerCase ( ) ; } export type Arrayable < T > = T | T [ ] ; export function toArray < T > ( v ) { if ( Array . isArray ( v ) ) return v ; return [ v ] ; } export function registerRegExpToJSON ( ) { const original = RegExp . prototype . toJSON ; RegExp . prototype . toJSON = function ( ) { return this . toString ( ) . slice ( 1 , - 1 ) ; } ; return ( ) => { RegExp . prototype . toJSON = original ; } ; }