2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-09-24 21:53:21 +00:00
|
|
|
/* eslint-disable guard-for-in */
|
|
|
|
/* eslint-disable no-restricted-syntax */
|
|
|
|
/* eslint-disable no-proto */
|
|
|
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
2020-04-13 17:37:29 +00:00
|
|
|
|
|
|
|
const arrayBuffer = new ArrayBuffer(0);
|
|
|
|
const uint8Array = new Uint8Array();
|
|
|
|
|
2020-09-24 21:53:21 +00:00
|
|
|
const StaticArrayBufferProto = (arrayBuffer as any).__proto__;
|
|
|
|
const StaticUint8ArrayProto = (uint8Array as any).__proto__;
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2020-09-24 21:53:21 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
2020-04-13 17:37:29 +00:00
|
|
|
function getString(thing: any): string {
|
|
|
|
if (thing === Object(thing)) {
|
|
|
|
if (thing.__proto__ === StaticUint8ArrayProto) {
|
|
|
|
return String.fromCharCode.apply(null, thing);
|
|
|
|
}
|
|
|
|
if (thing.__proto__ === StaticArrayBufferProto) {
|
|
|
|
return getString(new Uint8Array(thing));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return thing;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getStringable(thing: any): boolean {
|
|
|
|
return (
|
|
|
|
typeof thing === 'string' ||
|
|
|
|
typeof thing === 'number' ||
|
|
|
|
typeof thing === 'boolean' ||
|
|
|
|
(thing === Object(thing) &&
|
|
|
|
(thing.__proto__ === StaticArrayBufferProto ||
|
2021-07-13 18:54:53 +00:00
|
|
|
thing.__proto__ === StaticUint8ArrayProto))
|
2020-04-13 17:37:29 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function ensureStringed(thing: any): any {
|
|
|
|
if (getStringable(thing)) {
|
|
|
|
return getString(thing);
|
2020-09-24 21:53:21 +00:00
|
|
|
}
|
|
|
|
if (thing instanceof Array) {
|
2020-04-13 17:37:29 +00:00
|
|
|
const res = [];
|
|
|
|
for (let i = 0; i < thing.length; i += 1) {
|
|
|
|
res[i] = ensureStringed(thing[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
2020-09-24 21:53:21 +00:00
|
|
|
}
|
|
|
|
if (thing === Object(thing)) {
|
2020-04-13 17:37:29 +00:00
|
|
|
const res: any = {};
|
|
|
|
for (const key in thing) {
|
|
|
|
res[key] = ensureStringed(thing[key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
2020-09-24 21:53:21 +00:00
|
|
|
}
|
2022-09-14 21:40:44 +00:00
|
|
|
if (thing == null) {
|
2020-04-13 17:37:29 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
throw new Error(`unsure of how to jsonify object of type ${typeof thing}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Number formatting utils
|
|
|
|
const utils = {
|
|
|
|
getString,
|
2020-09-24 21:53:21 +00:00
|
|
|
isNumberSane: (number: string): boolean =>
|
2020-04-13 17:37:29 +00:00
|
|
|
number[0] === '+' && /^[0-9]+$/.test(number.substring(1)),
|
2020-09-24 21:53:21 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
|
|
jsonThing: (thing: unknown) => JSON.stringify(ensureStringed(thing)),
|
|
|
|
unencodeNumber: (number: string): Array<string> => number.split('.'),
|
2020-04-13 17:37:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default utils;
|