read-excel-file 8.0.1 → 8.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,104 +1,128 @@
1
1
  import { CellValue } from '../types.d.js'
2
2
 
3
3
  import {
4
- Constructor,
5
- ParseDataValueCustomType,
6
- ParseDataValueType,
4
+ StringType,
5
+ DateType,
6
+ NumberType,
7
+ BooleanType,
7
8
  Integer,
8
9
  Email,
9
- URL
10
+ URL,
11
+ ParseDataCustomType,
12
+ ParseDataValueType
10
13
  } from './parseDataValueType.d.js'
11
14
 
12
- // When `error` is `"required"`, `value` could only be `null` or `undefined`.
13
15
  export interface ParseDataValueRequiredError<
14
- ParseDataValueType = unknown,
15
- ColumnTitle = string
16
+ ColumnTitle extends string = string,
17
+ CustomType extends ParseDataCustomType<unknown> = never
16
18
  > {
17
19
  // row: number;
18
20
  column: ColumnTitle;
19
- type?: ParseDataValueType;
21
+ // `type: undefined` is treated as `type: String`.
22
+ type?: ParseDataValueType<CustomType>;
20
23
  error: 'required';
21
24
  reason: undefined;
25
+ // When `error` is `"required"`, `value` could only be `null` or `undefined`.
26
+ // * `null` means "cell is empty"
27
+ // * `undefined` means "column is missing"
22
28
  value: null | undefined;
23
29
  }
24
30
 
25
- // When `error` is not `"required"`, `value` is known to not be `null` or `undefined`
26
- // because when `value` is `null` or `undefined`, it won't be parsed at all,
27
- // so there can't be any error thrown during parsing phase.
28
31
  interface ParseDataError_<
29
- ParseDataValueType extends ParseDataValueCustomType<unknown> | undefined = ParseDataValueCustomType<unknown>,
30
- ColumnTitle = string,
31
- ErrorMessage extends string = string,
32
- ErrorReason extends string | undefined = string | undefined
32
+ ColumnTitle extends string,
33
+ // ` | undefined` is added to support `parseData()` errors that originate
34
+ // on properties that have no `type` property specified. In such cases,
35
+ // `type` defaults to `String`.
36
+ //
37
+ // One could ask: "Why is then the `type` not simply marked as optional?".
38
+ // The answer is that `type` could only be `undefined` in case of `parseData()` errors
39
+ // that originate from `type: String` parser while other type parsers can't have `type` be `undefined`.
40
+ //
41
+ Type extends ParseDataValueType<unknown> | undefined,
42
+ ErrorMessage extends string,
43
+ ErrorReason extends string | undefined
33
44
  > {
34
45
  // row: number;
35
46
  column: ColumnTitle;
36
- type: ParseDataValueType;
47
+ type: Type;
37
48
  error: ErrorMessage;
38
49
  reason: ErrorReason;
50
+ // When `error` is not `"required"`, `value` is known to not be `null` or `undefined`
51
+ // because when `value` is `null` or `undefined`, it won't be parsed at all,
52
+ // so there can't be any error thrown during parsing phase.
39
53
  value: CellValue;
40
54
  }
41
55
 
42
- export interface ParseDataError<
43
- ParseDataValueType extends ParseDataValueCustomType<unknown> = ParseDataValueCustomType<unknown>,
44
- ColumnTitle = string,
45
- ErrorMessage extends string = string,
46
- ErrorReason extends string | undefined = string | undefined
56
+ export type ParseDataCustomTypeErrorMessage<
57
+ CustomType extends ParseDataCustomType<unknown>
58
+ > = string
59
+
60
+ export type ParseDataCustomTypeErrorReason<
61
+ CustomType extends ParseDataCustomType<unknown>,
62
+ ErrorMessage extends ParseDataCustomTypeErrorMessage<CustomType>
63
+ > = string | undefined
64
+
65
+ // This is just a public export. It's not used internally.
66
+ interface ParseDataErrorCustomType<
67
+ ColumnTitle extends string = string,
68
+ CustomType extends ParseDataCustomType<unknown> = never,
69
+ ErrorMessage extends ParseDataCustomTypeErrorMessage<CustomType> = string,
70
+ ErrorReason extends ParseDataCustomTypeErrorReason<CustomType, ErrorMessage> = string | undefined
47
71
  > extends ParseDataError_<
