mirror of
https://github.com/vector-im/element-call.git
synced 2026-06-30 18:02:56 +00:00
Don't redact null or undefined
This commit is contained in:
@@ -34,3 +34,25 @@ test("no redaction of unrelated keys", () => {
|
||||
bar: "foo",
|
||||
});
|
||||
});
|
||||
|
||||
test("no redaction of missing keys", () => {
|
||||
expect(
|
||||
redact({ foo: "bar" } as { foo: string; bar: string | undefined }, "bar"),
|
||||
).to.deep.equal({
|
||||
foo: "bar",
|
||||
});
|
||||
});
|
||||
|
||||
test("no redaction of null values", () => {
|
||||
expect(redact({ foo: "bar", bar: null }, "bar")).to.deep.equal({
|
||||
foo: "bar",
|
||||
bar: null,
|
||||
});
|
||||
});
|
||||
|
||||
test("no redaction of undefined values", () => {
|
||||
expect(redact({ foo: "bar", bar: undefined }, "bar")).to.deep.equal({
|
||||
foo: "bar",
|
||||
bar: undefined,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -12,6 +12,14 @@ Please see LICENSE in the repository root for full details.
|
||||
* @param keys Keys to be redacted in the object
|
||||
* @returns A new object with the specified properties redacted
|
||||
*/
|
||||
export function redact<T extends object>(obj: T, ...keys: (keyof T)[]): T {
|
||||
return { ...obj, ...Object.fromEntries(keys.map((k) => [k, "<redacted>"])) };
|
||||
export function redact<T extends object>(
|
||||
obj: T,
|
||||
...keys: (keyof T)[]
|
||||
): Record<keyof T, unknown> {
|
||||
const result: Record<keyof T, unknown> = { ...obj };
|
||||
for (const key of keys)
|
||||
if (key in result && result[key] != null) {
|
||||
result[key] = "<redacted>";
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user