Skip to content

Commit

Permalink
write docs for aggregateObjects
Browse files Browse the repository at this point in the history
  • Loading branch information
abrantesarthur committed Jan 14, 2025
1 parent a8500c6 commit ae821ae
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
33 changes: 25 additions & 8 deletions src/oneTrust/helpers/flattenOneTrustAssessment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ import {
OneTrustEnrichedRisk,
} from '../codecs';

// TODO: will have to use something like csv-stringify

// TODO: test what happens when a value is null -> it should convert to ''
// FIXME: move to @transcend/type-utils, document and write tests
const flattenObject = (obj: any, prefix = ''): any =>
Object.keys(obj ?? []).reduce((acc, key) => {
const newKey = prefix ? `${prefix}_${key}` : key;
Expand All @@ -42,22 +40,42 @@ const flattenObject = (obj: any, prefix = ''): any =>
return acc;
}, {} as Record<string, any>);

// TODO: move to helpers
// FIXME: move to @transcend/type-utils
/**
* Aggregates multiple objects into a single object by combining values of matching keys.
* For each key present in any of the input objects, creates a comma-separated string
* of values from all objects.
*
* @param param - the objects to aggregate and the aggregation method
* @returns a single object containing all unique keys with aggregated values
* @example
* const obj1 = { name: 'John', age: 30 };
* const obj2 = { name: 'Jane', city: 'NY' };
* const obj3 = { name: 'Bob', age: 25 };
*
* // Without wrap
* aggregateObjects({ objs: [obj1, obj2, obj3] })
* // Returns: { name: 'John,Jane,Bob', age: '30,,25', city: ',NY,' }
*
* // With wrap
* aggregateObjects({ objs: [obj1, obj2, obj3], wrap: true })
* // Returns: { name: '[John],[Jane],[Bob]', age: '[30],[],[25]', city: '[],[NY],[]' }
*/
const aggregateObjects = ({
objs,
wrap = false,
}: {
/** the objects to aggregate in a single one */
objs: any[];
/** whether to wrap the values in a [] */
/** whether to wrap the concatenated values in a [] */
wrap?: boolean;
}): any => {
const allKeys = Array.from(new Set(objs.flatMap((a) => Object.keys(a))));

// build a single object where all the keys contain the respective values of objs
// Reduce into a single object, where each key contains concatenated values from all input objects
return allKeys.reduce((acc, key) => {
const values = objs
.map((a) => (wrap ? `[${a[key] ?? ''}]` : a[key] ?? ''))
.map((o) => (wrap ? `[${o[key] ?? ''}]` : o[key] ?? ''))
.join(',');
acc[key] = values;
return acc;
Expand All @@ -80,7 +98,6 @@ const flattenOneTrustNestedQuestions = (
questions: OneTrustAssessmentNestedQuestion[],
prefix: string,
): any => {
// TODO: how do extract properties handle null
const { options: allOptions, rest: restQuestions } = extractProperties(
questions,
['options'],
Expand Down
4 changes: 2 additions & 2 deletions src/oneTrust/helpers/oneTrustAssessmentToCsvRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { flattenOneTrustAssessment } from './flattenOneTrustAssessment';
import { OneTrustAssessmentCsvRecord } from '@transcend-io/privacy-types';

/**
* Converts the assessment into a csv record (header + values). It always
* returns a record with every key in the same order.
* Converts the assessment into a csv record (i.e. a map from the csv header
* to values). It always returns a record with every key in the same order.
*
* @param assessment - the assessment to convert to a csv record
* @returns a stringified csv entry ready to be appended to a file
Expand Down
4 changes: 2 additions & 2 deletions src/oneTrust/helpers/syncOneTrustAssessments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const syncOneTrustAssessments = async ({
const assessments = await getListOfOneTrustAssessments({ oneTrust });

/**
* fetch details about one assessment in series and push to transcend or write to disk
* fetch details about each assessment in series and write to transcend or to disk
* (depending on the dryRun argument) right away to avoid running out of memory
*/
await mapSeries(assessments, async (assessment, index) => {
Expand Down Expand Up @@ -86,7 +86,7 @@ export const syncOneTrustAssessments = async ({
);
}

// enrich the sections with risk details
// enrich the assessments with risk and details
const enrichedAssessment = enrichOneTrustAssessment({
assessment,
assessmentDetails,
Expand Down

0 comments on commit ae821ae

Please sign in to comment.