Skip to main content

functions/utils

Helper functions that do not belong to any category in particular.

Functions

eRange()

function eRange(
start,
end?,
increment?): Generator<number>

Helper function to get an iterator of integers with the specified range, inclusive on the lower end and exclusive on the high end. (The "e" in the function name stands for exclusive.) Thus, this function works in the same way as the built-in range function from Python.

If the end is lower than the start, then an empty range will be returned.

For example:

  • eRange(2) returns [0, 1].
  • eRange(3) returns [0, 1, 2].
  • eRange(-3) returns [0, -1, -2].
  • eRange(1, 3) returns [1, 2].
  • eRange(2, 5) returns [2, 3, 4].
  • eRange(5, 2) returns [].
  • eRange(3, 3) returns [].

If you want an array instead of an iterator, use the spread operator like this:

const myArray = [...eRange(1, 3)];

Parameters

ParameterTypeDefault valueDescription
startnumberundefinedThe integer to start at.
end?numberundefinedOptional. The integer to end at. If not specified, then the start will be 0 and the first argument will be the end.
increment?number1Optional. The increment to use. Default is 1.

Returns

Generator<number>

Defined in

functions/utils.ts:41


iRange()

function iRange(
start,
end?,
increment?): Generator<number>

Helper function to get an array of integers with the specified range, inclusive on both ends. (The "i" in the function name stands for inclusive.)

If the end is lower than the start, then an empty range will be returned.

For example:

  • iRange(2) returns [0, 1, 2].
  • iRange(3) returns [0, 1, 2, 3].
  • iRange(-3) returns [0, -1, -2, -3].
  • iRange(1, 3) returns [1, 2, 3].
  • iRange(2, 5) returns [2, 3, 4, 5].
  • iRange(5, 2) returns [].
  • iRange(3, 3) returns [3].

If you want an array instead of an iterator, use the spread operator like this:

const myArray = [...eRange(1, 3)];

Parameters

ParameterTypeDefault valueDescription
startnumberundefinedThe integer to start at.
end?numberundefinedOptional. The integer to end at. If not specified, then the start will be 0 and the first argument will be the end.
increment?number1Optional. The increment to use. Default is 1.

Returns

Generator<number>

Defined in

functions/utils.ts:83


isKeyOf()

function isKeyOf<T>(key, target): key is keyof T

From: https://stackoverflow.com/questions/61526746

Type Parameters

Type Parameter
T extends object

Parameters

ParameterType
keyPropertyKey
targetT

Returns

key is keyof T

Defined in

functions/utils.ts:98


noop()

function noop(): void

Helper function to perform a no-op. This can be useful in order to make a trailing return valid in functions that use the early return pattern.

Returns

void

Defined in

functions/utils.ts:110


parseFloatSafe()

function parseFloatSafe(string): number | undefined

This is a more reliable version of Number.parseFloat:

  • undefined is returned instead of Number.NaN, which is helpful in conjunction with TypeScript type narrowing patterns.
  • Strings that are a mixture of numbers and letters will result in undefined instead of the part of the string that is the number. (e.g. "1a" --> undefined instead of "1a" --> 1)
  • Non-strings will result in undefined instead of being coerced to a number.

Parameters

ParameterTypeDescription
stringstringA string to convert to an integer.

Returns

number | undefined

Defined in

functions/utils.ts:123


parseIntSafe()

function parseIntSafe(string): number | undefined

This is a more reliable version of Number.parseInt:

  • undefined is returned instead of Number.NaN, which is helpful in conjunction with TypeScript type narrowing patterns.
  • Strings that are a mixture of numbers and letters will result in undefined instead of the part of the string that is the number. (e.g. "1a" --> undefined instead of "1a" --> 1)
  • Non-strings will result in undefined instead of being coerced to a number.

If you have to use a radix other than 10, use the vanilla Number.parseInt function instead, because this function ensures that the string contains no letters.

Parameters

ParameterType
stringstring

Returns

number | undefined

Defined in

functions/utils.ts:151


repeat()

function repeat(num, func): void

Helper function to repeat code N times. This is faster to type and cleaner than using a for loop.

For example:

repeat(10, () => {
foo();
});

The repeated function is passed the index of the iteration, if needed:

repeat(3, (i) => {
console.log(i); // Prints "0", "1", "2"
});

Parameters

ParameterType
numnumber
func(i) => void

Returns

void

Defined in

functions/utils.ts:186


todo()

function todo(...args): void

Helper function to signify that the enclosing code block is not yet complete. Using this function is similar to writing a "TODO" comment, but it has the benefit of preventing ESLint errors due to unused variables or early returns.

When you see this function, it simply means that the programmer intends to add in more code to this spot later.

This function is variadic, meaning that you can pass as many arguments as you want. (This is useful as a means to prevent unused variables.)

This function does not actually do anything. (It is an "empty" function.)

Parameters

ParameterType
...argsreadonly unknown[]

Returns

void

Allow Empty Variadic

Defined in

functions/utils.ts:208