48
- ParseDataValueType,
49
72
  ColumnTitle,
73
+ CustomType,
50
74
  ErrorMessage,
51
75
  ErrorReason
52
76
  > {}
53
77
 
54
- interface ParseDataErrorNotABoolean<ColumnTitle = string> extends ParseDataError_<
55
- Constructor<Boolean>,
78
+ interface ParseDataErrorNotABoolean<ColumnTitle extends string = string> extends ParseDataError_<
56
79
  ColumnTitle,
80
+ BooleanType,
57
81
  'not_a_boolean',
58
82
  undefined
59
83
  > {
60
84
  value: Exclude<CellValue, boolean>;
61
85
  }
62
86
 
63
- interface ParseDataErrorNotADate<ColumnTitle = string> extends ParseDataError_<
64
- Constructor<Date>,
87
+ interface ParseDataErrorNotADate<ColumnTitle extends string = string> extends ParseDataError_<
65
88
  ColumnTitle,
89
+ DateType,
66
90
  'not_a_date',
67
91
  undefined
68
92
  > {
69
93
  value: Exclude<CellValue, typeof Date | number>;
70
94
  }
71
95
 
72
- interface ParseDataErrorDateOutOfBounds<ColumnTitle = string> extends ParseDataError_<
73
- Constructor<Date>,
96
+ interface ParseDataErrorDateOutOfBounds<ColumnTitle extends string = string> extends ParseDataError_<
74
97
  ColumnTitle,
98
+ DateType,
75
99
  'out_of_bounds',
76
100
  undefined
77
101
  > {
78
102
  value: typeof Date;
79
103
  }
80
104
 
81
- interface ParseDataErrorNotAString<ColumnTitle = string> extends ParseDataError_<
82
- Constructor<String> | undefined,
105
+ interface ParseDataErrorNotAString<ColumnTitle extends string = string> extends ParseDataError_<
83
106
  ColumnTitle,
107
+ StringType | undefined,
84
108
  'not_a_string',
85
109
  undefined
86
110
  > {
87
111
  value: Exclude<CellValue, string | number>;
88
112
  }
89
113
 
90
- interface ParseDataErrorStringInvalidNumber<ColumnTitle = string> extends ParseDataError_<
91
- Constructor<String> | undefined,
114
+ interface ParseDataErrorStringInvalidNumber<ColumnTitle extends string = string> extends ParseDataError_<
92
115
  ColumnTitle,
116
+ StringType | undefined,
93
117
  'invalid_number',
94
118
  undefined
95
119
  > {
96
120
  value: number;
97
121
  }
98
122
 
99
- interface ParseDataErrorStringNumberOutOfBounds<ColumnTitle = string> extends ParseDataError_<
100
- Constructor<String> | undefined,
123
+ interface ParseDataErrorStringNumberOutOfBounds<ColumnTitle extends string = string> extends ParseDataError_<
101
124
  ColumnTitle,
125
+ StringType | undefined,
102
126
  'out_of_bounds',
103
127
  undefined
104
128
  > {
@@ -106,11 +130,11 @@ interface ParseDataErrorStringNumberOutOfBounds<ColumnTitle = string> extends Pa
106
130
  }
107
131
 
108
132
  interface ParseDataErrorNotANumber<
109
- Type extends ParseDataValueCustomType<unknown> | undefined = Constructor<Number>,
110
- ColumnTitle = string
133
+ ColumnTitle extends string = string,
134
+ Type extends ParseDataCustomType<unknown> | undefined = NumberType
111
135
  > extends ParseDataError_<
112
- Type,
113
136
  ColumnTitle,
137
+ Type,
114
138
  'not_a_number',
115
139
  undefined
116
140
  > {
@@ -118,11 +142,11 @@ interface ParseDataErrorNotANumber<
118
142
  }
119
143
 
120
144
  interface ParseDataErrorNotANumberString<
