From f26adda1a805df0ac46b876e35cb59d9ed193d06 Mon Sep 17 00:00:00 2001 From: Kuba Orlik Date: Sun, 7 Nov 2021 10:28:48 +0100 Subject: [PATCH] Move value parsing logic to separate class --- request-cluster.ts | 32 ++++++++++++++++++++++++++++++++ stolen-data-row.tsx | 33 ++++++++------------------------- 2 files changed, 40 insertions(+), 25 deletions(-) diff --git a/request-cluster.ts b/request-cluster.ts index e00cf84..902622e 100644 --- a/request-cluster.ts +++ b/request-cluster.ts @@ -4,6 +4,7 @@ import ExtendedRequest from "./extended-request"; export type Sources = "cookie" | "pathname" | "queryparams" | "header"; import { TCString, TCModel } from "@iabtcf/core"; +import { isJSONObject, isURL, parseToObject } from "./util"; const id = (function* id() { let i = 0; @@ -49,6 +50,33 @@ export class StolenDataEntry { hasValue(value: string) { return this.value === value; } + + static parseValue(value: unknown): string | Record { + if (isJSONObject(value)) { + const object = parseToObject(value); + return object; + } else if (isURL(value)) { + const url = new URL(value); + const object = { + host: url.host, + path: url.pathname, + ...Object.fromEntries( + ( + url.searchParams as unknown as { + entries: () => Iterable<[string, string]>; + } + ).entries() + ), + }; + return object; + } else { + return value.toString(); + } + } + + getParsedValue(): string | Record { + return StolenDataEntry.parseValue(this.value); + } } export class MergedStolenDataEntry { @@ -84,6 +112,10 @@ export class MergedStolenDataEntry { getValues() { return Array.from(new Set(this.entries.map((e) => e.value))); } + + getParsedValues() { + return Array.from(new Set(this.entries.map((e) => e.getParsedValue()))); + } } export class RequestCluster extends EventEmitter { diff --git a/stolen-data-row.tsx b/stolen-data-row.tsx index 0827dd5..a92ec35 100644 --- a/stolen-data-row.tsx +++ b/stolen-data-row.tsx @@ -1,7 +1,7 @@ import React from "react"; import memory from "./memory"; -import { Sources } from "./request-cluster"; -import { hyphenate, isJSONObject, isURL, parseToObject } from "./util"; +import { Sources, StolenDataEntry } from "./request-cluster"; +import { hyphenate } from "./util"; function StolenDataValueTable({ object, @@ -18,7 +18,7 @@ function StolenDataValueTable({ {hyphenate(key)} @@ -33,33 +33,16 @@ function StolenDataValue({ value, prefixKey = "", }: { - value: unknown; + value: string | Record; prefixKey?: string; }) { if (!value) { return <>; } - console.log("parsing value!", value); - if (isJSONObject(value)) { - const object = parseToObject(value); - return ; - } else if (isURL(value)) { - const url = new URL(value); - const object = { - host: url.host, - path: url.pathname, - ...Object.fromEntries( - ( - url.searchParams as unknown as { - entries: () => Iterable<[string, string]>; - } - ).entries() - ), - }; - return ; - } else { - return <>{value.toString()}; + if (typeof value === "string") { + return <>{value}; } + return ; } export default function StolenDataRow({ @@ -108,7 +91,7 @@ export default function StolenDataRow({ {entry.getSources().map((source) => icons[source])} - + ))}