@@ -3,74 +3,120 @@ import dedent from "dedent";
3
3
import { parse } from "../lib/sync.js" ;
4
4
5
5
describe ( "API sync" , function ( ) {
6
- it ( "take a string and return records" , function ( ) {
7
- const records = parse ( "field_1,field_2\nvalue 1,value 2" ) ;
8
- records . should . eql ( [
9
- [ "field_1" , "field_2" ] ,
10
- [ "value 1" , "value 2" ] ,
11
- ] ) ;
12
- } ) ;
6
+ describe ( "content" , function ( ) {
7
+ it ( "take a string and return records" , function ( ) {
8
+ const records = parse ( "field_1,field_2\nvalue 1,value 2" ) ;
9
+ records . should . eql ( [
10
+ [ "field_1" , "field_2" ] ,
11
+ [ "value 1" , "value 2" ] ,
12
+ ] ) ;
13
+ } ) ;
13
14
14
- it ( "take a buffer and return records" , function ( ) {
15
- const records = parse ( Buffer . from ( "field_1,field_2\nvalue 1,value 2" ) ) ;
16
- records . should . eql ( [
17
- [ "field_1" , "field_2" ] ,
18
- [ "value 1" , "value 2" ] ,
19
- ] ) ;
20
- } ) ;
15
+ it ( "take a buffer and return records" , function ( ) {
16
+ const records = parse ( Buffer . from ( "field_1,field_2\nvalue 1,value 2" ) ) ;
17
+ records . should . eql ( [
18
+ [ "field_1" , "field_2" ] ,
19
+ [ "value 1" , "value 2" ] ,
20
+ ] ) ;
21
+ } ) ;
21
22
22
- it ( "take a Uint8Array and return records" , function ( ) {
23
- const records = parse (
24
- new TextEncoder ( ) . encode ( "field_1,field_2\nvalue 1,value 2" ) ,
25
- ) ;
26
- records . should . eql ( [
27
- [ "field_1" , "field_2" ] ,
28
- [ "value 1" , "value 2" ] ,
29
- ] ) ;
23
+ it ( "take a Uint8Array and return records" , function ( ) {
24
+ const records = parse (
25
+ new TextEncoder ( ) . encode ( "field_1,field_2\nvalue 1,value 2" ) ,
26
+ ) ;
27
+ records . should . eql ( [
28
+ [ "field_1" , "field_2" ] ,
29
+ [ "value 1" , "value 2" ] ,
30
+ ] ) ;
31
+ } ) ;
30
32
} ) ;
31
33
32
- it ( "honors columns option" , function ( ) {
33
- const records = parse ( "field_1,field_2\nvalue 1,value 2" , {
34
- columns : true ,
34
+ describe ( "options" , function ( ) {
35
+ it ( "`columns` option without generic" , function ( ) {
36
+ // Parse returns unknown[]
37
+ const records = parse ( "field_1,field_2\nvalue 1,value 2" , {
38
+ columns : true ,
39
+ } ) ;
40
+ records . should . eql ( [ { field_1 : "value 1" , field_2 : "value 2" } ] ) ;
41
+ } ) ;
42
+
43
+ it ( "`columns` option with generic" , function ( ) {
44
+ // Parse returns Record[]
45
+ interface Record {
46
+ field_1 : string ;
47
+ field_2 : string ;
48
+ }
49
+ const records : Record [ ] = parse < Record > (
50
+ "field_1,field_2\nvalue 1,value 2" ,
51
+ {
52
+ columns : true ,
53
+ } ,
54
+ ) ;
55
+ records . should . eql ( [ { field_1 : "value 1" , field_2 : "value 2" } ] ) ;
35
56
} ) ;
36
- records . should . eql ( [ { field_1 : "value 1" , field_2 : "value 2" } ] ) ;
37
- } ) ;
38
57
39
- it ( "honors objname option" , function ( ) {
40
- const records = parse ( "field_1,field_2\nname 1,value 1\nname 2,value 2" , {
41
- objname : "field_1" ,
42
- columns : true ,
58
+ it ( "`columns` and `on_record` options with generic" , function ( ) {
59
+ // Parse returns Record[]
60
+ interface RecordOriginal {
61
+ field_a : string ;
62
+ field_b : string ;
63
+ }
64
+ interface Record {
65
+ field_1 : string ;
66
+ field_2 : string ;
67
+ }
68
+ const records : Record [ ] = parse < Record , RecordOriginal > (
69
+ "field_1,field_2\nvalue 1,value 2" ,
70
+ {
71
+ columns : true ,
72
+ on_record : ( record : RecordOriginal ) => ( {
73
+ field_1 : record . field_a ,
74
+ field_2 : record . field_b ,
75
+ } ) ,
76
+ } ,
77
+ ) ;
78
+ records . should . eql ( [ { field_1 : "value 1" , field_2 : "value 2" } ] ) ;
43
79
} ) ;
44
- records . should . eql ( {
45
- "name 1" : { field_1 : "name 1" , field_2 : "value 1" } ,
46
- "name 2" : { field_1 : "name 2" , field_2 : "value 2" } ,
80
+
81
+ it ( "`objname` option" , function ( ) {
82
+ // Not good, parse returns unknown[]
83
+ const records = parse ( "field_1,field_2\nname 1,value 1\nname 2,value 2" , {
84
+ objname : "field_1" ,
85
+ columns : true ,
86
+ } ) ;
87
+ records . should . eql ( {
88
+ "name 1" : { field_1 : "name 1" , field_2 : "value 1" } ,
89
+ "name 2" : { field_1 : "name 2" , field_2 : "value 2" } ,
90
+ } ) ;
47
91
} ) ;
48
- } ) ;
49
92
50
- it ( "honors to_line" , function ( ) {
51
- const records = parse ( "1\n2\n3\n4" , { to_line : 2 } ) ;
52
- records . should . eql ( [ [ "1" ] , [ "2" ] ] ) ;
93
+ it ( "`to_line` option" , function ( ) {
94
+ const records = parse ( "1\n2\n3\n4" , { to_line : 2 } ) ;
95
+ records . should . eql ( [ [ "1" ] , [ "2" ] ] ) ;
96
+ } ) ;
53
97
} ) ;
54
98
55
- it ( "catch errors" , function ( ) {
56
- try {
57
- parse ( "A,B\nB\nC,K" , { trim : true } ) ;
58
- throw Error ( "Error not catched" ) ;
59
- } catch ( err ) {
60
- if ( ! err ) throw Error ( "Invalid assessment" ) ;
61
- ( err as Error ) . message . should . eql (
62
- "Invalid Record Length: expect 2, got 1 on line 2" ,
63
- ) ;
64
- }
65
- } ) ;
99
+ describe ( "errors" , function ( ) {
100
+ it ( "catch errors" , function ( ) {
101
+ try {
102
+ parse ( "A,B\nB\nC,K" , { trim : true } ) ;
103
+ throw Error ( "Error not catched" ) ;
104
+ } catch ( err ) {
105
+ if ( ! err ) throw Error ( "Invalid assessment" ) ;
106
+ ( err as Error ) . message . should . eql (
107
+ "Invalid Record Length: expect 2, got 1 on line 2" ,
108
+ ) ;
109
+ }
110
+ } ) ;
66
111
67
- it ( "catch err in last line while flushing" , function ( ) {
68
- ( ( ) => {
69
- parse ( dedent `
70
- headerA, headerB
71
- A2, B2
72
- A1, B1, C2, D2
73
- ` ) ;
74
- } ) . should . throw ( "Invalid Record Length: expect 2, got 4 on line 3" ) ;
112
+ it ( "catch err in last line while flushing" , function ( ) {
113
+ ( ( ) => {
114
+ parse ( dedent `
115
+ headerA, headerB
116
+ A2, B2
117
+ A1, B1, C2, D2
118
+ ` ) ;
119
+ } ) . should . throw ( "Invalid Record Length: expect 2, got 4 on line 3" ) ;
120
+ } ) ;
75
121
} ) ;
76
122
} ) ;
0 commit comments