121
- Type extends ParseDataValueCustomType<unknown> | undefined = Constructor<Number>,
122
- ColumnTitle = string
145
+ ColumnTitle extends string = string,
146
+ Type extends ParseDataCustomType<unknown> | undefined = NumberType
123
147
  > extends ParseDataError_<
124
- Type,
125
148
  ColumnTitle,
149
+ Type,
126
150
  'not_a_number',
127
151
  undefined
128
152
  > {
@@ -130,11 +154,11 @@ interface ParseDataErrorNotANumberString<
130
154
  }
131
155
 
132
156
  interface ParseDataErrorNumberInvalid<
133
- Type extends ParseDataValueCustomType<unknown> | undefined = Constructor<Number>,
134
- ColumnTitle = string
157
+ ColumnTitle extends string = string,
158
+ Type extends ParseDataCustomType<unknown> | undefined = NumberType
135
159
  > extends ParseDataError_<
136
- Type,
137
160
  ColumnTitle,
161
+ Type,
138
162
  'invalid_number',
139
163
  undefined
140
164
  > {
@@ -142,80 +166,80 @@ interface ParseDataErrorNumberInvalid<
142
166
  }
143
167
 
144
168
  interface ParseDataErrorNumberOutOfBounds<
145
- Type extends ParseDataValueCustomType<unknown> | undefined = Constructor<Number>,
146
- ColumnTitle = string
169
+ ColumnTitle extends string = string,
170
+ Type extends ParseDataCustomType<unknown> | undefined = NumberType
147
171
  > extends ParseDataError_<
148
- Type,
149
172
  ColumnTitle,
173
+ Type,
150
174
  'out_of_bounds',
151
175
  undefined
152
176
  > {
153
177
  value: number | string;
154
178
  }
155
179
 
156
- type ParseDataBaseValueTypeError<ColumnTitle = string> =
180
+ type ParseDataBaseValueTypeError<ColumnTitle extends string = string> =
157
181
  | ParseDataErrorNotABoolean<ColumnTitle>
158
182
  | ParseDataErrorNotADate<ColumnTitle>
159
183
  | ParseDataErrorDateOutOfBounds<ColumnTitle>
160
184
  | ParseDataErrorNotAString<ColumnTitle>
161
185
  | ParseDataErrorStringInvalidNumber<ColumnTitle>
162
186
  | ParseDataErrorStringNumberOutOfBounds<ColumnTitle>
163
- | ParseDataErrorNotANumber<Constructor<Number>, ColumnTitle>
164
- | ParseDataErrorNotANumberString<Constructor<Number>, ColumnTitle>
165
- | ParseDataErrorNumberInvalid<Constructor<Number>, ColumnTitle>
166
- | ParseDataErrorNumberOutOfBounds<Constructor<Number>, ColumnTitle>;
187
+ | ParseDataErrorNotANumber<ColumnTitle, NumberType>
188
+ | ParseDataErrorNotANumberString<ColumnTitle, NumberType>
189
+ | ParseDataErrorNumberInvalid<ColumnTitle, NumberType>
190
+ | ParseDataErrorNumberOutOfBounds<ColumnTitle, NumberType>;
167
191
 
168
- interface ParseDataErrorNotAnInteger<ColumnTitle = string> extends ParseDataError_<
169
- typeof Integer,
192
+ interface ParseDataErrorNotAnInteger<ColumnTitle extends string = string> extends ParseDataError_<
170
193
  ColumnTitle,
194
+ typeof Integer,
171
195
  'not_an_integer',
172
196
  undefined
173
197
  > {
174
198
  value: number | string;
175
199
  }
176
200
 
