-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create oneTrustAssessmentToCsvRecord helper
- Loading branch information
1 parent
5dae37d
commit 4256788
Showing
4 changed files
with
49 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { decodeCodec } from '@transcend-io/type-utils'; | ||
Check failure on line 1 in src/oneTrust/helpers/oneTrustAssessmentToCsvRecord.ts GitHub Actions / build-and-upload-artifacts
|
||
import { OneTrustEnrichedAssessment } from '../codecs'; | ||
import { DEFAULT_ONE_TRUST_ASSESSMENT_CSV_HEADER } from './constants'; | ||
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. | ||
* | ||
* @param assessment - the assessment to convert to a csv record | ||
* @returns a stringified csv entry ready to be appended to a file | ||
*/ | ||
export const oneTrustAssessmentToCsvRecord = ( | ||
/** The assessment to convert */ | ||
assessment: OneTrustEnrichedAssessment, | ||
): OneTrustAssessmentCsvRecord => { | ||
// flatten the assessment object so it does not have nested properties | ||
const flatAssessment = flattenOneTrustAssessment(assessment); | ||
|
||
// transform the flat assessment to have all CSV keys in the expected order | ||
const flatAssessmentFull = Object.fromEntries( | ||
DEFAULT_ONE_TRUST_ASSESSMENT_CSV_HEADER.map((header) => { | ||
const value = flatAssessment[header] ?? ''; | ||
const escapedValue = | ||
typeof value === 'string' && | ||
(value.includes(',') || value.includes('"')) | ||
? `"${value.replace(/"/g, '""')}"` | ||
: value; | ||
return [header, escapedValue]; | ||
}), | ||
); | ||
|
||
// ensure the record has the expected type! | ||
return decodeCodec(OneTrustAssessmentCsvRecord, flatAssessmentFull); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters