Skip to content

Modules Common Library / Utilities

Utilities ​

Common utility functions that can be used across bundles and tabs

Functions ​

assertFunctionOfLength() ​

Call Signature ​

ts
function assertFunctionOfLength<T>(
   f, 
   l, 
   func_name, 
   type_name?, 
   param_name?
): asserts f is T;

Defined in: lib/modules-lib/node_modules/js-slang/dist/utils/rttc.d.ts:97

Assertion version of isFunctionOfLength

Type Parameters ​
Type Parameter

T extends (...args) => any

Parameters ​
ParameterTypeDescription

f

(...args) => any

Value to validate

l

Parameters<T>["length"]

Number of parameters that f is expected to have

func_name

string

Function within which the validation is occurring

type_name?

string

Optional alias for the function type

param_name?

string

Name of the parameter that's being validated

Returns ​

asserts f is T

Call Signature ​

ts
function assertFunctionOfLength<T>(
   f, 
   l, 
   func_name, 
   type_name?, 
   param_name?
): asserts f is FunctionOfLength<T>;

Defined in: lib/modules-lib/node_modules/js-slang/dist/utils/rttc.d.ts:98

Assertion version of isFunctionOfLength

Type Parameters ​
Type Parameter

T extends number

Parameters ​
ParameterTypeDescription

f

unknown

Value to validate

l

T

Number of parameters that f is expected to have

func_name

string

Function within which the validation is occurring

type_name?

string

Optional alias for the function type

param_name?

string

Name of the parameter that's being validated

Returns ​

asserts f is FunctionOfLength<T>


assertNumberWithinRange() ​

Call Signature ​

ts
function assertNumberWithinRange(
   value, 
   func_name, 
   min?, 
   max?, 
   integer?, 
   param_name?
): asserts value is number;

Defined in: lib/modules-lib/node_modules/js-slang/dist/utils/rttc.d.ts:127

Assertion version of isNumberWithinRange

Parameters ​
ParameterType

value

unknown

func_name

string

min?

number

max?

number

integer?

boolean

param_name?

string

Returns ​

asserts value is number

Call Signature ​

ts
function assertNumberWithinRange(value, options): asserts value is number;

Defined in: lib/modules-lib/node_modules/js-slang/dist/utils/rttc.d.ts:128

Assertion version of isNumberWithinRange

Parameters ​
ParameterType

value

unknown

options

AssertNumberWithinRangeOptions

Returns ​

asserts value is number


assertTupleOfLength() ​

Call Signature ​

ts
function assertTupleOfLength<T, U>(
   obj, 
   l, 
   func_name, 
   param_name?
): asserts obj is TupleOfLength<T, U>;

Defined in: lib/modules-lib/node_modules/js-slang/dist/utils/rttc.d.ts:110

Assertion version of isTupleOfLength

Type Parameters ​
Type Parameter

T extends number

U

Parameters ​
ParameterType

obj

U[]

l

T

func_name

string

param_name?

string

Returns ​

asserts obj is TupleOfLength<T, U>

Call Signature ​

ts
function assertTupleOfLength<T>(
   obj, 
   l, 
   func_name, 
   param_name?
): asserts obj is TupleOfLength<T>;

Defined in: lib/modules-lib/node_modules/js-slang/dist/utils/rttc.d.ts:111

Assertion version of isTupleOfLength

Type Parameters ​
Type Parameter

T extends number

Parameters ​
ParameterType

obj

unknown

l

T

func_name

string

param_name?

string

Returns ​

asserts obj is TupleOfLength<T>


callIfFuncAndRightArgs() ​

ts
function callIfFuncAndRightArgs(
   f, 
   line, 
   column, 
   source, 
   nativeStorage, 
   ...args
): any;

Defined in: lib/modules-lib/node_modules/js-slang/dist/utils/operators.d.ts:34

Calls the provided value as if it were a function being called from the line and column within the provided source file, checking for argument count.

  • If nativeStorage is provided, then infinite recursion protection is also added.

Parameters ​

ParameterType

f

unknown

line

number

column

number

source

string | null

nativeStorage

NativeStorage | undefined

...args

any[]

Returns ​

any


callWithoutMetadata() ​

ts
function callWithoutMetadata<T>(f, ...args): ReturnType<T>;

Defined in: lib/modules-lib/node_modules/js-slang/dist/utils/operators.d.ts:39

Convenience wrapper for callIfFuncAndRightArgs that doesn't require any extra metadata to be passed into the function.

Type Parameters ​

Type Parameter

T extends (...args) => any

Parameters ​

ParameterType

f

T

...args

Parameters<T>

Returns ​

ReturnType<T>


degreesToRadians() ​

ts
function degreesToRadians(degrees): number;

Defined in: lib/modules-lib/src/utilities.ts:15

Converts an angle in degrees into radians

Parameters ​

ParameterTypeDescription

degrees

number

Angle in degrees

Returns ​

number

Angle in radians


hexToColor() ​

