Use the new flattening across other sources of data. Remove leading "." for entries one level deep

This commit is contained in:
Kuba Orlik 2021-11-28 12:31:51 +01:00
parent d33615987b
commit 97dc99dba9
2 changed files with 14 additions and 12 deletions

View File

@ -188,11 +188,11 @@ export default class ExtendedRequest {
return [];
}
return flattenObjectEntries(
Object.entries(parseCookie(this.getCookie()))
.map(([key, value]) => [key, value || ""])
.map(([key, value]) => {
return [key, StolenDataEntry.parseValue(value)];
})
Object.entries(parseCookie(this.getCookie())).map(([key, value]) => [
key,
value || "",
]),
StolenDataEntry.parseValue
).map(([key, value]) => new StolenDataEntry(this, "cookie", key, value));
}
@ -290,7 +290,7 @@ export default class ExtendedRequest {
.map((header) => {
return [
header.name,
StolenDataEntry.parseValue(decodeURIComponent(header.value)),
StolenDataEntry.parseValue(safeDecodeURIComponent(header.value)),
];
})
).map(([key, value]) => new StolenDataEntry(this, "header", key, value));

14
util.ts
View File

@ -204,22 +204,24 @@ export function flattenObject(
ret = [],
parsed = false
): [string, string][] {
const prefix = key === "" ? "" : `${key}.`;
if (Array.isArray(obj)) {
for (let i in obj) {
flattenObject(obj[i], parser, key + "." + i, ret);
if (obj.length == 1) {
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, key + "." + subkey, ret);
flattenObject(value, parser, prefix + subkey, ret);
}
} else if (!parsed) {
flattenObject(parser(obj), parser, key, ret, true);
} else {
ret.push([key, obj]);
}
if (key == "") {
console.log("FLATTENING!", obj, ret);
}
return ret;
}