Fix typescript errors
This commit is contained in:
parent
f647a07d94
commit
9c1eb31224
|
@ -6,7 +6,7 @@ import { getshorthost, unique } from '../util';
|
||||||
function handleNewFile(
|
function handleNewFile(
|
||||||
element: HTMLInputElement,
|
element: HTMLInputElement,
|
||||||
entries: StolenDataEntry[],
|
entries: StolenDataEntry[],
|
||||||
setFiltered: (Blob) => void
|
setFiltered: (arg0: Blob) => void
|
||||||
): void {
|
): void {
|
||||||
const reader = new FileReader();
|
const reader = new FileReader();
|
||||||
reader.addEventListener('load', () => {
|
reader.addEventListener('load', () => {
|
||||||
|
|
|
@ -2,15 +2,8 @@ import React, { Fragment, useEffect, useState } from 'react';
|
||||||
import ReactDOM from 'react-dom';
|
import ReactDOM from 'react-dom';
|
||||||
import Options from '../options';
|
import Options from '../options';
|
||||||
import { StolenData } from './stolen-data';
|
import { StolenData } from './stolen-data';
|
||||||
import { getshorthost, useEmitter } from '../util';
|
import { useEmitter } from '../util';
|
||||||
import { getMemory } from '../memory';
|
import { getMemory } from '../memory';
|
||||||
import InfoCircleIcon from '../assets/icons/info_circle_outline.svg';
|
|
||||||
import SettingsIcon from '../assets/icons/settings.svg';
|
|
||||||
import TrashIcon from '../assets/icons/trash_full.svg';
|
|
||||||
import MailIcon from '../assets/icons/mail.svg';
|
|
||||||
import ShortLeftIcon from '../assets/icons/short_left.svg';
|
|
||||||
import CloseBigIcon from '../assets/icons/close_big.svg';
|
|
||||||
import CookiesIcon from '../assets/icons/cookie.svg';
|
|
||||||
|
|
||||||
async function getCurrentTab() {
|
async function getCurrentTab() {
|
||||||
const [tab] = await browser.tabs.query({
|
const [tab] = await browser.tabs.query({
|
||||||
|
@ -110,11 +103,19 @@ const Sidebar = () => {
|
||||||
</div>
|
</div>
|
||||||
{stolenDataView ? (
|
{stolenDataView ? (
|
||||||
<a href="https://internet-czas-dzialac.pl">
|
<a href="https://internet-czas-dzialac.pl">
|
||||||
<InfoCircleIcon />
|
<img
|
||||||
|
src="/assets/icons/info_circle_outline.svg"
|
||||||
|
width="20"
|
||||||
|
height="20"
|
||||||
|
/>
|
||||||
</a>
|
</a>
|
||||||
) : (
|
) : (
|
||||||
<button onClick={() => setStolenDataView(true)}>
|
<button onClick={() => setStolenDataView(true)}>
|
||||||
<ShortLeftIcon />
|
<img
|
||||||
|
src="/assets/icons/short_left.svg"
|
||||||
|
width="20"
|
||||||
|
height="20"
|
||||||
|
/>
|
||||||
</button>
|
</button>
|
||||||
)}
|
)}
|
||||||
</header>
|
</header>
|
||||||
|
@ -122,7 +123,11 @@ const Sidebar = () => {
|
||||||
{stolenDataView ? (
|
{stolenDataView ? (
|
||||||
<nav>
|
<nav>
|
||||||
<button onClick={() => setStolenDataView(!stolenDataView)}>
|
<button onClick={() => setStolenDataView(!stolenDataView)}>
|
||||||
<SettingsIcon width={20} height={20} />
|
<img
|
||||||
|
src="/assets/icons/settings.svg"
|
||||||
|
width="20"
|
||||||
|
height="20"
|
||||||
|
/>
|
||||||
<span>Ustawienia</span>
|
<span>Ustawienia</span>
|
||||||
</button>
|
</button>
|
||||||
<button
|
<button
|
||||||
|
@ -132,7 +137,11 @@ const Sidebar = () => {
|
||||||
setMarksOccurrence(false);
|
setMarksOccurrence(false);
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<TrashIcon width={20} height={20} />
|
<img
|
||||||
|
src="/assets/icons/trash_full.svg"
|
||||||
|
width="20"
|
||||||
|
height="20"
|
||||||
|
/>
|
||||||
<span>Wyczyść historię wtyczki</span>
|
<span>Wyczyść historię wtyczki</span>
|
||||||
</button>
|
</button>
|
||||||
<button
|
<button
|
||||||
|
@ -142,7 +151,11 @@ const Sidebar = () => {
|
||||||
setMarksOccurrence(false);
|
setMarksOccurrence(false);
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<CookiesIcon width={20} height={20} />
|
<img
|
||||||
|
src="/assets/icons/cookie.svg"
|
||||||
|
width="20"
|
||||||
|
height="20"
|
||||||
|
/>
|
||||||
<span>Wyczyść ciasteczka</span>
|
<span>Wyczyść ciasteczka</span>
|
||||||
</button>
|
</button>
|
||||||
<button
|
<button
|
||||||
|
@ -165,7 +178,11 @@ const Sidebar = () => {
|
||||||
);
|
);
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<MailIcon width={20} height={20} />
|
<img
|
||||||
|
src="/assets/icons/mail.svg"
|
||||||
|
width="20"
|
||||||
|
height="20"
|
||||||
|
/>
|
||||||
<span>Utwórz wiadomość dla administratora witryny</span>
|
<span>Utwórz wiadomość dla administratora witryny</span>
|
||||||
</button>
|
</button>
|
||||||
</nav>
|
</nav>
|
||||||
|
@ -192,7 +209,11 @@ const Sidebar = () => {
|
||||||
);
|
);
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<CloseBigIcon width={16} height={16} />
|
<img
|
||||||
|
src="/assets/icons/close_big.svg"
|
||||||
|
width="16"
|
||||||
|
height="16"
|
||||||
|
/>
|
||||||
</button>
|
</button>
|
||||||
</section>
|
</section>
|
||||||
) : null}
|
) : null}
|
||||||
|
|
|
@ -3,8 +3,6 @@ import { getMemory } from '../memory';
|
||||||
import { StolenDataEntry } from '../stolen-data-entry';
|
import { StolenDataEntry } from '../stolen-data-entry';
|
||||||
|
|
||||||
import { maskString, useEmitter } from '../util';
|
import { maskString, useEmitter } from '../util';
|
||||||
import CookieIcon from '../assets/icons/cookie.svg';
|
|
||||||
import WarningIcon from '../assets/icons/warning.svg';
|
|
||||||
|
|
||||||
import './stolen-data-cluster.scss';
|
import './stolen-data-cluster.scss';
|
||||||
|
|
||||||
|
@ -80,7 +78,8 @@ function StolenDataRow({
|
||||||
<td className="icons">
|
<td className="icons">
|
||||||
{entry.source === 'cookie' ? (
|
{entry.source === 'cookie' ? (
|
||||||
<span title="Dane przechowywane w Cookies">
|
<span title="Dane przechowywane w Cookies">
|
||||||
<CookieIcon
|
<img
|
||||||
|
src="/assets/icons/cookie.svg"
|
||||||
height={16}
|
height={16}
|
||||||
width={16}
|
width={16}
|
||||||
className="cookie-data"
|
className="cookie-data"
|
||||||
|
@ -91,7 +90,8 @@ function StolenDataRow({
|
||||||
title="Wysłane w zapytaniu opatrzonym Cookies"
|
title="Wysłane w zapytaniu opatrzonym Cookies"
|
||||||
style={{ opacity: 0.25 }}
|
style={{ opacity: 0.25 }}
|
||||||
>
|
>
|
||||||
<CookieIcon
|
<img
|
||||||
|
src="/assets/icons/cookie.svg"
|
||||||
height={16}
|
height={16}
|
||||||
width={16}
|
width={16}
|
||||||
className="request-with-cookie"
|
className="request-with-cookie"
|
||||||
|
@ -100,7 +100,8 @@ function StolenDataRow({
|
||||||
) : null}
|
) : null}
|
||||||
{entry.exposesOrigin() ? (
|
{entry.exposesOrigin() ? (
|
||||||
<span title="Pokazuje część historii przeglądania">
|
<span title="Pokazuje część historii przeglądania">
|
||||||
<WarningIcon
|
<img
|
||||||
|
src="/assets/icons/warning.svg"
|
||||||
height={16}
|
height={16}
|
||||||
width={16}
|
width={16}
|
||||||
className="show-history-part"
|
className="show-history-part"
|
||||||
|
@ -111,7 +112,8 @@ function StolenDataRow({
|
||||||
title="Jest częścią zapytania, które ujawnia historię przeglądania"
|
title="Jest częścią zapytania, które ujawnia historię przeglądania"
|
||||||
style={{ opacity: 0.25 }}
|
style={{ opacity: 0.25 }}
|
||||||
>
|
>
|
||||||
<WarningIcon
|
<img
|
||||||
|
src="/assets/icons/warning.svg"
|
||||||
height={16}
|
height={16}
|
||||||
width={16}
|
width={16}
|
||||||
className="request-with-history-part"
|
className="request-with-history-part"
|
||||||
|
|
|
@ -154,6 +154,11 @@ export class StolenDataEntry extends EventEmitter {
|
||||||
let object = StolenDataEntry.parseValue(this.value);
|
let object = StolenDataEntry.parseValue(this.value);
|
||||||
for (const key of key_path.split('.')) {
|
for (const key of key_path.split('.')) {
|
||||||
if (key === '') continue;
|
if (key === '') continue;
|
||||||
|
if (typeof key === 'string') {
|
||||||
|
throw new Error(
|
||||||
|
'something went wrong when parsing ' + key_path
|
||||||
|
);
|
||||||
|
}
|
||||||
object = StolenDataEntry.parseValue(object[key]);
|
object = StolenDataEntry.parseValue(object[key]);
|
||||||
}
|
}
|
||||||
return object;
|
return object;
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"jsx": "react",
|
"jsx": "react",
|
||||||
"esModuleInterop": true,
|
"noImplicitAny": true,
|
||||||
"lib": ["es2017", "dom", "es2019"],
|
"esModuleInterop": true,
|
||||||
"typeRoots": ["node_modules/@types", "node_modules/web-ext-types"],
|
"lib": ["es2017", "dom", "es2019"],
|
||||||
"target": "es2019",
|
"typeRoots": ["node_modules/@types", "node_modules/web-ext-types"],
|
||||||
"outDir": "lib"
|
"target": "es2019",
|
||||||
}
|
"outDir": "lib",
|
||||||
|
"skipLibCheck": true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
356
util.ts
356
util.ts
|
@ -1,259 +1,257 @@
|
||||||
import { EventEmitter } from "events";
|
import { EventEmitter } from 'events';
|
||||||
import {
|
import { Dispatch, SetStateAction, useEffect, useState } from 'react';
|
||||||
Dispatch,
|
|
||||||
ReactChildren,
|
|
||||||
SetStateAction,
|
|
||||||
useEffect,
|
|
||||||
useState,
|
|
||||||
} from "react";
|
|
||||||
|
|
||||||
export type Unpromisify<T> = T extends Promise<infer R> ? R : T;
|
export type Unpromisify<T> = T extends Promise<infer R> ? R : T;
|
||||||
export type Unarray<T> = T extends Array<infer R> ? R : T;
|
export type Unarray<T> = T extends Array<infer R> ? R : T;
|
||||||
|
|
||||||
export type Tab = Unarray<Unpromisify<ReturnType<typeof browser.tabs.query>>>;
|
export type Tab = Unarray<Unpromisify<ReturnType<typeof browser.tabs.query>>>;
|
||||||
export type Request = {
|
export type Request = {
|
||||||
cookieStoreId?: string;
|
cookieStoreId?: string;
|
||||||
documentUrl?: string; // RL of the document in which the resource will be loaded. For example, if the web page at "https://example.com" contains an image or an iframe, then the documentUrl for the image or iframe will be "https://example.com". For a top-level document, documentUrl is undefined.
|
documentUrl?: string; // RL of the document in which the resource will be loaded. For example, if the web page at "https://example.com" contains an image or an iframe, then the documentUrl for the image or iframe will be "https://example.com". For a top-level document, documentUrl is undefined.
|
||||||
frameId: number;
|
frameId: number;
|
||||||
incognito?: boolean;
|
incognito?: boolean;
|
||||||
method: string;
|
method: string;
|
||||||
originUrl: string;
|
originUrl: string;
|
||||||
parentFrameId: number;
|
parentFrameId: number;
|
||||||
proxyInfo?: {
|
proxyInfo?: {
|
||||||
host: string;
|
host: string;
|
||||||
port: number;
|
port: number;
|
||||||
|
type: string;
|
||||||
|
username: string;
|
||||||
|
proxyDNS: boolean;
|
||||||
|
failoverTimeout: number;
|
||||||
|
};
|
||||||
|
requestHeaders?: { name: string; value?: string; binaryValue?: number[] }[];
|
||||||
|
requestId: string;
|
||||||
|
tabId: number;
|
||||||
|
thirdParty?: boolean;
|
||||||
|
timeStamp: number;
|
||||||
type: string;
|
type: string;
|
||||||
username: string;
|
url: string; // the target of the request;
|
||||||
proxyDNS: boolean;
|
urlClassification?: { firstParty: string[]; thirdParty: string[] };
|
||||||
failoverTimeout: number;
|
|
||||||
};
|
|
||||||
requestHeaders?: { name: string; value?: string; binaryValue?: number[] }[];
|
|
||||||
requestId: string;
|
|
||||||
tabId: number;
|
|
||||||
thirdParty?: boolean;
|
|
||||||
timeStamp: number;
|
|
||||||
type: string;
|
|
||||||
url: string; // the target of the request;
|
|
||||||
urlClassification?: { firstParty: string[]; thirdParty: string[] };
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export function getshorthost(host: string) {
|
export function getshorthost(host: string) {
|
||||||
const parts = host
|
const parts = host
|
||||||
.replace(/^.*:\/\//, "")
|
.replace(/^.*:\/\//, '')
|
||||||
.replace(/\/.*$/, "")
|
.replace(/\/.*$/, '')
|
||||||
.split(".");
|
.split('.');
|
||||||
let lookback = !['co','com'].includes(parts.at(-2)) ? -2 : -3;
|
let lookback = !['co', 'com'].includes(parts.at(-2)) ? -2 : -3;
|
||||||
if (parts.at(-2) == "doubleclick" || parts.at(-2) == "google") {
|
if (parts.at(-2) == 'doubleclick' || parts.at(-2) == 'google') {
|
||||||
lookback = -4; // to distinguish between google ads and stats
|
lookback = -4; // to distinguish between google ads and stats
|
||||||
} else if (parts.at(-2) == "google") {
|
} else if (parts.at(-2) == 'google') {
|
||||||
lookback = -3; // to distinguish various google services
|
lookback = -3; // to distinguish various google services
|
||||||
}
|
}
|
||||||
return parts.slice(lookback).join(".");
|
return parts.slice(lookback).join('.');
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useEmitter(
|
export function useEmitter(
|
||||||
e: EventEmitter
|
e: EventEmitter
|
||||||
): [number, Dispatch<SetStateAction<number>>] {
|
): [number, Dispatch<SetStateAction<number>>] {
|
||||||
const [counter, setCounter] = useState<number>(0);
|
const [counter, setCounter] = useState<number>(0);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const callback = () => {
|
const callback = () => {
|
||||||
setCounter((counter) => counter + 1);
|
setCounter((counter) => counter + 1);
|
||||||
};
|
};
|
||||||
e.on("change", callback);
|
e.on('change', callback);
|
||||||
return () => {
|
return () => {
|
||||||
e.removeListener("change", callback);
|
e.removeListener('change', callback);
|
||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
return [counter, setCounter];
|
return [counter, setCounter];
|
||||||
}
|
}
|
||||||
|
|
||||||
export function parseCookie(cookie: string): Record<string, string> {
|
export function parseCookie(cookie: string): Record<string, string> {
|
||||||
return cookie
|
return cookie
|
||||||
.split(";")
|
.split(';')
|
||||||
.map((l) => l.split("="))
|
.map((l) => l.split('='))
|
||||||
.reduce(
|
.reduce(
|
||||||
(acc, [key, value]) => ({
|
(acc, [key, value]) => ({
|
||||||
...acc,
|
...acc,
|
||||||
[key]: value,
|
[key]: value,
|
||||||
}),
|
}),
|
||||||
{}
|
{}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getTabByID(id: number) {
|
export async function getTabByID(id: number) {
|
||||||
const tabs = await browser.tabs.query({ currentWindow: true });
|
const tabs = await browser.tabs.query({ currentWindow: true });
|
||||||
return tabs.find((tab) => tab.id == id);
|
return tabs.find((tab) => tab.id == id);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function parseToObject(str: unknown): Record<string | symbol, unknown> {
|
export function parseToObject(str: unknown): Record<string | symbol, unknown> {
|
||||||
let result: Record<string | symbol, unknown>;
|
let result: Record<string | symbol, unknown>;
|
||||||
let original_string: string;
|
let original_string: string;
|
||||||
if (typeof str === "string") {
|
if (typeof str === 'string') {
|
||||||
original_string = str;
|
original_string = str;
|
||||||
result = JSON.parse(str);
|
result = JSON.parse(str);
|
||||||
} else if (typeof str == "object") {
|
} else if (typeof str == 'object') {
|
||||||
result = str as Record<string | symbol, unknown>;
|
result = str as Record<string | symbol, unknown>;
|
||||||
original_string =
|
original_string =
|
||||||
(result[Symbol.for("originalString")] as string) || JSON.stringify(str);
|
(result[Symbol.for('originalString')] as string) ||
|
||||||
}
|
JSON.stringify(str);
|
||||||
result[Symbol.for("originalString")] = original_string;
|
}
|
||||||
return result;
|
result[Symbol.for('originalString')] = original_string;
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isJSONObject(
|
export function isJSONObject(
|
||||||
str: unknown
|
str: unknown
|
||||||
): str is Record<string, unknown> | string | number {
|
): str is Record<string, unknown> | string | number {
|
||||||
try {
|
try {
|
||||||
const firstChar = JSON.stringify(parseToObject(str))[0];
|
const firstChar = JSON.stringify(parseToObject(str))[0];
|
||||||
return ["{", "["].includes(firstChar);
|
return ['{', '['].includes(firstChar);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isURL(str: unknown): str is string {
|
export function isURL(str: unknown): str is string {
|
||||||
try {
|
try {
|
||||||
return !!(typeof str === "string" && new URL(str));
|
return !!(typeof str === 'string' && new URL(str));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function hyphenate(str: string): string {
|
export function hyphenate(str: string): string {
|
||||||
return str.replace(/[_\[A-Z]/g, `${String.fromCharCode(173)}$&`);
|
return str.replace(/[_\[A-Z]/g, `${String.fromCharCode(173)}$&`);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function unique<T>(array: T[]): Array<T> {
|
export function unique<T>(array: T[]): Array<T> {
|
||||||
return Array.from(new Set<T>(array));
|
return Array.from(new Set<T>(array));
|
||||||
}
|
}
|
||||||
|
|
||||||
export function allSubhosts(host: string) {
|
export function allSubhosts(host: string) {
|
||||||
const parts = host.split(".");
|
const parts = host.split('.');
|
||||||
const result = [];
|
const result = [];
|
||||||
for (let i = 0; i < parts.length - 2; i++) {
|
for (let i = 0; i < parts.length - 2; i++) {
|
||||||
result.push(parts.slice(i).join("."));
|
result.push(parts.slice(i).join('.'));
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function reduceConcat<T>(a: T[], b: T[]): T[] {
|
export function reduceConcat<T>(a: T[], b: T[]): T[] {
|
||||||
return a.concat(b);
|
return a.concat(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getDate() {
|
export function getDate() {
|
||||||
const d = new Date();
|
const d = new Date();
|
||||||
return `${d.getFullYear()}-${(d.getMonth() + 1)
|
return `${d.getFullYear()}-${(d.getMonth() + 1)
|
||||||
.toString()
|
.toString()
|
||||||
.padStart(2, "0")}-${d.getDate().toString().padStart(2, "0")}`;
|
.padStart(2, '0')}-${d.getDate().toString().padStart(2, '0')}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function toBase64(file: File): Promise<string> {
|
export function toBase64(file: File): Promise<string> {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
const FR = new FileReader();
|
const FR = new FileReader();
|
||||||
FR.addEventListener("load", (e) => {
|
FR.addEventListener('load', (e) => {
|
||||||
resolve(e.target.result as string);
|
resolve(e.target.result as string);
|
||||||
|
});
|
||||||
|
FR.readAsDataURL(file);
|
||||||
});
|
});
|
||||||
FR.readAsDataURL(file);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function makeThrottle(interval: number) {
|
export function makeThrottle(interval: number) {
|
||||||
let last_emit = 0;
|
let last_emit = 0;
|
||||||
function emit(callback: () => void) {
|
function emit(callback: () => void) {
|
||||||
if (Date.now() - last_emit > interval) {
|
if (Date.now() - last_emit > interval) {
|
||||||
callback();
|
callback();
|
||||||
last_emit = Date.now();
|
last_emit = Date.now();
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
return function (callback: () => void) {
|
||||||
return function (callback: () => void) {
|
if (!emit(callback)) {
|
||||||
if (!emit(callback)) {
|
setTimeout(() => emit(callback), interval);
|
||||||
setTimeout(() => emit(callback), interval);
|
}
|
||||||
}
|
};
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isSameURL(url1: string, url2: string): boolean {
|
export function isSameURL(url1: string, url2: string): boolean {
|
||||||
if (url1 === url2) {
|
if (url1 === url2) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
url1 = url1.replace(/^https?:\/\//, "").replace(/\/$/, "");
|
url1 = url1.replace(/^https?:\/\//, '').replace(/\/$/, '');
|
||||||
url2 = url2.replace(/^https?:\/\//, "").replace(/\/$/, "");
|
url2 = url2.replace(/^https?:\/\//, '').replace(/\/$/, '');
|
||||||
return url1 === url2;
|
return url1 === url2;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isBase64(s: string): boolean {
|
export function isBase64(s: string): boolean {
|
||||||
try {
|
try {
|
||||||
atob(s);
|
atob(s);
|
||||||
return true;
|
return true;
|
||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isBase64JSON(s: unknown): s is string {
|
export function isBase64JSON(s: unknown): s is string {
|
||||||
return typeof s === "string" && isBase64(s) && isJSONObject(atob(s));
|
return typeof s === 'string' && isBase64(s) && isJSONObject(atob(s));
|
||||||
}
|
}
|
||||||
|
|
||||||
export function flattenObject(
|
export function flattenObject(
|
||||||
obj: unknown,
|
obj: unknown,
|
||||||
parser: (to_parse: unknown) => string | Record<string, unknown> = (id) =>
|
parser: (to_parse: unknown) => string | Record<string, unknown> = (id) =>
|
||||||
id.toString(),
|
id.toString(),
|
||||||
key = "",
|
key = '',
|
||||||
ret = [],
|
ret = [] as [string, string][],
|
||||||
parsed = false
|
parsed = false
|
||||||
): [string, string][] {
|
): [string, string][] {
|
||||||
const prefix = key === "" ? "" : `${key}.`;
|
const prefix = key === '' ? '' : `${key}.`;
|
||||||
if (Array.isArray(obj)) {
|
if (Array.isArray(obj)) {
|
||||||
if (obj.length == 1) {
|
if (obj.length == 1) {
|
||||||
flattenObject(obj[0], parser, key, ret);
|
flattenObject(obj[0], parser, key, ret);
|
||||||
|
} else {
|
||||||
|
for (let i in obj) {
|
||||||
|
flattenObject(obj[i], parser, prefix + i, ret);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (typeof obj === 'object') {
|
||||||
|
for (const [subkey, value] of Object.entries(obj)) {
|
||||||
|
flattenObject(value, parser, prefix + subkey, ret);
|
||||||
|
}
|
||||||
|
} else if (!parsed) {
|
||||||
|
flattenObject(parser(obj), parser, key, ret, true);
|
||||||
|
} else if (typeof obj === 'string') {
|
||||||
|
ret.push([key, obj]);
|
||||||
} else {
|
} else {
|
||||||
for (let i in obj) {
|
throw new Error('Something went wrong when parsing ' + obj);
|
||||||
flattenObject(obj[i], parser, prefix + i, ret);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else if (typeof obj === "object") {
|
return ret;
|
||||||
for (const [subkey, value] of Object.entries(obj)) {
|
|
||||||
flattenObject(value, parser, prefix + subkey, ret);
|
|
||||||
}
|
|
||||||
} else if (!parsed) {
|
|
||||||
flattenObject(parser(obj), parser, key, ret, true);
|
|
||||||
} else {
|
|
||||||
ret.push([key, obj]);
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function flattenObjectEntries(
|
export function flattenObjectEntries(
|
||||||
entries: [string, unknown][],
|
entries: [string, unknown][],
|
||||||
parser: (to_parse: unknown) => string | Record<string, unknown> = (id) =>
|
parser: (to_parse: unknown) => string | Record<string, unknown> = (id) =>
|
||||||
id.toString()
|
id.toString()
|
||||||
): [string, string][] {
|
): [string, string][] {
|
||||||
return flattenObject(Object.fromEntries(entries), parser);
|
return flattenObject(Object.fromEntries(entries), parser);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function maskString(
|
export function maskString(
|
||||||
str: string,
|
str: string,
|
||||||
max_fraction_remaining: number,
|
max_fraction_remaining: number,
|
||||||
max_chars_total: number
|
max_chars_total: number
|
||||||
): string {
|
): string {
|
||||||
const amount_of_chars_to_cut =
|
const amount_of_chars_to_cut =
|
||||||
str.length - Math.min(str.length * max_fraction_remaining, max_chars_total);
|
str.length -
|
||||||
if (amount_of_chars_to_cut == 0) {
|
Math.min(str.length * max_fraction_remaining, max_chars_total);
|
||||||
return str;
|
if (amount_of_chars_to_cut == 0) {
|
||||||
}
|
return str;
|
||||||
return (
|
}
|
||||||
str.slice(0, str.length / 2 - amount_of_chars_to_cut / 2) +
|
return (
|
||||||
"(...)" +
|
str.slice(0, str.length / 2 - amount_of_chars_to_cut / 2) +
|
||||||
str.slice(str.length / 2 + amount_of_chars_to_cut / 2)
|
'(...)' +
|
||||||
);
|
str.slice(str.length / 2 + amount_of_chars_to_cut / 2)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function safeDecodeURIComponent(s: string) {
|
export function safeDecodeURIComponent(s: string) {
|
||||||
try {
|
try {
|
||||||
return decodeURIComponent(s);
|
return decodeURIComponent(s);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user