import { ParseError } from "./errors" ; import * as Events from "events" ; param maybeInt returns throws export const parseInteger = ( maybeInt ) : number => { const [ radix , toParse ] = ( ( ) => { const toReturn = getRadix ( maybeInt ) ; if ( isNullOrUndefined ( toReturn ) ) { throw new ParseError ( "Celery.Utility.parseInteger: could not " + `parse ${ maybeInt } as integer` ) ; } return toReturn ; } ) ( ) ; const maybeParsed = Number . parseInt ( toParse , radix ) ; return maybeParsed ; } ; param maybeBoolean returns throws export const parseBoolean = ( maybeBoolean ) : boolean => { switch ( maybeBoolean . toLowerCase ( ) . trim ( ) ) { case "true" : case "1" : case "on" : case "yes" : return true ; case "false" : case "0" : case "off" : case "no" : return false ; } throw new ParseError ( "Celery.Utility.parseBoolean: could not parse " + `${ maybeBoolean } as a boolean` ) ; } ; param value returns export const isNullOrUndefined = < T > ( value ) : value is null | undefined => isNull ( value ) || isUndefined ( value ) ; param value returns export const isNull = < T > ( value ) : value is null => value === null ; param value returns export const isUndefined = < T > ( value ) : value is undefined => typeof value === "undefined" ; param toConvert returns export const toCamelCase = ( toConvert ) : string => toConvert . replace ( /_([a-z])/ , ( _ , match ) => match . toUpperCase ( ) ) ; param emitter param name returns export const promisifyEvent = async < T > ( emitter , name ) : Promise < T > => new Promise < T > ( ( resolve ) => emitter . once ( name , resolve ) ) ; param emitter param filterMap param name returns export const filterMapEvent = async < T > ( { emitter , filterMap , name } ) : Promise < T > => new Promise < T > ( ( resolve ) => { let resolved = false ; const onEvent = ( ... values ) => { if ( resolved ) { return ; } const maybeMapped = filterMap ( ... values ) ; if ( ! isNullOrUndefined ( maybeMapped ) ) { emitter . removeListener ( name , onEvent ) ; resolve ( maybeMapped ) ; resolved = true ; } } ; emitter . addListener ( name , onEvent ) ; } ) ; param timeout param promise returns see createTimerPromise export const createTimeoutPromise = async < T > ( promise , timeout ? ) : Promise < T > => { if ( isNullOrUndefined ( timeout ) ) { return promise ; } return Promise . race ( [ promise , createTimerPromise ( timeout ) ] ) ; } ; param timeout returns export const createTimerPromise = async ( timeout ) : Promise < never > => new Promise < never > ( ( _ , reject ) => setTimeout ( ( ) => reject ( new Error ( "timed out" ) ) , timeout ) ) ; /**
 * Set of valid numeric bases accepted by `parseInteger`.
 */ type Radix = 2 | 8 | 10 | 16 ; param maybeNumber returns const getRadix = ( maybeNumber ) : [ Radix , string ] | undefined => { const REGEX = /^(?:(0[0-7]*)|(?:0x([\da-f]+))|(?:0b([01]+))|([1-9][\d]*))$/ ; const OCTAL_INDEX = 1 ; const HEX_INDEX = 2 ; const BINARY_INDEX = 3 ; const DECIMAL_INDEX = 4 ; const trimmedLowered = maybeNumber . toLowerCase ( ) . trim ( ) ; const maybeMatches = REGEX . exec ( trimmedLowered ) ; if ( isNullOrUndefined ( maybeMatches ) ) { return undefined ; } const matches = maybeMatches ; if ( ! isNullOrUndefined ( matches [ OCTAL_INDEX ] ) ) { return [ 8 , matches [ OCTAL_INDEX ] ] ; } else if ( ! isNullOrUndefined ( matches [ HEX_INDEX ] ) ) { return [ 16 , matches [ HEX_INDEX ] ] ; } else if ( ! isNullOrUndefined ( matches [ BINARY_INDEX ] ) ) { return [ 2 , matches [ BINARY_INDEX ] ] ; } return [ 10 , matches [ DECIMAL_INDEX ] ] ; } ;