ts
function hexToColor(hex, func_name?): [number, number, number];

Defined in: lib/modules-lib/src/utilities.ts:33

Converts a color hex value string to its red, green and blue components, each normalized to between 0 and 1. The leading # is optional.

Parameters ​

ParameterType

hex

string

func_name?

string

Returns ​

[number, number, number]

Tuple of three numbers representing the R, G and B components


hueToRgb() ​

ts
function hueToRgb(hue): [number, number, number];

Defined in: lib/modules-lib/src/utilities.ts:60

Converts a hue value in [0,1) into an RGB triple in [0,255].

Parameters ​

ParameterTypeDescription

hue

number

hue around the colour wheel, where 0 and 1 both map to red

Returns ​

[number, number, number]

[r, g, b] values between 0 and 255


isFunctionOfLength() ​

Call Signature ​

ts
function isFunctionOfLength<T>(f, l): f is T;

Defined in: lib/modules-lib/node_modules/js-slang/dist/utils/rttc.d.ts:86

Type guard for checking that the provided value is a function and that it has the specified number of parameters. Of course at runtime parameter types are not checked, so this is only useful when combined with TypeScript types.

Type Parameters ​
Type Parameter

T extends (...args) => any

Parameters ​
ParameterType

f

(...args) => any

l

Parameters<T>["length"]

Returns ​

f is T

Call Signature ​

ts
function isFunctionOfLength<T>(f, l): f is FunctionOfLength<T>;

Defined in: lib/modules-lib/node_modules/js-slang/dist/utils/rttc.d.ts:87

Type guard for checking that the provided value is a function and that it has the specified number of parameters. Of course at runtime parameter types are not checked, so this is only useful when combined with TypeScript types.

Type Parameters ​
Type Parameter

T extends number

Parameters ​
ParameterType

f

unknown

l

T

Returns ​

f is FunctionOfLength<T>


isNumberWithinRange() ​

Call Signature ​

ts
function isNumberWithinRange(
   value, 
   min?, 
   max?, 
   integer?
): value is number;

Defined in: lib/modules-lib/node_modules/js-slang/dist/utils/rttc.d.ts:118

Function for checking if a given value is a number and that it also potentially satisfies a bunch of other criteria:

  • Within a given range of [min, max]
  • Is an integer
  • Is not NaN
Parameters ​
ParameterType

value

unknown

min?

number

max?

number

integer?

boolean

Returns ​

value is number

Call Signature ​

ts
function isNumberWithinRange(value, options): value is number;

Defined in: lib/modules-lib/node_modules/js-slang/dist/utils/rttc.d.ts:119

Function for checking if a given value is a number and that it also potentially satisfies a bunch of other criteria:

  • Within a given range of [min, max]
  • Is an integer
  • Is not NaN
Parameters ​
ParameterType

value

unknown

options

InvalidNumberParameterErrorOptions

Returns ​

value is number


isTupleOfLength() ​

Call Signature ​

ts
function isTupleOfLength<T, U>(obj, l): obj is TupleOfLength<T, U>;

Defined in: lib/modules-lib/node_modules/js-slang/dist/utils/rttc.d.ts:103

Function for checking if the given obj is a tuple of the given length. Optionally, providing a type guard function or string can type check the entire tuple.

Type Parameters ​
Type Parameter

T extends number

U

Parameters ​
ParameterType

obj

U[]

l

T

Returns ​

obj is TupleOfLength<T, U>

Call Signature ​

ts
function isTupleOfLength<T, U>(
   obj, 
   l, 
   guard
): obj is TupleOfLength<T, U>;

Defined in: lib/modules-lib/node_modules/js-slang/dist/utils/rttc.d.ts:104

Function for checking if the given obj is a tuple of the given length. Optionally, providing a type guard function or string can type check the entire tuple.

Type Parameters ​
Type Parameter

T extends number

U

Parameters ​
ParameterType

obj

unknown

l

T

guard

(arg) => arg is U

Returns ​

obj is TupleOfLength<T, U>

Call Signature ​

ts
function isTupleOfLength<T, U>(
   obj, 
   l, 
   guard
): obj is TupleOfLength<T, TypeOfConstantToType<U>>;

Defined in: lib/modules-lib/node_modules/js-slang/dist/utils/rttc.d.ts:105

Function for checking if the given obj is a tuple of the given length. Optionally, providing a type guard function or string can type check the entire tuple.

Type Parameters ​
Type Parameter

T extends number

U extends TypeOfConstants

Parameters ​
ParameterType

obj

unknown

l

T

guard

U

Returns ​

obj is TupleOfLength<T, TypeOfConstantToType<U>>

Call Signature ​

ts
function isTupleOfLength<T>(obj, l): obj is TupleOfLength<T>;

Defined in: lib/modules-lib/node_modules/js-slang/dist/utils/rttc.d.ts:106

Function for checking if the given obj is a tuple of the given length. Optionally, providing a type guard function or string can type check the entire tuple.

Type Parameters ​
Type Parameter

T extends number

