Newer
Older
import { CommandType } from "../types";
const digitRe = /^\d+$/;
const digitDotRe = /^\d+\..*$/;
const isDigit = (v: any) => digitRe.test(v);
const firstIsIndex = (s: string) => digitDotRe.test(s);
// Based on: https://www.30secondsofcode.org/js/s/unflatten-object
export const unflatten = (obj: any) =>
Object.keys(obj).reduce(
(res, k) => {
k.split(".")
.filter((it) => it !== "")
.map((v) => (isDigit(v) ? parseInt(v) - 1 : v))
.reduce(
(acc: any, e, i, keys) =>
acc[e] ||
(acc[e] = isNaN(Number(keys[i + 1]))
? keys.length - 1 === i
? obj[k]
: {}
: []),
res
);
return res;
},
firstIsIndex(Object.keys(obj)[0]) ? [] : {}
);
export const search = (obj: any, key: string): any => {
if (typeof obj !== "object") return null;
if (obj[key]) return obj[key];
for (const val of Object.values(obj)) {
const s = search(val, key);
if (s) return s;
}
};
export const searchParent = (
obj: any,
key: string
): Record<string, any> | undefined => {
if (typeof obj !== "object") return;
if (obj[key]) return obj;
for (const val of Object.values(obj)) {
const s = searchParent(val, key);
if (s) return s;
}
};
const _searchAll = (obj: any, key: string): any[] =>
typeof obj !== "object"
? []
: Object.entries(obj).reduce(
(acc, [k, v]) => [...acc, k === key ? v : _searchAll(v, key)],
[] as any[]
);
export const searchAll = (obj: any, key: string) =>
_searchAll(obj, key).flat(Infinity);
export const extractCommand = (msg: {
[key: string]: any;
}): CommandType | undefined => {
const msgType: string | undefined = search(msg, "msgType");
if (!msgType) {
const id: string | undefined = search(msg, "msgId");
const [frst] = id ? id.split("@") : [""];
return frst.toUpperCase().replace("_RESP", "") as CommandType;
return msgType.replace("_RESP", "") as CommandType;
export const unwrapObject = (data: any): any =>
!Array.isArray(data) &&
typeof data === "object" &&
Object.keys(data).length === 1
? Object.values(data)[0]
: data;
export const unwrapArray = (arr: any) =>
Array.isArray(arr) && arr.length === 1 ? arr[0] : arr;
const isObject = (val: any): boolean =>
typeof val === "object" && val !== null && !Array.isArray(val);
export const fullyUnwrapObject = (obj: any) =>
Object.keys(obj).length !== 1 ?
obj :
isObject(Object.values(obj)[0]) ?
fullyUnwrapObject(Object.values(obj)[0]) :
Object.values(obj)[0];
export const convertToObject = (arr: any[]) => arr.reduce(
(acc, it) => ({ ...acc, [it.resolvedPath]: it.resultParams }),
{}
);
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
export function makeBuffer(
rootRecord: protobuf.Root,
payload: any,
options: Record<string, string>
) {
const NoSessionContextRecord = rootRecord.lookupType(
"usp_record.NoSessionContextRecord"
);
const noSessionContextRecordMsg = NoSessionContextRecord.create({
payload,
});
const record: any = rootRecord.lookupType("usp_record.Record");
const recordMsg = record.create({
version: "1.0",
PayloadSecurity: record.PayloadSecurity.PLAINTEXT,
noSessionContext: noSessionContextRecordMsg,
...options,
});
const buffer = record.encode(recordMsg).finish();
return buffer;
}
export const uniq = (initial?: string): string =>
(initial || "") +
(
Date.now().toString(36) + Math.random().toString(36).substr(2, 5)
).toUpperCase();