2022-05-30 16:44:37 +02:00
|
|
|
import { createHash } from 'crypto';
|
|
|
|
|
2023-05-15 14:12:03 +02:00
|
|
|
export function anonymise(s?: string): string {
|
|
|
|
if (!s) {
|
|
|
|
return '';
|
|
|
|
}
|
2022-05-30 16:44:37 +02:00
|
|
|
const hash = createHash('sha256')
|
|
|
|
.update(s, 'utf-8')
|
|
|
|
.digest('hex')
|
|
|
|
.slice(0, 9);
|
|
|
|
return `${hash}@unleash.run`;
|
|
|
|
}
|
2023-05-09 11:13:38 +02:00
|
|
|
|
|
|
|
export function anonymiseKeys<T>(object: T, keys: string[]): T {
|
|
|
|
if (typeof object !== 'object' || object === null) {
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Array.isArray(object)) {
|
2023-05-09 15:20:39 +02:00
|
|
|
return object.map((item) => anonymiseKeys(item, keys)) as T;
|
2023-05-09 11:13:38 +02:00
|
|
|
} else {
|
|
|
|
return Object.keys(object).reduce((result, key) => {
|
|
|
|
if (
|
|
|
|
keys.includes(key) &&
|
|
|
|
result[key] !== undefined &&
|
|
|
|
result[key] !== null
|
|
|
|
) {
|
|
|
|
result[key] = anonymise(result[key]);
|
|
|
|
} else if (typeof result[key] === 'object') {
|
|
|
|
result[key] = anonymiseKeys(result[key], keys);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}, object);
|
|
|
|
}
|
|
|
|
}
|