Parameters ​
ParameterType

obj

unknown

l

T

Returns ​

obj is TupleOfLength<T>


mockDebuggerContext() ​

ts
function mockDebuggerContext<T>(moduleState, name): DebuggerContext;

Defined in: lib/modules-lib/src/utilities.ts:81

Returns a partial DebuggerContext object that contains a context which contains the mocked module state for the given module. Function intended for testing use only.

Type Parameters ​

Type Parameter

T

Parameters ​

ParameterType

moduleState

T

name

string

Returns ​

DebuggerContext


radiansToDegrees() ​

ts
function radiansToDegrees(radians): number;

Defined in: lib/modules-lib/src/utilities.ts:24

Converts an angle in radians into degrees

Parameters ​

ParameterTypeDescription

radians

number

Angle in radians

Returns ​

number

Angle in degrees


wrapFunction() ​

Call Signature ​

ts
function wrapFunction<T, OptArgs>(
   f, 
   optArgCount, 
   funcName?, 
   stringified?, 
   source?
): T;

Defined in: lib/modules-lib/node_modules/js-slang/dist/utils/operators.d.ts:60

Augment the given function with the necessary information for it to be called properly by callIfFuncAndRightArgs. It won't redefine any existing details that the function has already been wrapped with.

Type Parameters ​
Type Parameter

T extends (...args) => any

OptArgs extends number

Parameters ​
ParameterType

f

HasCorrectParameters<T, OptArgs>

optArgCount

OptArgs

funcName?

string

stringified?

string

source?

string | null

Returns ​

T

Example ​
ts
export const wrapped = wrap((...args: any[]) => args.length, true, 'wrapped');
  • optArgCount: Represents the number of optional arguments the function has

    • If set to undefined, it will be assumed to be 0
    • If set to a number, that will be taken to be the number of optional arguments
    • If set to true, the function is assumed to have a rest argument
  • If stringified is undefined, the function won't try to define toReplString.

  • funcName

    • If funcName is undefined, the function won't try to define the name property.
    • If funcName is provided, the name property will get overriden.

Call Signature ​

ts
function wrapFunction<T>(
   f, 
   optArgCount, 
   funcName?, 
   stringified?, 
   source?
): T;

Defined in: lib/modules-lib/node_modules/js-slang/dist/utils/operators.d.ts:61

Augment the given function with the necessary information for it to be called properly by callIfFuncAndRightArgs. It won't redefine any existing details that the function has already been wrapped with.

Type Parameters ​
Type Parameter

T extends (...args) => any

Parameters ​
ParameterType

f

HasCorrectParameters<T, true>

optArgCount

true

funcName?

string

stringified?

string

source?

string | null

Returns ​

T

Example ​
ts
export const wrapped = wrap((...args: any[]) => args.length, true, 'wrapped');
  • optArgCount: Represents the number of optional arguments the function has

    • If set to undefined, it will be assumed to be 0
    • If set to a number, that will be taken to be the number of optional arguments
    • If set to true, the function is assumed to have a rest argument
  • If stringified is undefined, the function won't try to define toReplString.

  • funcName

    • If funcName is undefined, the function won't try to define the name property.
    • If funcName is provided, the name property will get overriden.

Call Signature ​

ts
function wrapFunction<T>(
   f, 
   optArgCount?, 
   funcName?, 
   stringified?, 
   source?
): T;

Defined in: lib/modules-lib/node_modules/js-slang/dist/utils/operators.d.ts:62

Augment the given function with the necessary information for it to be called properly by callIfFuncAndRightArgs. It won't redefine any existing details that the function has already been wrapped with.

Type Parameters ​
Type Parameter

T extends (...args) => any

Parameters ​
ParameterType

f

number extends Parameters<T>["length"] ? never : CheckTupleHelper<Parameters<T>, 0, []> extends true ? T : never

optArgCount?

undefined

funcName?

string

stringified?

string

source?

string | null

Returns ​

T

Example ​
ts
export const wrapped = wrap((...args: any[]) => args.length, true, 'wrapped');
  • optArgCount: Represents the number of optional arguments the function has

    • If set to undefined, it will be assumed to be 0
    • If set to a number, that will be taken to be the number of optional arguments
    • If set to true, the function is assumed to have a rest argument
  • If stringified is undefined, the function won't try to define toReplString.

  • funcName

    • If funcName is undefined, the function won't try to define the name property.
    • If funcName is provided, the name property will get overriden.

wrapFunctionUnsafe() ​

ts
function wrapFunctionUnsafe<T>(
   f, 
   optArgCount?, 
   funcName?, 
   stringified?, 
   source?
): T;

Defined in: lib/modules-lib/node_modules/js-slang/dist/utils/operators.d.ts:67

A type-agnostic version of wrap to make it easier when the function type is not known.

Type Parameters ​

Type Parameter

T extends (...args) => any

Parameters ​

ParameterType

f

T

optArgCount?

number | true

funcName?

string

stringified?

string

source?

string | null

Returns ​

T