graphile-test
Version:
PostGraphile Testing
58 lines (57 loc) • 1.83 kB
JavaScript
const uuidRegexp = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
const idReplacement = (v) => (!v ? v : '[ID]');
function mapValues(obj, fn) {
return Object.entries(obj).reduce((acc, [key, value]) => {
acc[key] = fn(value, key);
return acc;
}, {});
}
export const pruneDates = (row) => mapValues(row, (v, k) => {
if (!v) {
return v;
}
if (v instanceof Date) {
return '[DATE]';
}
else if (typeof v === 'string' &&
/(_at|At)$/.test(k) &&
/^20[0-9]{2}-[0-9]{2}-[0-9]{2}/.test(v)) {
return '[DATE]';
}
return v;
});
export const pruneIds = (row) => mapValues(row, (v, k) => (k === 'id' || (typeof k === 'string' && k.endsWith('_id'))) &&
(typeof v === 'string' || typeof v === 'number')
? idReplacement(v)
: v);
export const pruneIdArrays = (row) => mapValues(row, (v, k) => typeof k === 'string' && k.endsWith('_ids') && Array.isArray(v)
? `[UUIDs-${v.length}]`
: v);
export const pruneUUIDs = (row) => mapValues(row, (v, k) => {
if (typeof v !== 'string') {
return v;
}
if (['uuid', 'queue_name'].includes(k) && uuidRegexp.test(v)) {
return '[UUID]';
}
if (k === 'gravatar' && /^[0-9a-f]{32}$/i.test(v)) {
return '[gUUID]';
}
return v;
});
export const pruneHashes = (row) => mapValues(row, (v, k) => typeof k === 'string' &&
k.endsWith('_hash') &&
typeof v === 'string' &&
v.startsWith('$')
? '[hash]'
: v);
export const prune = (obj) => pruneHashes(pruneUUIDs(pruneIds(pruneIdArrays(pruneDates(obj)))));
export const snapshot = (obj) => {
if (Array.isArray(obj)) {
return obj.map(snapshot);
}
else if (obj && typeof obj === 'object') {
return mapValues(prune(obj), snapshot);
}
return obj;
};