177
- interface ParseDataErrorIntegerNotANumber<ColumnTitle = string> extends ParseDataErrorNotANumber<typeof Integer, ColumnTitle> {}
178
- interface ParseDataErrorIntegerNotANumberString<ColumnTitle = string> extends ParseDataErrorNotANumberString<typeof Integer, ColumnTitle> {}
179
- interface ParseDataErrorIntegerNumberInvalid<ColumnTitle = string> extends ParseDataErrorNumberInvalid<typeof Integer, ColumnTitle> {}
180
- interface ParseDataErrorIntegerNumberOutOfBounds<ColumnTitle = string> extends ParseDataErrorNumberOutOfBounds<typeof Integer, ColumnTitle> {}
201
+ interface ParseDataErrorIntegerNotANumber<ColumnTitle extends string = string> extends ParseDataErrorNotANumber<ColumnTitle, typeof Integer> {}
202
+ interface ParseDataErrorIntegerNotANumberString<ColumnTitle extends string = string> extends ParseDataErrorNotANumberString<ColumnTitle, typeof Integer> {}
203
+ interface ParseDataErrorIntegerNumberInvalid<ColumnTitle extends string = string> extends ParseDataErrorNumberInvalid<ColumnTitle, typeof Integer> {}
204
+ interface ParseDataErrorIntegerNumberOutOfBounds<ColumnTitle extends string = string> extends ParseDataErrorNumberOutOfBounds<ColumnTitle, typeof Integer> {}
181
205
 
182
- interface ParseDataErrorNotAUrl<ColumnTitle = string> extends ParseDataError_<
183
- typeof URL,
206
+ interface ParseDataErrorNotAUrl<ColumnTitle extends string = string> extends ParseDataError_<
184
207
  ColumnTitle,
208
+ typeof URL,
185
209
  'not_a_url',
186
210
  undefined
187
211
  > {
188
212
  value: string;
189
213
  }
190
214
 
191
- interface ParseDataErrorUrlNotAString<ColumnTitle = string> extends ParseDataError_<
192
- typeof URL,
215
+ interface ParseDataErrorUrlNotAString<ColumnTitle extends string = string> extends ParseDataError_<
193
216
  ColumnTitle,
217
+ typeof URL,
194
218
  'not_a_string',
195
219
  undefined
196
220
  > {
197
221
  value: Exclude<CellValue, string>;
198
222
  }
199
223
 
200
- interface ParseDataErrorNotAnEmail<ColumnTitle = string> extends ParseDataError_<
201
- typeof Email,
224
+ interface ParseDataErrorNotAnEmail<ColumnTitle extends string = string> extends ParseDataError_<
202
225
  ColumnTitle,
226
+ typeof Email,
203
227
  'not_an_email',
204
228
  undefined
205
229
  > {
206
230
  value: string;
207
231
  }
208
232
 
209
- interface ParseDataErrorEmailNotAString<ColumnTitle = string> extends ParseDataError_<
210
- typeof Email,
233
+ interface ParseDataErrorEmailNotAString<ColumnTitle extends string = string> extends ParseDataError_<
211
234
  ColumnTitle,
235
+ typeof Email,
212
236
  'not_a_string',
213
237
  undefined
214
238
  > {
215
239
  value: Exclude<CellValue, string>;
216
240
  }
217
241
 
218
- type ParseDataAdditionalBuiltInValueTypeError<ColumnTitle = string> =
242
+ type ParseDataAdditionalValueTypeError<ColumnTitle extends string = string> =
219
243
  | ParseDataErrorNotAnInteger<ColumnTitle>
220
244
  | ParseDataErrorIntegerNotANumber<ColumnTitle>
221
245
  | ParseDataErrorIntegerNotANumberString<ColumnTitle>
@@ -226,14 +250,32 @@ type ParseDataAdditionalBuiltInValueTypeError<ColumnTitle = string> =
226
250
  | ParseDataErrorNotAnEmail<ColumnTitle>
227
251
  | ParseDataErrorEmailNotAString<ColumnTitle>;
228
252
 
229
- type ParseDataBuiltInValueTypeError<ColumnTitle = string> =
253
+ type ParseDataBuiltInValueTypeError<ColumnTitle extends string = string> =
230
254
  | ParseDataBaseValueTypeError<ColumnTitle>
231
- | ParseDataAdditionalBuiltInValueTypeError<ColumnTitle>;
255
+ | ParseDataAdditionalValueTypeError<ColumnTitle>;
256
+
257
+ interface ParseDataArrayValueSyntaxError<
258
+ ColumnTitle extends string = string,
259
+ ParseDataCustomType_ extends ParseDataCustomType<unknown> = ParseDataCustomType<unknown>
260
+ > extends ParseDataError_<
261
+ ColumnTitle,
262
+ ParseDataValueType<ParseDataCustomType_>,
263
+ 'invalid',
264
+ 'syntax'
265
+ > {}
232
266
 
