Utils
Helper functions that do not belong to any category in particular.
Functions
Section titled “Functions”eRange()
Section titled “eRange()”function eRange(start, end?, increment?): Generator<number>;Defined in: functions/utils.ts:41
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
Section titled “Parameters”| Parameter | Type | Default value | Description |
|---|---|---|---|
start |
number |
undefined |
The integer to start at. |
end? |
number |
undefined |
Optional. The integer to end at. If not specified, then the start will be 0 and the first argument will be the end. |
increment? |
number |
1 |
Optional. The increment to use. Default is 1. |
Returns
Section titled “Returns”Generator<number>
iRange()
Section titled “iRange()”function iRange(start, end?, increment?): Generator<number>;Defined in: functions/utils.ts:83
Helper function to get an iterator of 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 = [...iRange(1, 3)];Parameters
Section titled “Parameters”| Parameter | Type | Default value | Description |
|---|---|---|---|
start |
number |
undefined |
The integer to start at. |
end? |
number |
undefined |
Optional. The integer to end at. If not specified, then the start will be 0 and the first argument will be the end. |
increment? |
number |
1 |
Optional. The increment to use. Default is 1. |
Returns
Section titled “Returns”Generator<number>
isKeyOf()
Section titled “isKeyOf()”function isKeyOf<T>(key, target): key is keyof T;Defined in: functions/utils.ts:98
From: https://stackoverflow.com/questions/61526746
Type Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
T extends object |
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
key |
PropertyKey |
target |
T |
Returns
Section titled “Returns”key is keyof T
noop()
Section titled “noop()”function noop(): void;Defined in: functions/utils.ts:110
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
Section titled “Returns”void
parseFloatSafe()
Section titled “parseFloatSafe()”function parseFloatSafe(string): number | undefined;Defined in: functions/utils.ts:121
This is a more reliable version of Number.parseFloat:
undefinedis returned instead ofNumber.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
Section titled “Parameters”| Parameter | Type |
|---|---|
string |
string |
Returns
Section titled “Returns”number | undefined
parseIntSafe()
Section titled “parseIntSafe()”function parseIntSafe(string): number | undefined;Defined in: functions/utils.ts:151
This is a more reliable version of Number.parseInt:
undefinedis returned instead ofNumber.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
Section titled “Parameters”| Parameter | Type |
|---|---|
string |
string |
Returns
Section titled “Returns”number | undefined
repeat()
Section titled “repeat()”function repeat(num, func): void;Defined in: functions/utils.ts:188
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
Section titled “Parameters”| Parameter | Type |
|---|---|
num |
number |
func |
(i) => void |
Returns
Section titled “Returns”void
todo()
Section titled “todo()”function todo(...args): void;Defined in: functions/utils.ts:210
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
Section titled “Parameters”| Parameter | Type |
|---|---|
…args |
readonly unknown[] |
Returns
Section titled “Returns”void
