read-excel-file 9.0.1 → 9.0.2
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.
- package/CHANGELOG.md +1 -0
- package/README.md +17 -65
- package/package.json +1 -1
- package/types/parseData/parseData.d.ts +11 -19
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* Refactored `parseData()` function.
|
|
5
5
|
* The result of `parseData()` function is now `{ errors, objects }`. If there're no errors, `errors` will be `undefined`. Otherwise, `errors` will be a non-empty array and `objects` will be `undefined`.
|
|
6
6
|
* Previously the result of `parseData()` function was `[{ errors, object }, ...]`, i.e. the `errors` were split between each particular data row. Now the `errors` are combined for all data rows. The rationale is that it's simpler to handle the result of the function this way.
|
|
7
|
+
* Re-added `row: number` property to the `error` object.
|
|
7
8
|
* In a schema, a nested object is now not allowed to be `required: true`. Otherwise, if a nested object was allowed to be `required: true`, a corresponding `"required"` error would have to include a specific `column` title but a nested object simply doesn't have one.
|
|
8
9
|
|
|
9
10
|
8.0.0 / 11.03.2026
|
package/README.md
CHANGED
|
@@ -70,6 +70,7 @@ Also check out [`write-excel-file`](https://www.npmjs.com/package/write-excel-fi
|
|
|
70
70
|
* Refactored `parseData()` function.
|
|
71
71
|
* The result of `parseData()` function is now `{ errors, objects }`. If there're no errors, `errors` will be `undefined`. Otherwise, `errors` will be a non-empty array and `objects` will be `undefined`.
|
|
72
72
|
* Previously the result of `parseData()` function was `[{ errors, object }, ...]`, i.e. the `errors` were split between each particular data row. Now the `errors` are combined for all data rows. The rationale is that it's simpler to handle the result of the function this way.
|
|
73
|
+
* Re-added `row: number` property to the `error` object.
|
|
73
74
|
* In a schema, a nested object is now not allowed to be `required: true`. Otherwise, if a nested object was allowed to be `required: true`, a corresponding `"required"` error would have to include a specific `column` title but a nested object simply doesn't have one.
|
|
74
75
|
</details>
|
|
75
76
|
|
|
@@ -435,18 +436,19 @@ const schema = {
|
|
|
435
436
|
// Read `data` from an `.xlsx` file
|
|
436
437
|
const data = await readSheet(file)
|
|
437
438
|
|
|
438
|
-
// Parse `data` using
|
|
439
|
-
const
|
|
439
|
+
// Parse `data` using a `schema`
|
|
440
|
+
const { objects, errors } = parseData(data, schema)
|
|
440
441
|
|
|
441
|
-
// There
|
|
442
|
-
|
|
442
|
+
// There have been no errors when parsing the sheet data, so `errors` is `undefined`.
|
|
443
|
+
// Should there have been any errors when parsing the sheet data, `errors` would've been
|
|
444
|
+
// an array of items having shape: `{ row, column, error, reason?, value?, type? }`.
|
|
445
|
+
errors === undefined
|
|
443
446
|
|
|
444
|
-
// There
|
|
445
|
-
|
|
446
|
-
// with items having shape: `{ column, error, reason?, value?, type? }`.
|
|
447
|
-
results[0].errors === undefined
|
|
447
|
+
// There's one data row in the `.xlsx` file.
|
|
448
|
+
objects.length === 1
|
|
448
449
|
|
|
449
|
-
|
|
450
|
+
// The parsed data row.
|
|
451
|
+
objects[0] === {
|
|
450
452
|
startDate: new Date(Date.UTC(2018, 3 - 1, 24)),
|
|
451
453
|
seats: 10,
|
|
452
454
|
status: 'SCHEDULED',
|
|
@@ -469,37 +471,6 @@ function PhoneNumber(value) {
|
|
|
469
471
|
}
|
|
470
472
|
```
|
|
471
473
|
|
|
472
|
-
An example of how an application could handle the `results`:
|
|
473
|
-
|
|
474
|
-
```js
|
|
475
|
-
const errors = []
|
|
476
|
-
const objects = []
|
|
477
|
-
|
|
478
|
-
// If this code was written in TypeScript, `errors` and `objects` would've been declared as:
|
|
479
|
-
// const errors: { error: ParseDataError, row: number }[] = []
|
|
480
|
-
// const objects: Object[] = []
|
|
481
|
-
|
|
482
|
-
let row = 1
|
|
483
|
-
for (const { errors: errorsInRow, object } of results) {
|
|
484
|
-
if (errorsInRow) {
|
|
485
|
-
for (const error of errorsInRow) {
|
|
486
|
-
errors.push({ error, row })
|
|
487
|
-
}
|
|
488
|
-
} else {
|
|
489
|
-
objects.push(object)
|
|
490
|
-
}
|
|
491
|
-
row++
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
if (errors.length > 0) {
|
|
495
|
-
for (const { error, row } of errors) {
|
|
496
|
-
console.error('Error in data row', row, 'column', error.column, ':', error.error, error.reason || '')
|
|
497
|
-
}
|
|
498
|
-
} else {
|
|
499
|
-
console.log('Objects', objects)
|
|
500
|
-
}
|
|
501
|
-
```
|
|
502
|
-
|
|
503
474
|
<details>
|
|
504
475
|
<summary>An example of defining a <strong>custom <code>type</code></strong> in <strong>TypeScript</strong></summary>
|
|
505
476
|
|
|
@@ -562,33 +533,14 @@ const schema: Schema<Object, ColumnTitle> = {
|
|
|
562
533
|
}
|
|
563
534
|
}
|
|
564
535
|
|
|
565
|
-
const
|
|
536
|
+
const { objects, errors } = parseData<Object, ColumnTitle, PossibleError>([
|
|
566
537
|
['COLUMN TITLE 1', 'COLUMN TITLE 2'],
|
|
567
538
|
['Value 1', 'Value 2']
|
|
568
539
|
], schema)
|
|
569
540
|
|
|
570
|
-
|
|
571
|
-
error
|
|
572
|
-
|
|
573
|
-
}[] = []
|
|
574
|
-
|
|
575
|
-
const objects: Object[] = []
|
|
576
|
-
|
|
577
|
-
let row = 1
|
|
578
|
-
for (const { errors: errorsInRow, object } of results) {
|
|
579
|
-
if (errorsInRow) {
|
|
580
|
-
for (const error of errorsInRow) {
|
|
581
|
-
errors.push({ error, row })
|
|
582
|
-
}
|
|
583
|
-
} else {
|
|
584
|
-
objects.push(object)
|
|
585
|
-
}
|
|
586
|
-
row++
|
|
587
|
-
}
|
|
588
|
-
|
|
589
|
-
if (errors.length > 0) {
|
|
590
|
-
for (const { error, row } of errors) {
|
|
591
|
-
console.error('Error in data row', row, 'column', error.column, ':', error.error, error.reason || '')
|
|
541
|
+
if (errors) {
|
|
542
|
+
for (const error of errors) {
|
|
543
|
+
console.error('Error in data row', error.row, 'column', error.column, ':', error.error, error.reason || '')
|
|
592
544
|
}
|
|
593
545
|
} else {
|
|
594
546
|
console.log('Objects', objects)
|
|
@@ -629,9 +581,9 @@ function ErrorItem({ error: errorDetails }) {
|
|
|
629
581
|
<code>{stringifyValue(value)}</code>
|
|
630
582
|
{' in column '}
|
|
631
583
|
<code>"{column}"</code>
|
|
632
|
-
{' in row '}
|
|
584
|
+
{' in data row '}
|
|
633
585
|
<code>{row}</code>
|
|
634
|
-
{' of spreadsheet'}
|
|
586
|
+
{' of the spreadsheet'}
|
|
635
587
|
</div>
|
|
636
588
|
)
|
|
637
589
|
}
|
package/package.json
CHANGED
|
@@ -1,32 +1,24 @@
|
|
|
1
1
|
import { ParseDataError } from './parseDataError.d.js'
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
Object
|
|
5
|
-
Error extends ParseDataError = ParseDataError
|
|
6
|
-
> = ParseDataResultItem<
|
|
7
|
-
Object,
|
|
8
|
-
Error
|
|
9
|
-
>[]
|
|
10
|
-
|
|
11
|
-
type ParseDataResultItem<
|
|
12
|
-
Object,
|
|
13
|
-
Error extends ParseDataError
|
|
14
|
-
> =
|
|
15
|
-
| ParseDataResultItemSuccess<Object>
|
|
16
|
-
| ParseDataResultItemError<Error>
|
|
17
|
-
|
|
18
|
-
interface ParseDataResultItemSuccess<Object> {
|
|
19
|
-
object: Object;
|
|
3
|
+
interface ParseDataResultSuccess<Object> {
|
|
4
|
+
objects: Object[];
|
|
20
5
|
errors: undefined;
|
|
21
6
|
}
|
|
22
7
|
|
|
23
|
-
interface
|
|
8
|
+
interface ParseDataResultError<
|
|
24
9
|
Error extends ParseDataError
|
|
25
10
|
> {
|
|
26
|
-
|
|
11
|
+
objects: undefined;
|
|
27
12
|
errors: Error[];
|
|
28
13
|
}
|
|
29
14
|
|
|
15
|
+
export type ParseDataResult<
|
|
16
|
+
Object,
|
|
17
|
+
Error extends ParseDataError = ParseDataError
|
|
18
|
+
> =
|
|
19
|
+
| ParseDataResultSuccess<Object>
|
|
20
|
+
| ParseDataResultError<Error>
|
|
21
|
+
|
|
30
22
|
export interface ParseDataOptions {
|
|
31
23
|
propertyValueWhenColumnIsMissing?: any;
|
|
32
24
|
propertyValueWhenCellIsEmpty?: any;
|