export function test ( ) { return "Successfully called test function." ; } export function empty ( ) { } export function identityString ( name ) { return name ; } export function identityNum ( n ) { return n ; } export function identityBool ( b ) { return b ; } export function identityUndefined ( u ) { return u ; } export function identityNull ( n ) { return n ; } export function identityObject ( o ) { return o ; } export function identityArrayNum ( n ) { return n ; } export function identityArrayString ( s ) { return s ; } export function identityInt8 ( a ) { return a ; } export function identityUint8 ( a ) { return a ; } export function identityUint8Clamped ( a ) { return a ; } export function identityInt16 ( a ) { return a ; } export function identityUint16 ( a ) { return a ; } export function identityInt32 ( a ) { return a ; } export function identityUint32 ( a ) { return a ; } export function identityFloat32 ( a ) { return a ; } export function identityFloat64 ( a ) { return a ; } export function identityBigInt64 ( a ) { return a ; } export function identityBigUint64 ( a ) { return a ; } export function identityMap ( m ) { return m ; } export function identitySet ( s ) { return s ; } export const arrow = ( str ) => str ; export const asyncArrow = async ( str ) => str ; export function hello ( name ) { return `Hello ${ name }!` ; } export function fact ( n ) { return n <= 1 ? 1 : n * fact ( n - 1 ) ; } export function concat ( a , b ) { return a + b ; } export function error ( a ) { throw new Error ( `Expected error. Arg: ${ a }` ) ; } export function noargs ( ) { return "called function with no args." ; } export function sleep ( ms ) { return new Promise < void > ( resolve => setTimeout ( resolve , ms ) ) ; } export function delayReject ( ms ) { return new Promise ( ( _ , reject ) => setTimeout ( reject , ms ) ) ; } export async function async ( ) { await sleep ( 200 ) ; return "async function: success" ; } export function path ( ) { return sleep ( 200 ) . then ( ( ) => process . env . PATH || "no PATH variable" ) ; } export function emptyReject ( ) { return Promise . reject ( ) ; } export function rejected ( ) { return Promise . reject ( "intentionally rejected" ) ; } export interface Timing { start : number ; end : number ; } export async function timer ( delayMs ) { const start = Date . now ( ) ; await sleep ( delayMs ) ; const end = Date . now ( ) ; return { start , end } ; } export function spin ( ms ) { const start = Date . now ( ) ; while ( true ) { if ( Date . now ( ) - start >= ms ) { break ; } } const end = Date . now ( ) ; return { start , end } ; } export function infiniteLoop ( ) { while ( true ) { } } export function optionalArg ( arg ? ) { return arg ? arg : "No arg" ; } export function consoleLog ( str ) { console . log ( str ) ; } export function consoleWarn ( str ) { console . warn ( str ) ; } export function consoleError ( str ) { console . error ( str ) ; } export function consoleInfo ( str ) { console . info ( str ) ; } export function processExit ( code ? ) { process . exit ( code ) ; } class CustomError extends Error { constructor ( message , public custom ) { super ( message ) ; } } export function customError ( ) { throw new CustomError ( "custom error message" , "custom value" ) ; } export function returnsError ( ) { return new Error ( "a returned non-thrown error" ) ; } export async function allocate ( bytes ) { const array = new Array ( bytes / 8 ) ; const elems = array . length ; for ( let i = 0 ; i < elems ; i ++ ) { array [ i ] = i ; } console . log ( `allocated: %O` , { bytes , elems } ) ; console . log ( `post allocate memory usage: %O` , process . memoryUsage ( ) ) ; await sleep ( 1000 ) ; console . log ( `Returning from allocate` ) ; return { bytes , elems } ; } export function getEnv ( key ) { return process . env [ key ] ; } export interface MonteCarloReturn { inside : number ; samples : number ; } export function monteCarloPI ( samples ) { let inside = 0 ; for ( let n = 0 ; n < samples ; n ++ ) { const [ x , y ] = [ Math . random ( ) , Math . random ( ) ] ; if ( x ** 2 + y ** 2 <= 1 ) { inside ++ ; } } return { inside , samples } ; } export function * generator ( args ) { yield * args ; } export async function * asyncGenerator ( args ) { yield * args ; } export async function * generateThenInfiniteLoop ( arg ) { yield arg ; while ( true ) { } } export async function * asyncGeneratorDelay ( args , delay ) { for ( const arg of args ) { await sleep ( delay ) ; yield arg ; } } remarks export function promiseArg ( promise ) { return promise ; } export function identityFunction ( fn ) { return fn ; } export function functionReturn ( ) { return ( ) => { console . log ( "returned a function" ) ; } ; } export function identityBuffer ( buf ) { return buf ; } export function identityDate ( arg ) { return arg ; } export class Cls { constructor ( ) { } } export function identityClass ( arg ) { return arg ; } export function classReturn ( ) { return new Cls ( ) ; }