-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathtypes.d.ts
119 lines (100 loc) · 4 KB
/
types.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
export function Integer(): void;
export function URL(): void;
export function Email(): void;
export type CellValue = string | number | boolean | typeof Date
export type Row = CellValue[]
type BasicType =
| string
| number
| boolean
| typeof Date
| typeof Integer
| typeof URL
| typeof Email;
// A cell "type" is a function that receives a "raw" value and returns a "parsed" value or `undefined`.
export type Type<ParsedValue> = (value: CellValue) => ParsedValue | undefined;
type SchemaEntryRequiredProperty<Object> = boolean | ((row: Object) => boolean);
interface SchemaEntryForValue<Key extends keyof Object, Object, TopLevelObject> {
prop: Key;
type?: BasicType | Type<Object[Key]>;
oneOf?: Object[Key][];
required?: SchemaEntryRequiredProperty<TopLevelObject>;
validate?(value: Object[Key]): void;
}
// Legacy versions of this library supported supplying a custom `parse()` function.
// Since then, the `parse()` function has been renamed to `type()` function.
interface SchemaEntryForValueLegacy<Key extends keyof Object, Object, TopLevelObject> {
prop: Key;
parse: (value: CellValue) => Object[Key] | undefined;
oneOf?: Object[Key][];
required?: SchemaEntryRequiredProperty<TopLevelObject>;
validate?(value: Object[Key]): void;
}
// Implementing recursive types in TypeScript:
// https://dev.to/busypeoples/notes-on-typescript-recursive-types-and-immutability-5ck1
interface SchemaEntryRecursive<Key extends keyof Object, Object, TopLevelObject, ColumnTitle extends string> {
prop: Key;
type: Record<ColumnTitle, SchemaEntry<keyof Object[Key], Object[Key], TopLevelObject, ColumnTitle>>;
required?: SchemaEntryRequiredProperty<TopLevelObject>;
}
type SchemaEntry<Key extends keyof Object, Object, TopLevelObject, ColumnTitle extends string> =
SchemaEntryForValue<Key, Object, TopLevelObject> |
SchemaEntryForValueLegacy<Key, Object, TopLevelObject> |
SchemaEntryRecursive<Key, Object, TopLevelObject, ColumnTitle>
export type Schema<Object = Record<string, any>, ColumnTitle extends string = string> = Record<ColumnTitle, SchemaEntry<keyof Object, Object, Object, ColumnTitle>>
export interface Error<CellValue_ = CellValue, ParsedValue = any> {
error: string;
reason?: string;
row: number;
column: string;
value?: CellValue_;
type?: Type<ParsedValue>;
}
export interface ParsedObjectsResult<Object> {
rows: Object[];
errors: Error[];
}
interface ParseCommonOptions {
sheet?: number | string;
trim?: boolean;
parseNumber?: (string: string) => any;
}
export interface ParseWithSchemaOptions<Object> extends ParseCommonOptions, MappingParametersReadExcelFile {
schema: Schema<Object>;
transformData?: (rows: Row[]) => Row[];
ignoreEmptyRows?: boolean;
// `includeNullValues: true` parameter is deprecated.
// It could be replaced with the following combination of parameters:
// * `schemaPropertyValueForMissingColumn: null`
// * `schemaPropertyValueForEmptyCell: null`
// * `getEmptyObjectValue = () => null`
includeNullValues?: boolean;
}
type MapProperty = string;
type MapObject = {
[key: string]: MapProperty | MapObject;
};
type Map = MapObject;
export interface ParseWithMapOptions extends ParseCommonOptions {
map: Map;
transformData?: (rows: Row[]) => Row[];
dateFormat?: string;
}
export interface ParseWithoutSchemaOptions extends ParseCommonOptions {
dateFormat?: string;
}
interface MappingParametersCommon {
schemaPropertyValueForMissingColumn?: any;
schemaPropertyShouldSkipRequiredValidationForMissingColumn?(column: string, parameters: { object: Record<string, any> }): boolean;
getEmptyObjectValue?(object: Record<string, any>, parameters: { path?: string }): any;
getEmptyArrayValue?(array: any[], parameters: { path: string }): any;
}
interface MappingParametersReadExcelFile extends MappingParametersCommon {
schemaPropertyValueForEmptyCell?: null | undefined;
}
export interface MappingParameters extends MappingParametersCommon {
schemaPropertyValueForUndefinedCellValue?: any;
schemaPropertyValueForNullCellValue?: any;
isColumnOriented?: boolean;
rowIndexMap?: Record<string, number>;
}