-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfixed-width.d.ts
More file actions
211 lines (204 loc) · 5.35 KB
/
fixed-width.d.ts
File metadata and controls
211 lines (204 loc) · 5.35 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import { Transform } from "node:stream";
export declare class FixedWidthError extends Error {
code: string;
[key: string]: any;
}
export interface Options {
/**
* Allow lines to be longer than the declared fields while parsing.
*
* @default true
*/
allowLongerLines?: boolean;
/**
* Allow lines to be shorter than the declared fields while parsing.
*
* @default false
*/
allowShorterLines?: boolean;
/**
* Encoding for both input or output data.
*
* @default "utf8"
*/
encoding?: string;
/**
* If true, append at the End Of File an "End Of Line" string.
*
* @default true
*/
eof?: boolean;
/**
* String that separe line records (End Of Line).
*/
eol?: string;
/**
* List of fields.
*/
fields: Field[];
/**
* The first line to consider while parsing (inclusive).
* It is a **1-based** integer (`1` is the first line).
*
* @default 1
*/
from?: number;
/**
* Padding value. Must be one char (byte).
*
* @default " "
*/
pad?: string;
/**
* @deprecated Use `allowLongerLines` and `allowShorterLines` options.
*/
relax?: boolean;
/**
* Completely ignore all empty lines. This options does **not** change the
* behaviour of the `allowShorterLines` option.
*
* @default true
*/
skipEmptyLines?: boolean;
/**
* The last line to consider while parsing (inclusive).
* It is a **1-based** integer (`1` is the first line).
*
* @default Infinity
*/
to?: number;
/**
* String trimming for parsed values.
* - `true`: trim the string
* - `false`: do not trim the string
* - `"left"`: trim the left of the string
* - `"right"`: trim the right of the string
* - `"auto"`: guess trim by field's alignment
*
* @default true
*/
trim?: boolean | "left" | "right" | "auto";
}
export interface Field {
/**
* Field value alignment used by the stringifier.
*
* @default "left"
*/
align?: "left" | "right";
/**
* Custom casting applied while parsing.
*/
cast?: (
value: string,
context: { column: number; line: number; width: number }
) => any;
/**
* Custom way of writing the value to the file
*/
stringify?: (
value: any
) => string;
/**
* Field's column number. This is 1-based. First column is 1.
*/
column?: number;
/**
* Field-level padding value. Must be one char (byte).
*/
pad?: string;
/**
* Name (or symbol) of matching property for this field.
*
* When parsing, this is the name of the property where the value will be saved. When serializing, this is the property name to read.
*
* If not specified, the parsed will output an array of values, and the stringifier will expect an array of values as input.
*/
property?: string | Symbol;
/**
* String trimming when parsing.
* - `true`: trim the field's value
* - `false`: do not trim the field's value
* - `"left"`: trim the left of the field's value
* - `"right"`: trim the right of the field's value
* - `"auto"`: guess trim by field's alignment
*
* Defaults to global `trim` option.
*/
trim?: boolean | "left" | "right" | "auto";
/**
* Field width (chars). Required.
*/
width: number;
}
export declare class Parser<T = unknown> {
/**
* Get a [Transform](https://nodejs.org/api/stream.html#class-streamtransform) stream (Node.js).
*/
static stream(options: Options): Transform;
/**
* @constructor
*/
constructor(options: Options);
/**
* Push a chunk of text. Returns an iterable that yields the parsed objects.
*/
write(chunk: string | Buffer): Iterable<T>;
/**
* Returns a final iterable that yields the remaining objects (if any).
*/
end(): Iterable<T>;
}
export declare class Stringifier {
/**
* Get a [Transform](https://nodejs.org/api/stream.html#class-streamtransform) stream (Node.js).
*/
static stream(options: Options): Transform;
/**
* @constructor
*/
constructor(options: Options);
/**
* Push an object to serialize. Returns the serialized text of the passed object, including new line terminators.
*/
write(obj: object): string;
/**
* Close the parsing and returns a final string.
*/
end(): string;
}
/**
* Parse objects from buffer or text.
*
* If the argument is string or buffer, the output will be an array. The whole conversion is performed at the moment and in-memory.
*
* If the argument is some kind of iterable (sync or async), the output will be the same kind of inputted iterable.
*/
export declare function parse<T = unknown>(
input: string | Buffer,
options: Options
): T[];
export declare function parse<T = unknown>(
input: Iterable<string | Buffer>,
options: Options
): Iterable<T>;
export declare function parse<T = unknown>(
input: AsyncIterable<string | Buffer>,
options: Options
): AsyncIterable<T>;
/**
* Stringify objects to text.
*
* If the argument is an array, the output will be a string. The whole conversion is performed at the moment and in-memory.
*
* If the argument is some kind of iterable (sync or async), the output will be the same kind of inputted iterable.
*/
export declare function stringify(input: any[], options: Options): string;
export declare function stringify(
input: Iterable<any>,
options: Options
): Iterable<string>;
export declare function stringify(
input: AsyncIterable<any>,
options: Options
): AsyncIterable<string>;