Skip to content

Commit c65baf8

Browse files
committed
feat(common): Checks and CodedError classes
Utilities for easier one-liners that can verify arguments or execution state in a much more concise way compared to if conditions throwing manually. Makes the code less verbose and requires less typing as well. Also: added a bools utlity class for checking strict boolean typing of a value and a new string utility method as well. Fixes #266 Signed-off-by: Peter Somogyvari <[email protected]>
1 parent bc4d6fe commit c65baf8

File tree

5 files changed

+67
-0
lines changed

5 files changed

+67
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export class Bools {
2+
/**
3+
* Determines if a value is strictly a boolean `true` or `false`. Anything else
4+
* will result in the method returning `false`.
5+
*
6+
* Useful in cases where you have an optional boolean parameter that you need
7+
* to assign a default value to by determining if it had been set or not.
8+
*
9+
* @param val The value to be decided on whether it's strictly boolean `true`
10+
* or `false`.
11+
*/
12+
public static isBooleanStrict(val: unknown): boolean {
13+
return val === true || val === false;
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { CodedError } from "./coded-error";
2+
3+
export class Checks {
4+
/**
5+
* Verifies that a boolean condition is met or throws an Error if it is not.
6+
*
7+
* @param checkResult Determines the outcome of the check via it's truthyness.
8+
* @param subjectOfCheck The error message if `checkResult` is falsy.
9+
* @param code The code of the error if `checkResult is falsy.
10+
*/
11+
public static truthy(
12+
checkResult: any,
13+
subjectOfCheck: string = "variable",
14+
code: string = "-1"
15+
): void {
16+
if (!checkResult) {
17+
const message = `"${subjectOfCheck}" is falsy, need a truthy value.`;
18+
throw new CodedError(message, code);
19+
}
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Utility class for better exception handling by way of having a code property
3+
* which is designated to uniquely identify the type of exception that was
4+
* thrown, essentially acting as a discriminator property.
5+
*/
6+
export class CodedError extends Error {
7+
constructor(public readonly message: string, public readonly code: string) {
8+
super(message);
9+
}
10+
11+
sameCode(codedError: CodedError): boolean {
12+
return this.code === codedError?.code;
13+
}
14+
}

packages/cactus-common/src/main/typescript/public-api.ts

+5
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@ export { Logger, ILoggerOptions } from "./logging/logger";
33
export { LogLevelDesc } from "loglevel";
44
export { Objects } from "./objects";
55
export { Strings } from "./strings";
6+
export { Bools } from "./bools";
7+
export { Checks } from "./checks";
8+
export { CodedError } from "./coded-error";
9+
610
export {
711
JsObjectSigner,
812
IJsObjectSignerOptions,
913
SignatureFunction,
1014
VerifySignatureFunction,
1115
HashFunction,
1216
} from "./js-object-signer";
17+
1318
export { ISignerKeyPair } from "./signer-key-pair";
1419
export { Secp256k1Keys } from "./secp256k1-keys";

packages/cactus-common/src/main/typescript/strings.ts

+12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Checks } from "./checks";
2+
13
export class Strings {
24
public static replaceAll(
35
source: string,
@@ -6,4 +8,14 @@ export class Strings {
68
): string {
79
return source.replace(new RegExp(searchValue, "gm"), replaceValue);
810
}
11+
12+
public static isString(val: any): boolean {
13+
return typeof val === "string" || val instanceof String;
14+
}
15+
16+
public static dropNonPrintable(val: string): string {
17+
const fnTag = "Strings#dropNonPrintable()";
18+
Checks.truthy(Strings.isString(val), `${fnTag} Strings.isString(val)`);
19+
return val.replace(/[^ -~]+/g, "");
20+
}
921
}

0 commit comments

Comments
 (0)