Skip to content

Commit ee8acdb

Browse files
committed
CidDocument as interface not class
1 parent e4cb514 commit ee8acdb

File tree

10 files changed

+52
-49
lines changed

10 files changed

+52
-49
lines changed

src/decoding/base.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import Debug from 'debug';
1919
const log = Debug('CID:Decoder');
2020

2121
import type { Config } from '../config/Config.js';
22-
import { CidDocument } from '../document.js';
22+
import type { CidDocument } from '../document.js';
2323
import type { RawData, MappedData } from '../document.js';
2424

2525
export abstract class DecoderBase {
@@ -106,7 +106,7 @@ export abstract class DecoderBase {
106106
}
107107

108108
// takes a list of rows with a list of strings and returns a new Sheet object.
109-
documentFromRawData(name: string, sheetData: RawData) {
110-
return new CidDocument(name, this.convertSheetRowsToObjects(sheetData));
109+
documentFromRawData = (name: string, sheetData: RawData): CidDocument => {
110+
return { name, data: this.convertSheetRowsToObjects(sheetData) };
111111
}
112112
}

src/decoding/xlsx.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const log = Debug('CID:XLSXDecoder');
2626

2727
import { DecoderBase } from './base.js';
2828
import type { Config } from '../config/Config.js';
29-
import { CidDocument } from '../document.js';
29+
import type { CidDocument } from '../document.js';
3030

3131
// A decoder for CSVs
3232
class XlsxDecoder extends DecoderBase {
@@ -57,7 +57,7 @@ class XlsxDecoder extends DecoderBase {
5757
const dataWithAliases = this.renameColumnsToAliases(data);
5858

5959
log('DECODED:', dataWithAliases[0]);
60-
return new CidDocument(path, dataWithAliases);
60+
return { name: path, data: dataWithAliases };
6161
}
6262

6363
// Renames the incoming columns from their hunan names to their aliases

src/document.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,7 @@ export enum SUPPORTED_FILE_TYPES {
2323
export type RawData = Array<Array<any>>;
2424
export type MappedData = { [key: string]: any };
2525

26-
// The data class describing a sheet
27-
export class CidDocument {
26+
export interface CidDocument {
2827
name: string;
2928
data: MappedData[];
30-
constructor(name: string, data: MappedData[]) {
31-
this.name = name;
32-
this.data = data;
33-
}
34-
}
29+
}

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ export {
88
processFile,
99
validateDocument,
1010
} from './processing/index.js';
11-
export { CidDocument, SUPPORTED_FILE_TYPES } from './document.js';
11+
export { SUPPORTED_FILE_TYPES } from './document.js';
1212
export { makeValidationResultDocument } from './validation/index.js';
1313
export { keepOutputColumns } from './processing/mapping.js';
1414

1515
export { joinFieldsForHash, cleanValueList, extractAlgoColumnsFromObject } from './hashing/utils.js';
1616
export { BaseHasher } from './hashing/base.js';
1717

18+
export type { CidDocument } from './document.js';
1819
export type { makeHasherFunction } from './hashing/base.js';
1920
export type { Config } from './config/Config.js';
2021
export type { Validation } from './validation/Validation.js';

src/processing/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
import path from 'node:path';
1919
import os from 'node:os';
2020

21-
import { CidDocument, SUPPORTED_FILE_TYPES } from '../document.js';
21+
import { SUPPORTED_FILE_TYPES } from '../document.js';
22+
import type { CidDocument } from '../document.js';
2223

2324
import { encoderForFile } from '../encoding/index.js';
2425
import { decoderForFile, fileTypeOf } from '../decoding/index.js';
@@ -71,16 +72,19 @@ export function validateDocument(
7172
return validateDocumentWithListDict(validatorDict, decoded);
7273
}
7374

74-
export function generateHashesForDocument(
75+
export const generateHashesForDocument = (
7576
hasher: BaseHasher,
7677
document: CidDocument,
77-
) {
78+
): CidDocument => {
7879
// generate for all rows
7980
let rows = document.data.map((row) => {
8081
const generatedHashes = hasher.generateHashForObject(row);
8182
return Object.assign({}, row, generatedHashes);
8283
});
83-
return new CidDocument('hashedDocument', rows);
84+
return {
85+
name: 'hashedDocument',
86+
data: rows
87+
};
8488
}
8589

8690
// helper to output a document with a specific config

src/processing/mapping.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
import Debug from 'debug';
2525
import type { Config } from '../config/Config.js';
26-
import { CidDocument } from '../document.js';
26+
import type { CidDocument } from '../document.js';
2727
const log = Debug('CID:Processing::mapping');
2828

2929
// Returns a list of columns containing both algorithm-required and "always-include" columns

src/validation/index.ts

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
} from './validators/index.js';
3131
import { SUPPORTED_VALIDATORS } from './Validation.js';
3232
import type { Validation, Validator } from './Validation.js';
33-
import { CidDocument } from '../document.js';
33+
import type { CidDocument } from '../document.js';
3434
import type { Config } from '../config/Config.js';
3535

3636
// MAIN VALIDATION
@@ -225,40 +225,42 @@ export function validateDocumentWithListDict(
225225

226226
// Generates a document for output based on the validation results.
227227
// sourceConfig is required to map the original column names in the error messages
228-
export function makeValidationResultDocument(
228+
export const makeValidationResultDocument = (
229229
sourceConfig: Config.Options['source'],
230230
documentResult: Validation.DocumentResult,
231-
) {
231+
): CidDocument => {
232232
let fieldNameMapping = sourceConfig.columns.reduce(
233233
(memo, col) => {
234234
return Object.assign(memo, { [col.alias]: col.name });
235235
},
236236
{} as { [key: string]: string },
237237
);
238238

239-
return new CidDocument(
240-
'validationResult',
241-
documentResult.results.map((rowResult, rowIdx) => {
242-
// build an error message
243-
let errorList = rowResult.errors.map((error) => {
244-
// find the column name
245-
let columnHumanName = fieldNameMapping[error.column] || error.column;
246-
return error.errors
247-
.map(({ message }) => `${columnHumanName} ${message};`)
248-
.join('\n');
249-
});
239+
const documentData: CidDocument["data"] = documentResult.results.map((rowResult, rowIdx) => {
240+
// build an error message
241+
let errorList = rowResult.errors.map((error) => {
242+
// find the column name
243+
let columnHumanName = fieldNameMapping[error.column] || error.column;
244+
return error.errors
245+
.map(({ message }) => `${columnHumanName} ${message};`)
246+
.join('\n');
247+
});
250248

251-
// combine with the row onject
252-
return Object.assign(
253-
{
254-
// The row number should match the row number in the input document (row index 0 is row# 2)
255-
row_number: rowIdx + 2,
256-
// The error list should be an empty string (so that it'll be hidden if no errors are present)
257-
// NOTE: the line-ending can be tricky
258-
errors: errorList.join('\n'),
259-
},
260-
rowResult.row,
261-
);
262-
}),
263-
);
249+
// combine with the row onject
250+
return Object.assign(
251+
{
252+
// The row number should match the row number in the input document (row index 0 is row# 2)
253+
row_number: rowIdx + 2,
254+
// The error list should be an empty string (so that it'll be hidden if no errors are present)
255+
// NOTE: the line-ending can be tricky
256+
errors: errorList.join('\n'),
257+
},
258+
rowResult.row,
259+
);
260+
})
261+
262+
return {
263+
name: 'validationResult',
264+
data: documentData
265+
};
264266
}

tests/encoding/csv.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { fileURLToPath } from 'node:url';
1919
import { join, dirname } from 'node:path';
2020
import { existsSync, unlinkSync, readFileSync } from 'node:fs';
2121
import { makeCsvEncoder } from '../../src/encoding/csv.js';
22-
import { CidDocument } from '../../src/document.js';
22+
import type { CidDocument } from '../../src/document.js';
2323
import type { Config } from '../../src/config/Config.js';
2424

2525
const __dirname = dirname(fileURLToPath(import.meta.url));

tests/processing/mapping.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717

1818
import type { Config } from '../../src/config/Config.js';
19-
import { CidDocument } from '../../src/document.js';
19+
import type { CidDocument } from '../../src/document.js';
2020
import {
2121
mapRequiredColumns,
2222
isMappingOnlyDocument,

tests/validation/index.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,17 @@
1515
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1616
*/
1717

18-
import type { Config } from '../../src/config/Config.js';
1918
import {
2019
makeValidatorListDict,
2120
validateDocumentWithListDict,
2221
makeValidationResultDocument,
2322
} from '../../src/validation/index.js';
2423
import { OptionsValidator } from '../../src/validation/validators/options.js';
2524
import { SUPPORTED_VALIDATORS } from '../../src/validation/Validation.js';
25+
26+
import type { Config } from '../../src/config/Config.js';
2627
import type { Validation } from '../../src/validation/Validation.js';
27-
import { CidDocument } from '../../src/document.js';
28+
import type { CidDocument } from '../../src/document.js';
2829

2930
// get the class name
3031
const className = (obj: object) => obj.constructor.name;

0 commit comments

Comments
 (0)