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
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
Generator
<number
>
Defined in
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 = [...iRange(1, 3)];
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
Generator
<number
>
Defined in
isKeyOf()
function isKeyOf<T>(key, target): key is keyof T
From: https://stackoverflow.com/questions/61526746
Type Parameters
Type Parameter |
---|
T extends object |
Parameters
Parameter | Type |
---|---|
key | PropertyKey |
target | T |
Returns
key is keyof T
Defined in
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
parseFloatSafe()
function parseFloatSafe(string): number | undefined
This is a more reliable version of Number.parseFloat
:
undefined
is 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
Parameter | Type | Description |
---|---|---|
string | string | A string to convert to an integer. |
Returns
number
| undefined
Defined in
parseIntSafe()
function parseIntSafe(string): number | undefined
This is a more reliable version of Number.parseInt
:
undefined
is 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
Parameter | Type |
---|---|
string | string |
Returns
number
| undefined
Defined in
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
Parameter | Type |
---|---|
num | number |
func | (i ) => void |
Returns
void
Defined in
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
Parameter | Type |
---|---|
...args | readonly unknown [] |
Returns
void