@synchronopeia/csv-recordset
v0.1.1
Published
Table--Dataset Interop With Barley Enough Schema Enforcement
Downloads
12
Readme
CSV-Recordset
Incorporate CSV's into a semi-structured-data workflow by using a simple schema.
Illustrative Example
import { fromCsv, toCsv } from '@synchronopedia/csv-recordset';
const SCHEMA_DEFS = [{
fieldId: 'participantId',
default: '',
/** without colName, this field is excluded from any CSV operations */
}, {
fieldId: 'lastName',
default: '',
colName: 'Last Name', /** CSV uses this as a header */
}, {
fieldId: 'firstName',
default: '',
colName: 'First Name',
}, {
fieldId: 'email',
default: '',
colName: 'Email',
}, {
fieldId: 'interestRating',
default: null,
colName: 'Interest Rating',
}];
const CSV = [
['Last Name', 'First Name', 'Email', 'Interest Rating'], /** header row */
['Tebaldi', 'Renata', '[email protected]', 91],
['Freni', 'Mirella', '[email protected]', 97],
['Anderson', 'Marian', '[email protected]'], /** OOPS! The integer value for the last column (Interest Rating) is missing */
['Flagstad', 'Kirsten', '[email protected]', 92],
];
const recsFromCsv = fromCsv(CSV, SCHEMA_DEFS);
/** Note that:
* (1) participantId is set to its default ('')
* (2) the missing value for Marian Anderson's "Interest Rating" has been set to its default (null)
*/
/**
[
{
participantId: '',
lastName: 'Tebaldi',
firstName: 'Renata',
email: '[email protected]',
interestRating: 91
},
{
participantId: '',
lastName: 'Freni',
firstName: 'Mirella',
email: '[email protected]',
interestRating: 97
},
{
participantId: '',
lastName: 'Anderson',
firstName: 'Marian',
email: '[email protected]',
interestRating: null
},
{
participantId: '',
lastName: 'Flagstad',
firstName: 'Kirsten',
email: '[email protected]',
interestRating: 92
}
]
*/
const recsFromCsvWithParticipantIds = recsFromCsv.map((rec, index) => ({
...rec,
participantId: `Singer-${index + 1}`,
}));
Requirements
We are using es6 modules (Node version >= 13.2.0).