@the_glitch_master/jstools
v1.4.1
Published
Javascript Utilities
Downloads
369
Readme
jstools
jstools/async
AsyncLock
A mutex designed to allow safe concurrent access of shared values.
Example
const lock = await AsyncLock.new("myLock");
await lock.acquire();
doWork();
lock.release();
const v = await lock.with(async () => {
await doWork();
});
if (v instanceof Error) throw v; // Check for thrown errorsAsyncLock.new
Description
Create a new AsyncLock with AsyncLock.new. Example:
const lock = await AsyncLock.new("myLockName");Arguments
| Argument | Type | Description | | -------- | ---- | ----------- | | name | string | The name for the lock (used for error contents) | | disallowReentry? | boolean (=false) | Whether the same async context can acquire the lock multiple times (tracked with AsyncLocalStorage) |
Type
class AsyncLock {
static async new(name: string, disallowReentry: boolean = false): AsyncLock {};
}AsyncLock.acquire
Description
Acquire the lock, or wait until it can be acquired. Example:
const lock = await AsyncLock.new("myLockName");
await lock.acquire();Type
class AsyncLock {
acquire(): Promise<void> {};
}AsyncLock.release
Description
Release the lock. Example:
const lock = await AsyncLock.new("myLockName");
await lock.acquire();
// do work
lock.release();Type
class AsyncLock {
release(): void {};
}AsyncLock.with
Description
Acquire the lock, run a block of code, release the lock and return the value returned by the block of code (if any), or an error (if any error was thrown).
Type
class AsyncLock {
with(fn: () => Promise<Empty>): Promise<Empty | Error | any> {};
}getAsyncContextId
Get a unique identifier for the current async context.
Takes no arguments, returns a number.
jstools/string
jstools/string/transform
prefix.removePrefix
let stringTransform.prefix.removePrefix: (str: string, prefix: string) => string = ...;If the string
strstarts with the prefixprefix, the string will be returned without the prefix. Otherwise, the string will be returned unchanged.
prefix.removePrefixUnsafe
let stringTransform.prefix.removePrefixUnsafe: (str: string, prefix: string) => string = ...;If the string
strstarts with the prefixprefix, the string will be returned without the prefix. Otherwise, the string will be sliced anyways, as if the prefix were present.
prefix.hasPrefix
let stringTransform.prefix.hasPrefix: (str: string, prefix: string) => boolean = ...;If the string
strends with the prefixprefix, the function will returntrue. Otherwise,falsewill be returned.
suffix.removeSuffix
let stringTransform.suffix.removeSuffix: (str: string, suffix: string) => string = ...;If the string
strends with the suffixsuffix, the string will be returned without the suffix. Otherwise, the string will be returned unchanged.
suffix.removeSuffixUnsafe
let stringTransform.suffix.removeSuffixUnsafe: (str: string, suffix: string) => string = ...;If the string
strends with the suffixsuffix, the string will be returned without the suffix. Otherwise, the string will be sliced anyways, as if the suffix were present.
suffix.hasSuffix
let stringTransform.suffix.hasSuffix: (str: string, suffix: string) => boolean = ...;If the string
strstarts with the suffixsuffix, the function will returntrue. Otherwise,falsewill be returned.
strcmp
let stringTransform.strcmp: (a: string, b: string) => number = ...;Compares
aandbthe same way as the standard Cstrcmp()does, though return values differ. Ifa > b, return -1. Ifa < b, return 1. Ifa = b, return 0.
jstools/errors
| Error | Description | | ----- | ----------- | | AsyncError | Async-related errors | | RuntimeError | Runtime errors | | AsyncRuntimeError | Async-related runtime errors | | DeadlockError | Potential deadlocks | | AsyncDeadlockError | Async-related potential deadlocks | | RuntimeAsyncDeadlockError | Runtime async-related potential deadlocks | | TypeConversionError | Error during type conversions | | LogicError | Failed assertion or unexpected value |
These errors are not specific to this library, so feel free to use them yourself!
jstools/types
int.toString()
Arguments:
n: number?
Returns: string | "(undefined)" | "(math.inf)" | "-(math.inf)" | "NaN"
Description: Safely convert a number to a string (correctly handles inf/nan values and undefined values)
int.toHex()
Arguments:
n: number?signed: boolean = false(whether to not convert to an unsigned integer)
Returns: HexString | "0xinf" | "-0xinf" | "NaN"
Description: Safely convert a number to a hex string (correctly handles inf/nan values and undefined values)
int.toOctal()
Arguments:
n: number?signed: boolean = false(whether to not convert to an unsigned integer)
Returns: OctalString | "0oinf" | "-0oinf" | "NaN"
Description: Safely convert a number to an octal string (correctly handles inf/nan values and undefined values)
hex.toInt()
Arguments:
h: HexString?
Returns: number
Description: Safely convert a hex string into a number
hex.toIntAsString()
Arguments:
h: HexString?
Returns: string | "(undefined)" | "(math.inf)" | "-(math.inf)" | "NaN"
Description: Convert a hex string into an integer and then into a string
hex.asString()
Arguments:
h: HexString?
Returns: string | "(undefined)"
Description: Converts the raw contents of a hex string into an ordinary string
octal.toInt()
Arguments:
h: OctalString?
Returns: number
Description: Safely convert an octal string into a number
octal.toIntAsString()
Arguments:
h: OctalString?
Returns: string | "(undefined)" | "(math.inf)" | "-(math.inf)" | "NaN"
Description: Convert an octal string into an integer and then into a string
octal.asString()
Arguments:
h: OctalString?
Returns: string | "(undefined)"
Description: Converts the raw contents of an octal string into an ordinary string
