Better, recursive FlattenObject that now also parses recursively
This commit is contained in:
parent
b11f5f9737
commit
1d31b79f1e
8
test.ts
8
test.ts
|
@ -1,3 +1,5 @@
|
||||||
|
import ExtendedRequest from "./extended-request";
|
||||||
|
import { StolenDataEntry } from "./stolen-data-entry";
|
||||||
import { flattenObject, maskString } from "./util";
|
import { flattenObject, maskString } from "./util";
|
||||||
|
|
||||||
console.log(flattenObject({ a: { b: { c: [1, 2, 3] } } }));
|
console.log(flattenObject({ a: { b: { c: [1, 2, 3] } } }));
|
||||||
|
@ -5,3 +7,9 @@ console.log(flattenObject({ a: { b: { c: [1, 2, 3] } } }));
|
||||||
console.log(maskString("abcdefghijklmnopqrstuvwxyz", 1 / 3, 5));
|
console.log(maskString("abcdefghijklmnopqrstuvwxyz", 1 / 3, 5));
|
||||||
|
|
||||||
console.log(maskString("abcdefghijklmnopqrstuvwxyz", 1, 30));
|
console.log(maskString("abcdefghijklmnopqrstuvwxyz", 1, 30));
|
||||||
|
|
||||||
|
console.log(
|
||||||
|
StolenDataEntry.parseValue(
|
||||||
|
`[{"@context":"https://schema.org/","@type":"Product","image":["/medias/sys_master/root/images/h95/h8b/10873724928030/oppo-reno6-black-front.png","/medias/sys_master/root/images/h15/hb9/10873725681694/oppo-reno6-black-side.png","/medias/sys_master/root/images/hf8/h81/10873728729118/oppo-reno6-black-back.png"],"description":"OPPO Reno6 5G bez umowy lub w abonamencie w sklepie Orange. Zamów do domu lub odbierz w salonie w 24h. Transport gratis!","sku":"1100027218","brand":{"@type":"Thing","name":"OPPO"},"offers":{"@type":"Offer","priceCurrency":"PLN","priceValidUntil":"2099-12-31T00:00:00","itemCondition":"https://schema.org/UsedCondition","availability":"https://schema.org/InStock","url":"/esklep/smartfony/oppo/oppo-reno6-5g","seller":{"@type":"Organization","name":"Orange Polska S.A"},"price":"2199"},"name":"OPPO Reno6 5G"},{"@context":"http://schema.org/","@type":"Product","name":"OPPO Reno6 5G","description":"null","offers":{"@type":"Offer","priceCurrency":"PLN","price":"1248.00"}}]`
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
35
util.ts
35
util.ts
|
@ -197,31 +197,38 @@ export function isBase64JSON(s: unknown): s is string {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function flattenObject(
|
export function flattenObject(
|
||||||
obj: Record<string, unknown>
|
obj: unknown,
|
||||||
|
parser: (to_parse: unknown) => string | Record<string, unknown> = (id) =>
|
||||||
|
id.toString(),
|
||||||
|
key = "",
|
||||||
|
ret = [],
|
||||||
|
parsed = false
|
||||||
): [string, string][] {
|
): [string, string][] {
|
||||||
const ret: [string, string][] = [];
|
if (Array.isArray(obj)) {
|
||||||
for (const [key, value] of Object.entries(obj)) {
|
for (let i in obj) {
|
||||||
const value = obj[key];
|
flattenObject(obj[i], parser, key + "." + i, ret);
|
||||||
if (value === null) {
|
|
||||||
ret.push([key, "null"]);
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
if (typeof value === "object") {
|
} else if (typeof obj === "object") {
|
||||||
const flattened = flattenObject(value as Record<string, unknown>);
|
for (const [subkey, value] of Object.entries(obj)) {
|
||||||
for (const [subkey, subvalue] of flattened) {
|
flattenObject(value, parser, key + "." + subkey, ret);
|
||||||
ret.push([`${key}.${subkey}`, subvalue]);
|
|
||||||
}
|
}
|
||||||
|
} else if (!parsed) {
|
||||||
|
flattenObject(parser(obj), parser, key, ret, true);
|
||||||
} else {
|
} else {
|
||||||
ret.push([key, value ? value.toString() : "<empty>"]);
|
ret.push([key, obj]);
|
||||||
}
|
}
|
||||||
|
if (key == "") {
|
||||||
|
console.log("FLATTENING!", obj, ret);
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function flattenObjectEntries(
|
export function flattenObjectEntries(
|
||||||
entries: [string, unknown][]
|
entries: [string, unknown][],
|
||||||
|
parser: (to_parse: unknown) => string | Record<string, unknown> = (id) =>
|
||||||
|
id.toString()
|
||||||
): [string, string][] {
|
): [string, string][] {
|
||||||
return flattenObject(Object.fromEntries(entries));
|
return flattenObject(Object.fromEntries(entries), parser);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function maskString(
|
export function maskString(
|
||||||
|
|
Loading…
Reference in New Issue
Block a user