233
- export type ParseDataPossibleError<
234
- ParseDataValueCustomType_ extends ParseDataValueCustomType<unknown>,
235
- ColumnTitle = string
267
+ export type ParseDataError<
268
+ ColumnTitle extends string = string,
269
+ CustomType extends ParseDataCustomType<unknown> = never,
270
+ ErrorMessage extends ParseDataCustomTypeErrorMessage<CustomType> = string,
271
+ ErrorReason extends ParseDataCustomTypeErrorReason<CustomType, ErrorMessage> = string | undefined
236
272
  > =
237
273
  | ParseDataBuiltInValueTypeError<ColumnTitle>
238
- | ParseDataValueRequiredError<ParseDataValueType<ParseDataValueCustomType_>, ColumnTitle>
239
- | ParseDataError<ParseDataValueType<ParseDataValueCustomType_>, ColumnTitle>;
274
+ | ParseDataValueRequiredError<ColumnTitle, ParseDataValueType<CustomType>>
275
+ | ParseDataArrayValueSyntaxError<ColumnTitle, ParseDataValueType<CustomType>>
276
+ | ParseDataErrorCustomType<
277
+ ColumnTitle,
278
+ ParseDataValueType<CustomType>,
279
+ ErrorMessage,
280
+ ErrorReason
281
+ >;
@@ -1,4 +1,4 @@
1
- import { ParseDataValueType, ParseDataValueCustomType } from './parseDataValueType.js'
1
+ import { ParseDataValueType, ParseDataCustomType } from './parseDataValueType.js'
2
2
 
3
3
  type SchemaEntryRequiredOrNot<Object> = boolean | ((row: Object) => boolean);
4
4
 
@@ -9,7 +9,7 @@ interface SchemaEntryForValue<
9
9
  ColumnTitle extends string
