summaryrefslogtreecommitdiff
path: root/packages/taler-util/src/helpers.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/taler-util/src/helpers.ts')
-rw-r--r--packages/taler-util/src/helpers.ts30
1 files changed, 21 insertions, 9 deletions
diff --git a/packages/taler-util/src/helpers.ts b/packages/taler-util/src/helpers.ts
index 089602c9d..d4c3c86b5 100644
--- a/packages/taler-util/src/helpers.ts
+++ b/packages/taler-util/src/helpers.ts
@@ -63,10 +63,7 @@ export function canonicalJson(obj: any): string {
// Check for cycles, etc.
obj = JSON.parse(JSON.stringify(obj));
if (typeof obj === "string") {
- const s = JSON.stringify(obj);
- return s.replace(/[\u007F-\uFFFF]/g, function (chr) {
- return "\\u" + ("0000" + chr.charCodeAt(0).toString(16)).substr(-4);
- });
+ return JSON.stringify(obj);
}
if (typeof obj === "number" || typeof obj === "boolean" || obj === null) {
return JSON.stringify(obj);
@@ -94,7 +91,7 @@ export function canonicalJson(obj: any): string {
/**
* Lexically compare two strings.
*/
-export function strcmp(s1: string, s2: string): number {
+export function strcmp(s1: string, s2: string): -1 | 0 | 1 {
if (s1 < s2) {
return -1;
}
@@ -113,15 +110,30 @@ export function j2s(x: any): string {
/**
* Use this to filter null or undefined from an array in a type-safe fashion
- *
+ *
* example:
* const array: Array<T | undefined> = [undefined, null]
* const filtered: Array<T> = array.filter(notEmpty)
- *
- * @param value
- * @returns
+ *
+ * @param value
+ * @returns
*/
export function notEmpty<T>(value: T | null | undefined): value is T {
return value !== null && value !== undefined;
}
+/**
+ * Safe function to stringify errors.
+ */
+export function stringifyError(x: any): string {
+ if (typeof x === "undefined") {
+ return "<thrown undefined>";
+ }
+ if (x === null) {
+ return `<thrown null>`;
+ }
+ if (typeof x === "object") {
+ return x.toString();
+ }
+ return `<thrown ${typeof x}>`;
+}