Add declarations for 'convert-excel-to-json' (#47788)

* Add declarations for 'convert-excel-to-json'

* remove unnecessarily referenced 'node'
This commit is contained in:
Xun Sun 2020-09-22 03:49:34 +08:00 committed by GitHub
parent 49505736d2
commit 51ba34e7b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 121 additions and 0 deletions

View File

@ -0,0 +1,71 @@
import excelToJson = require("convert-excel-to-json");
import { readFileSync } from "fs";
const sourceFile = "test-data.xlsx";
const sourceBuffer = readFileSync(sourceFile);
// $ExpectError
excelToJson({
header: {
rows: 1
}
});
// $ExpectType any
excelToJson({
sourceFile
}).sheet1[2];
// $ExpectType any[]
excelToJson(`{"sourceFile": "${sourceFile}"}`).sheet1;
// $ExpectType any
excelToJson({
source: sourceBuffer
}).sheet1[0];
// $ExpectType any[]
excelToJson({
sourceFile,
range: "A2:B3",
sheets: ["sheet1", {
name: "sheet2",
header: {
rows: 1
},
columnToKey: {
A: "id",
B: "firstName",
D: "email"
}
}]
}).sheet2;
// $ExpectType { [key: string]: any[]; }
excelToJson({
sourceFile,
sheets: [{
name: "sheet2",
}],
header: {
rows: 1
},
columnToKey: {
A: "{{A1}}",
B: "{{B1}}",
D: "{{D1}}"
}
});
// $ExpectType any[]
excelToJson({
sourceFile,
sheets: [{
name: "sheet3",
}],
header: {
rows: 0
},
includeEmptyLines: false,
sheetStubs: true
}).sheet3;

24
types/convert-excel-to-json/index.d.ts vendored Normal file
View File

@ -0,0 +1,24 @@
// Type definitions for convert-excel-to-json 1.7
// Project: https://github.com/DiegoZoracKy/convert-excel-to-json
// Definitions by: UNIDY2002 <https://github.com/UNIDY2002>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
interface SheetConfig {
header?: { rows: number };
range?: string;
columnToKey?: { [key: string]: string };
includeEmptyLines?: boolean;
sheetStubs?: boolean;
}
declare function excelToJson(
config: ({ sourceFile: string } | { source: string | Buffer }) // Either sourceFile or source should have a value
& { sheets?: ReadonlyArray<(string | (SheetConfig & { name: string }))> } // Nested SheetConfig should be allowed
& SheetConfig // ... or just a simple config for all
| string, // Input can also be a json-string (for cli)
sourceFile?: string, // For cli
): { [key: string]: any[] }; // Using any to provide more flexibility for downstream usages
export = excelToJson;

View File

@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"convert-excel-to-json-tests.ts"
]
}

View File

@ -0,0 +1,3 @@
{
"extends": "dtslint/dt.json"
}