10
10
  > {
11
11
  column: ColumnTitle;
12
- type?: ParseDataValueType<ParseDataValueCustomType<Object[Key]>>;
12
+ type?: ParseDataValueType<ParseDataCustomType<Object[Key]>>;
13
13
  oneOf?: Object[Key][];
14
14
  required?: SchemaEntryRequiredOrNot<TopLevelObject>;
15
15
  validate?(value: Object[Key]): void;
@@ -15,31 +15,36 @@ export type Constructor<Type> =
15
15
  ? BooleanConstructor
16
16
  : never;
17
17
 
18
+ export type StringType = Constructor<String>;
19
+ export type DateType = Constructor<Date>;
20
+ export type NumberType = Constructor<Number>;
21
+ export type BooleanType = Constructor<Boolean>;
22
+
18
23
  // Parsed value `type` (foundational ones).
19
- type BaseType =
24
+ type ParseDataBaseType =
20
25
  Constructor<String> |
21
26
  Constructor<Date> |
22
27
  Constructor<Number> |
23
28
  Constructor<Boolean>;
24
29
 
25
- // Parsed value `type` (custom one).
26
- // A function that receives a cell `value` and returns a "parsed" value.
27
- // Returning `undefined` will have same effect as returning `null`.
28
- // When cell value is `undefined` or `null`, its `type` is completely ignored (skipped).
29
- export type ParseDataValueCustomType<ParsedValue> = (value: CellValue) => ParsedValue | undefined | null;
30
-
31
30
  // Parsed value `type` (additional built-in ones).
32
31
  export function Integer(value: CellValue): number;
33
32
  export function URL(value: CellValue): string;
34
33
  export function Email(value: CellValue): string;
35
34
 
36
- type AdditionalBuiltInParseDataValueType =
35
+ type ParseDataAdditionalType =
37
36
  | typeof Integer
38
37
  | typeof URL
39
38
  | typeof Email;
40
39
 
40
+ // Parsed value `type` (custom one).
41
+ // A function that receives a cell `value` and returns a "parsed" value.
42
+ // Returning `undefined` will have same effect as returning `null`.
43
+ // When cell value is `undefined` or `null`, its `type` is completely ignored (skipped).
44
+ export type ParseDataCustomType<ParsedValue> = (value: CellValue) => ParsedValue | undefined;
45
+
41
46
  // Schema entry `type`: foundational ones, additional ones, custom ones.
42
- export type ParseDataValueType<ParseDataValueCustomType> =
43
- | BaseType
44
- | AdditionalBuiltInParseDataValueType
45
- | ParseDataValueCustomType;
47
+ export type ParseDataValueType<ParseDataCustomType> =
48
+ | ParseDataBaseType
49
+ | ParseDataAdditionalType
50
+ | ParseDataCustomType;
@@ -18,6 +18,10 @@ import {
18
18
  Schema
19
19
  } from '../types/parseData/parseDataSchema.d.js';
20
20
 
21
+ import {
22
+ ParseDataError
23
+ } from '../types/parseData/parseDataError.d.js';
24
+
21
25
  export {
22
26
  CellValue,
23
27
  Row,
@@ -25,7 +29,12 @@ export {
25
29
  } from '../types/types.d.js';
26
30
 
27
31
  export {
28
- ParseDataValueCustomType as ParseDataValueType,
32
+ ParseDataCustomType,
33
+ // Base `type`s when parsing data.
34
+ StringType as String,
35
+ DateType as Date,
36
+ NumberType as Number,
37
+ BooleanType as Boolean,
29
38
  // Additional built-in `type`s when parsing data.
30
39
  Integer,
31
40
  Email,
@@ -33,6 +42,8 @@ export {
33
42
  } from '../types/parseData/parseDataValueType.d.js';
34
43
 
35
44
  export {
45
+ ParseDataCustomTypeErrorMessage,
46
+ ParseDataCustomTypeErrorReason,
36
47
  ParseDataError,
37
48
  ParseDataValueRequiredError
38
49
  } from '../types/parseData/parseDataError.d.js';
@@ -63,9 +74,10 @@ export function readSheet<ParsedNumber = number>(
63
74
 
64
75
  export function parseData<
65
76
  Object extends object,
66
- ColumnTitle extends string
77
+ ColumnTitle extends string,
78
+ Error extends ParseDataError
67
79
  >(
68
80
  data: SheetData,
69
81
  schema: Schema<Object, ColumnTitle>,
70
82
  options?: ParseDataOptions
71
- ): ParseDataResult<Object>;
83
+ ): ParseDataResult<Object, Error>;
@@ -18,6 +18,10 @@ import {
18
18
  Schema
19
19
  } from '../types/parseData/parseDataSchema.d.js';
20
20
 
21
+ import {
22
+ ParseDataError
23
+ } from '../types/parseData/parseDataError.d.js';
24
+
21
25
  export {
22
26
  CellValue,
23
27
  Row,
@@ -25,7 +29,12 @@ export {
25
29
  } from '../types/types.d.js';
26
30
 
27
31
  export {
28
- ParseDataValueCustomType as ParseDataValueType,
32
+ ParseDataCustomType,
33
+ // Base `type`s when parsing data.
34
+ StringType as String,
35
+ DateType as Date,
36
+ NumberType as Number,
37
+ BooleanType as Boolean,
29
38
  // Additional built-in `type`s when parsing data.
30
39
  Integer,
31
40
  Email,
@@ -33,6 +42,8 @@ export {
33
42
  } from '../types/parseData/parseDataValueType.d.js';
34
43
 
35
44
  export {
45
+ ParseDataCustomTypeErrorMessage,
46
+ ParseDataCustomTypeErrorReason,
36
47
  ParseDataError,
37
48
  ParseDataValueRequiredError
38
49
  } from '../types/parseData/parseDataError.d.js';
@@ -63,9 +74,10 @@ export function readSheet<ParsedNumber = number>(
63
74
 
64
75
  export function parseData<
65
76
  Object extends object,
66
- ColumnTitle extends string
77
+ ColumnTitle extends string,
78
+ Error extends ParseDataError
67
79
  >(
68
80
  data: SheetData,
69
81
  schema: Schema<Object, ColumnTitle>,
70
82
  options?: ParseDataOptions
71
- ): ParseDataResult<Object>;
83
+ ): ParseDataResult<Object, Error>;