diff --git a/types/objects-to-csv/index.d.ts b/types/objects-to-csv/index.d.ts new file mode 100644 index 0000000000..71a6e39f8d --- /dev/null +++ b/types/objects-to-csv/index.d.ts @@ -0,0 +1,55 @@ +// Type definitions for objects-to-csv 1.3 +// Project: https://github.com/anton-bot/objects-to-csv#readme +// Definitions by: Tom Plant +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare class ObjectsToCsv { + /** + * Creates a new instance of the object array to csv converter. + */ + constructor(data: object[]); + + /** + * Holds data to be converted. + */ + data: object[]; + + /** + * Saves the CSV file to the specified file. + * @param filename The path and filename of the new CSV file. + * @param options The options for writing to disk. + * @param options.append Whether to append to file. Default is overwrite (false). + * @param options.bom Append the BOM mark so that Excel shows + * @param options.allColumns Whether to check all items for column names or only the first. Default is the first. + * @returns Data converted to a CSV string. + */ + toDisk( + filename: string, + options?: { + append?: boolean; + bom?: boolean; + allColumns?: boolean; + }, + ): Promise; + + /** + * Returns the CSV file as string. + * @param header - If false, omit the first row containing the + * column names. + * @param allColumns - Whether to check all items for column names. + * Uses only the first item if false. + */ + toString(header?: boolean, allColumns?: boolean): Promise; + + /** + * Private method to run the actual conversion of array of objects to CSV data. + * @param data Data to be converted. + * @param header Whether the first line should contain column headers. + * @param allColumns Whether to check all items for column names. + * Uses only the first item if false. + * @returns Data converted to a CSV string. + */ + convert(data: object[], header?: boolean, allColumns?: boolean): Promise; +} + +export = ObjectsToCsv; diff --git a/types/objects-to-csv/objects-to-csv-tests.ts b/types/objects-to-csv/objects-to-csv-tests.ts new file mode 100644 index 0000000000..75e35f94e2 --- /dev/null +++ b/types/objects-to-csv/objects-to-csv-tests.ts @@ -0,0 +1,12 @@ +import ObjectsToCsv = require('objects-to-csv'); + +const SAMPLE_ASCII = [ + { code: 'HK', name: 'Hong Kong' }, + { code: 'KLN', name: 'Kowloon' }, + { code: 'NT', name: 'New Territories' }, +]; + +const ascii = new ObjectsToCsv(SAMPLE_ASCII); // $ExpectType ObjectsToCsv +const asciiFile = ascii.toDisk('name'); // $ExpectType Promise +const asciiString = ascii.toString(); // $ExpectType Promise +const convertAscii = ascii.convert(SAMPLE_ASCII); // $ExpectType Promise diff --git a/types/objects-to-csv/tsconfig.json b/types/objects-to-csv/tsconfig.json new file mode 100644 index 0000000000..9e9d3f713d --- /dev/null +++ b/types/objects-to-csv/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "objects-to-csv-tests.ts" + ] +} diff --git a/types/objects-to-csv/tslint.json b/types/objects-to-csv/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/objects-to-csv/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }