@loaders.gl/netcdf 4.0.0-alpha.5 → 4.0.0-alpha.7
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/dist/es5/index.js +20 -0
- package/dist/es5/index.js.map +1 -0
- package/dist/es5/iobuffer/iobuffer.js +369 -0
- package/dist/es5/iobuffer/iobuffer.js.map +1 -0
- package/dist/es5/netcdf-loader.js +81 -0
- package/dist/es5/netcdf-loader.js.map +1 -0
- package/dist/es5/netcdfjs/netcdf-reader.js +173 -0
- package/dist/es5/netcdfjs/netcdf-reader.js.map +1 -0
- package/dist/es5/netcdfjs/netcdf-types.js +2 -0
- package/dist/es5/netcdfjs/netcdf-types.js.map +1 -0
- package/dist/es5/netcdfjs/read-data.js +31 -0
- package/dist/es5/netcdfjs/read-data.js.map +1 -0
- package/dist/es5/netcdfjs/read-header.js +168 -0
- package/dist/es5/netcdfjs/read-header.js.map +1 -0
- package/dist/es5/netcdfjs/read-type.js +108 -0
- package/dist/es5/netcdfjs/read-type.js.map +1 -0
- package/dist/esm/index.js +3 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/iobuffer/iobuffer.js +270 -0
- package/dist/esm/iobuffer/iobuffer.js.map +1 -0
- package/dist/esm/netcdf-loader.js +38 -0
- package/dist/esm/netcdf-loader.js.map +1 -0
- package/dist/esm/netcdfjs/LICENSE +24 -0
- package/dist/esm/netcdfjs/netcdf-reader.js +104 -0
- package/dist/esm/netcdfjs/netcdf-reader.js.map +1 -0
- package/dist/esm/netcdfjs/netcdf-types.js +2 -0
- package/dist/esm/netcdfjs/netcdf-types.js.map +1 -0
- package/dist/esm/netcdfjs/read-data.js +24 -0
- package/dist/esm/netcdfjs/read-data.js.map +1 -0
- package/dist/esm/netcdfjs/read-header.js +161 -0
- package/dist/esm/netcdfjs/read-header.js.map +1 -0
- package/dist/esm/netcdfjs/read-type.js +97 -0
- package/dist/esm/netcdfjs/read-type.js.map +1 -0
- package/dist/index.js +7 -3
- package/dist/iobuffer/iobuffer.d.ts +1 -1
- package/dist/iobuffer/iobuffer.d.ts.map +1 -1
- package/dist/iobuffer/iobuffer.js +425 -370
- package/dist/netcdf-loader.d.ts +2 -2
- package/dist/netcdf-loader.d.ts.map +1 -1
- package/dist/netcdf-loader.js +46 -36
- package/dist/netcdfjs/netcdf-reader.js +155 -126
- package/dist/netcdfjs/netcdf-types.d.ts +5 -5
- package/dist/netcdfjs/netcdf-types.d.ts.map +1 -1
- package/dist/netcdfjs/netcdf-types.js +2 -2
- package/dist/netcdfjs/read-data.js +47 -26
- package/dist/netcdfjs/read-header.js +206 -173
- package/dist/netcdfjs/read-type.js +130 -117
- package/package.json +5 -5
- package/dist/index.js.map +0 -1
- package/dist/iobuffer/iobuffer.js.map +0 -1
- package/dist/netcdf-loader.js.map +0 -1
- package/dist/netcdfjs/netcdf-reader.js.map +0 -1
- package/dist/netcdfjs/netcdf-types.js.map +0 -1
- package/dist/netcdfjs/read-data.js.map +0 -1
- package/dist/netcdfjs/read-header.js.map +0 -1
- package/dist/netcdfjs/read-type.js.map +0 -1
- /package/dist/{netcdfjs → es5/netcdfjs}/LICENSE +0 -0
|
@@ -1,197 +1,230 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.readName = exports.readNetCDFHeader = void 0;
|
|
4
|
+
const read_type_1 = require("./read-type");
|
|
5
|
+
// Grammar constants
|
|
2
6
|
const ZERO = 0;
|
|
3
7
|
const NC_DIMENSION = 10;
|
|
4
8
|
const NC_VARIABLE = 11;
|
|
5
9
|
const NC_ATTRIBUTE = 12;
|
|
6
10
|
const NC_UNLIMITED = 0;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
11
|
+
/**
|
|
12
|
+
* Read the header of the file
|
|
13
|
+
* @param buffer - Buffer for the file data
|
|
14
|
+
* @param version - Version of the file
|
|
15
|
+
* @return - Header
|
|
16
|
+
*/
|
|
17
|
+
function readNetCDFHeader(buffer, version) {
|
|
18
|
+
// Length of record dimension
|
|
19
|
+
// sum of the varSize's of all the record variables.
|
|
20
|
+
const recordDimensionLength = buffer.readUint32();
|
|
21
|
+
// List of dimensions
|
|
22
|
+
const dimList = readDimensionsList(buffer);
|
|
23
|
+
// List of global attributes
|
|
24
|
+
const attributes = readAttributesList(buffer);
|
|
25
|
+
// List of variables
|
|
26
|
+
const variableList = readVariablesList(buffer, dimList.recordId, version);
|
|
27
|
+
const header = {
|
|
28
|
+
version,
|
|
29
|
+
recordDimension: {
|
|
30
|
+
length: recordDimensionLength,
|
|
31
|
+
id: dimList.recordId,
|
|
32
|
+
name: dimList.recordName,
|
|
33
|
+
recordStep: variableList.recordStep
|
|
34
|
+
},
|
|
35
|
+
dimensions: dimList.dimensions,
|
|
36
|
+
variables: variableList.variables,
|
|
37
|
+
attributes
|
|
38
|
+
};
|
|
39
|
+
return header;
|
|
25
40
|
}
|
|
26
|
-
|
|
41
|
+
exports.readNetCDFHeader = readNetCDFHeader;
|
|
42
|
+
/**
|
|
43
|
+
* Read list of dimensions
|
|
44
|
+
* @ignore
|
|
45
|
+
* @param {IOBuffer} buffer - Buffer for the file data
|
|
46
|
+
*/
|
|
27
47
|
function readDimensionsList(buffer) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
48
|
+
const dimList = buffer.readUint32();
|
|
49
|
+
if (dimList === ZERO) {
|
|
50
|
+
if (buffer.readUint32() !== ZERO) {
|
|
51
|
+
throw new Error('NetCDF: wrong empty tag for list of dimensions');
|
|
52
|
+
}
|
|
53
|
+
// TODO - is this empty dimension list supported / recoverable?
|
|
54
|
+
return {
|
|
55
|
+
recordId: 0,
|
|
56
|
+
recordName: '',
|
|
57
|
+
dimensions: []
|
|
58
|
+
};
|
|
33
59
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
recordId = dim;
|
|
57
|
-
recordName = name;
|
|
60
|
+
if (dimList !== NC_DIMENSION) {
|
|
61
|
+
throw new Error('NetCDF: wrong tag for list of dimensions');
|
|
62
|
+
}
|
|
63
|
+
// Length of dimensions
|
|
64
|
+
const dimensionSize = buffer.readUint32();
|
|
65
|
+
const dimensions = new Array(dimensionSize);
|
|
66
|
+
let recordId;
|
|
67
|
+
let recordName;
|
|
68
|
+
for (let dim = 0; dim < dimensionSize; dim++) {
|
|
69
|
+
// Read name
|
|
70
|
+
const name = readName(buffer);
|
|
71
|
+
// Read dimension size
|
|
72
|
+
const size = buffer.readUint32();
|
|
73
|
+
if (size === NC_UNLIMITED) {
|
|
74
|
+
// in netcdf 3 one field can be of size unlimmited
|
|
75
|
+
recordId = dim;
|
|
76
|
+
recordName = name;
|
|
77
|
+
}
|
|
78
|
+
dimensions[dim] = {
|
|
79
|
+
name,
|
|
80
|
+
size
|
|
81
|
+
};
|
|
58
82
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
83
|
+
return {
|
|
84
|
+
dimensions,
|
|
85
|
+
recordId,
|
|
86
|
+
recordName
|
|
63
87
|
};
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
return {
|
|
67
|
-
dimensions,
|
|
68
|
-
recordId,
|
|
69
|
-
recordName
|
|
70
|
-
};
|
|
71
88
|
}
|
|
72
|
-
|
|
89
|
+
/**
|
|
90
|
+
* List of attributes
|
|
91
|
+
* @ignore
|
|
92
|
+
* @param buffer - Buffer for the file data
|
|
93
|
+
* @return List of attributes with:
|
|
94
|
+
*/
|
|
73
95
|
function readAttributesList(buffer) {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
96
|
+
const gAttList = buffer.readUint32();
|
|
97
|
+
if (gAttList === ZERO) {
|
|
98
|
+
if (buffer.readUint32() !== ZERO) {
|
|
99
|
+
throw new Error('NetCDF: wrong empty tag for list of attributes');
|
|
100
|
+
}
|
|
101
|
+
return [];
|
|
79
102
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
if (gAttList !== NC_ATTRIBUTE) {
|
|
85
|
-
throw new Error('NetCDF: wrong tag for list of attributes');
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
const attributeSize = buffer.readUint32();
|
|
89
|
-
const attributes = new Array(attributeSize);
|
|
90
|
-
|
|
91
|
-
for (let gAtt = 0; gAtt < attributeSize; gAtt++) {
|
|
92
|
-
const name = readName(buffer);
|
|
93
|
-
const type = buffer.readUint32();
|
|
94
|
-
|
|
95
|
-
if (type < 1 || type > 6) {
|
|
96
|
-
throw new Error("NetCDF: non valid type ".concat(type));
|
|
103
|
+
if (gAttList !== NC_ATTRIBUTE) {
|
|
104
|
+
throw new Error('NetCDF: wrong tag for list of attributes');
|
|
97
105
|
}
|
|
98
|
-
|
|
99
|
-
const
|
|
100
|
-
const
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
106
|
+
// Length of attributes
|
|
107
|
+
const attributeSize = buffer.readUint32();
|
|
108
|
+
const attributes = new Array(attributeSize);
|
|
109
|
+
for (let gAtt = 0; gAtt < attributeSize; gAtt++) {
|
|
110
|
+
// Read name
|
|
111
|
+
const name = readName(buffer);
|
|
112
|
+
// Read type
|
|
113
|
+
const type = buffer.readUint32();
|
|
114
|
+
if (type < 1 || type > 6) {
|
|
115
|
+
throw new Error(`NetCDF: non valid type ${type}`);
|
|
116
|
+
}
|
|
117
|
+
// Read attribute
|
|
118
|
+
const size = buffer.readUint32();
|
|
119
|
+
const value = (0, read_type_1.readType)(buffer, type, size);
|
|
120
|
+
// Apply padding
|
|
121
|
+
padding(buffer);
|
|
122
|
+
attributes[gAtt] = {
|
|
123
|
+
name,
|
|
124
|
+
type: (0, read_type_1.num2str)(type),
|
|
125
|
+
value
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
return attributes;
|
|
110
129
|
}
|
|
111
|
-
|
|
130
|
+
/**
|
|
131
|
+
* List of variables
|
|
132
|
+
* @param buffer - Buffer for the file data
|
|
133
|
+
* @param recordId - Id of the unlimited dimension (also called record dimension)
|
|
134
|
+
* This value may be undefined if there is no unlimited dimension
|
|
135
|
+
* @param {number} version - Version of the file
|
|
136
|
+
*/
|
|
137
|
+
// eslint-disable-next-line max-statements, complexity
|
|
112
138
|
function readVariablesList(buffer, recordId, version) {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
139
|
+
const varList = buffer.readUint32();
|
|
140
|
+
let recordStep = 0;
|
|
141
|
+
if (varList === ZERO) {
|
|
142
|
+
if (buffer.readUint32() !== ZERO) {
|
|
143
|
+
throw new Error('NetCDF: wrong empty tag for list of variables');
|
|
144
|
+
}
|
|
145
|
+
return {
|
|
146
|
+
recordStep,
|
|
147
|
+
variables: []
|
|
148
|
+
};
|
|
119
149
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
recordStep,
|
|
123
|
-
variables: []
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
if (varList !== NC_VARIABLE) {
|
|
128
|
-
throw new Error('NetCDF: wrong tag for list of variables');
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
const variableSize = buffer.readUint32();
|
|
132
|
-
const variables = new Array(variableSize);
|
|
133
|
-
|
|
134
|
-
for (let v = 0; v < variableSize; v++) {
|
|
135
|
-
const name = readName(buffer);
|
|
136
|
-
const dimensionality = buffer.readUint32();
|
|
137
|
-
const dimensionsIds = new Array(dimensionality);
|
|
138
|
-
|
|
139
|
-
for (let dim = 0; dim < dimensionality; dim++) {
|
|
140
|
-
dimensionsIds[dim] = buffer.readUint32();
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
const attributes = readAttributesList(buffer);
|
|
144
|
-
const type = buffer.readUint32();
|
|
145
|
-
|
|
146
|
-
if (type < 1 && type > 6) {
|
|
147
|
-
throw new Error("NetCDF: non valid type ".concat(type));
|
|
150
|
+
if (varList !== NC_VARIABLE) {
|
|
151
|
+
throw new Error('NetCDF: wrong tag for list of variables');
|
|
148
152
|
}
|
|
149
|
-
|
|
150
|
-
const
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
153
|
+
// Length of variables
|
|
154
|
+
const variableSize = buffer.readUint32();
|
|
155
|
+
const variables = new Array(variableSize);
|
|
156
|
+
for (let v = 0; v < variableSize; v++) {
|
|
157
|
+
// Read name
|
|
158
|
+
const name = readName(buffer);
|
|
159
|
+
// Read dimensionality of the variable
|
|
160
|
+
const dimensionality = buffer.readUint32();
|
|
161
|
+
// Index into the list of dimensions
|
|
162
|
+
const dimensionsIds = new Array(dimensionality);
|
|
163
|
+
for (let dim = 0; dim < dimensionality; dim++) {
|
|
164
|
+
dimensionsIds[dim] = buffer.readUint32();
|
|
165
|
+
}
|
|
166
|
+
// Read variables size
|
|
167
|
+
const attributes = readAttributesList(buffer);
|
|
168
|
+
// Read type
|
|
169
|
+
const type = buffer.readUint32();
|
|
170
|
+
if (type < 1 && type > 6) {
|
|
171
|
+
throw new Error(`NetCDF: non valid type ${type}`);
|
|
172
|
+
}
|
|
173
|
+
// Read variable size
|
|
174
|
+
// The 32-bit varSize field is not large enough to contain the size of variables that require
|
|
175
|
+
// more than 2^32 - 4 bytes, so 2^32 - 1 is used in the varSize field for such variables.
|
|
176
|
+
const varSize = buffer.readUint32();
|
|
177
|
+
// Read offset
|
|
178
|
+
let offset = buffer.readUint32();
|
|
179
|
+
if (version === 2) {
|
|
180
|
+
if (offset > 0) {
|
|
181
|
+
throw new Error('NetCDF: offsets larger than 4GB not supported');
|
|
182
|
+
}
|
|
183
|
+
offset = buffer.readUint32();
|
|
184
|
+
}
|
|
185
|
+
let record = false;
|
|
186
|
+
// Count amount of record variables
|
|
187
|
+
if (typeof recordId !== 'undefined' && dimensionsIds[0] === recordId) {
|
|
188
|
+
recordStep += varSize;
|
|
189
|
+
record = true;
|
|
190
|
+
}
|
|
191
|
+
variables[v] = {
|
|
192
|
+
name,
|
|
193
|
+
dimensions: dimensionsIds,
|
|
194
|
+
attributes,
|
|
195
|
+
type: (0, read_type_1.num2str)(type),
|
|
196
|
+
size: varSize,
|
|
197
|
+
offset,
|
|
198
|
+
record
|
|
199
|
+
};
|
|
159
200
|
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
if (typeof recordId !== 'undefined' && dimensionsIds[0] === recordId) {
|
|
164
|
-
recordStep += varSize;
|
|
165
|
-
record = true;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
variables[v] = {
|
|
169
|
-
name,
|
|
170
|
-
dimensions: dimensionsIds,
|
|
171
|
-
attributes,
|
|
172
|
-
type: num2str(type),
|
|
173
|
-
size: varSize,
|
|
174
|
-
offset,
|
|
175
|
-
record
|
|
201
|
+
return {
|
|
202
|
+
variables,
|
|
203
|
+
recordStep
|
|
176
204
|
};
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
return {
|
|
180
|
-
variables,
|
|
181
|
-
recordStep
|
|
182
|
-
};
|
|
183
205
|
}
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
206
|
+
// HELPERS
|
|
207
|
+
/**
|
|
208
|
+
* Reads the name
|
|
209
|
+
* @param buffer - Buffer for the file data
|
|
210
|
+
* @return Name
|
|
211
|
+
*/
|
|
212
|
+
function readName(buffer) {
|
|
213
|
+
// Read name
|
|
214
|
+
const nameLength = buffer.readUint32();
|
|
215
|
+
const name = buffer.readChars(nameLength);
|
|
216
|
+
// validate name
|
|
217
|
+
// TODO
|
|
218
|
+
// Apply padding
|
|
219
|
+
padding(buffer);
|
|
220
|
+
return name;
|
|
190
221
|
}
|
|
191
|
-
|
|
222
|
+
exports.readName = readName;
|
|
223
|
+
/**
|
|
224
|
+
* Moves 1, 2, or 3 bytes to next 4-byte boundary
|
|
225
|
+
*/
|
|
192
226
|
function padding(buffer) {
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
227
|
+
if (buffer.offset % 4 !== 0) {
|
|
228
|
+
buffer.skip(4 - (buffer.offset % 4));
|
|
229
|
+
}
|
|
196
230
|
}
|
|
197
|
-
//# sourceMappingURL=read-header.js.map
|
|
@@ -1,127 +1,140 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.str2num = exports.num2bytes = exports.num2str = exports.readType = exports.TYPES = void 0;
|
|
4
|
+
exports.TYPES = {
|
|
5
|
+
BYTE: 1,
|
|
6
|
+
CHAR: 2,
|
|
7
|
+
SHORT: 3,
|
|
8
|
+
INT: 4,
|
|
9
|
+
FLOAT: 5,
|
|
10
|
+
DOUBLE: 6
|
|
8
11
|
};
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
12
|
+
/**
|
|
13
|
+
* Given a type and a size reads the next element
|
|
14
|
+
* @param buffer - Buffer for the file data
|
|
15
|
+
* @param type - Type of the data to read
|
|
16
|
+
* @param size - Size of the element to read
|
|
17
|
+
* @return
|
|
18
|
+
*/
|
|
19
|
+
function readType(buffer, type, size) {
|
|
20
|
+
switch (type) {
|
|
21
|
+
case exports.TYPES.BYTE:
|
|
22
|
+
return buffer.readBytes(size);
|
|
23
|
+
case exports.TYPES.CHAR:
|
|
24
|
+
return trimNull(buffer.readChars(size));
|
|
25
|
+
case exports.TYPES.SHORT:
|
|
26
|
+
return readNumber(size, buffer.readInt16.bind(buffer));
|
|
27
|
+
case exports.TYPES.INT:
|
|
28
|
+
return readNumber(size, buffer.readInt32.bind(buffer));
|
|
29
|
+
case exports.TYPES.FLOAT:
|
|
30
|
+
return readNumber(size, buffer.readFloat32.bind(buffer));
|
|
31
|
+
case exports.TYPES.DOUBLE:
|
|
32
|
+
return readNumber(size, buffer.readFloat64.bind(buffer));
|
|
33
|
+
/* istanbul ignore next */
|
|
34
|
+
default:
|
|
35
|
+
throw new Error(`NetCDF: non valid type ${type}`);
|
|
36
|
+
}
|
|
32
37
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
38
|
+
exports.readType = readType;
|
|
39
|
+
/**
|
|
40
|
+
* Parse a number into their respective type
|
|
41
|
+
* @param type - integer that represents the type
|
|
42
|
+
* @return parsed value of the type
|
|
43
|
+
*/
|
|
44
|
+
function num2str(type) {
|
|
45
|
+
switch (Number(type)) {
|
|
46
|
+
case exports.TYPES.BYTE:
|
|
47
|
+
return 'byte';
|
|
48
|
+
case exports.TYPES.CHAR:
|
|
49
|
+
return 'char';
|
|
50
|
+
case exports.TYPES.SHORT:
|
|
51
|
+
return 'short';
|
|
52
|
+
case exports.TYPES.INT:
|
|
53
|
+
return 'int';
|
|
54
|
+
case exports.TYPES.FLOAT:
|
|
55
|
+
return 'float';
|
|
56
|
+
case exports.TYPES.DOUBLE:
|
|
57
|
+
return 'double';
|
|
58
|
+
/* istanbul ignore next */
|
|
59
|
+
default:
|
|
60
|
+
return 'undefined';
|
|
61
|
+
}
|
|
56
62
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
63
|
+
exports.num2str = num2str;
|
|
64
|
+
/**
|
|
65
|
+
* Parse a number type identifier to his size in bytes
|
|
66
|
+
* @param type - integer that represents the type
|
|
67
|
+
* @return size of the type
|
|
68
|
+
*/
|
|
69
|
+
function num2bytes(type) {
|
|
70
|
+
switch (Number(type)) {
|
|
71
|
+
case exports.TYPES.BYTE:
|
|
72
|
+
return 1;
|
|
73
|
+
case exports.TYPES.CHAR:
|
|
74
|
+
return 1;
|
|
75
|
+
case exports.TYPES.SHORT:
|
|
76
|
+
return 2;
|
|
77
|
+
case exports.TYPES.INT:
|
|
78
|
+
return 4;
|
|
79
|
+
case exports.TYPES.FLOAT:
|
|
80
|
+
return 4;
|
|
81
|
+
case exports.TYPES.DOUBLE:
|
|
82
|
+
return 8;
|
|
83
|
+
/* istanbul ignore next */
|
|
84
|
+
default:
|
|
85
|
+
return -1;
|
|
86
|
+
}
|
|
80
87
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
88
|
+
exports.num2bytes = num2bytes;
|
|
89
|
+
/**
|
|
90
|
+
* Reverse search of num2str
|
|
91
|
+
* @param type string that represents the type
|
|
92
|
+
* @return parsed value of the type
|
|
93
|
+
*/
|
|
94
|
+
function str2num(type) {
|
|
95
|
+
switch (String(type)) {
|
|
96
|
+
case 'byte':
|
|
97
|
+
return exports.TYPES.BYTE;
|
|
98
|
+
case 'char':
|
|
99
|
+
return exports.TYPES.CHAR;
|
|
100
|
+
case 'short':
|
|
101
|
+
return exports.TYPES.SHORT;
|
|
102
|
+
case 'int':
|
|
103
|
+
return exports.TYPES.INT;
|
|
104
|
+
case 'float':
|
|
105
|
+
return exports.TYPES.FLOAT;
|
|
106
|
+
case 'double':
|
|
107
|
+
return exports.TYPES.DOUBLE;
|
|
108
|
+
/* istanbul ignore next */
|
|
109
|
+
default:
|
|
110
|
+
return -1;
|
|
111
|
+
}
|
|
104
112
|
}
|
|
105
|
-
|
|
113
|
+
exports.str2num = str2num;
|
|
114
|
+
/**
|
|
115
|
+
* Auxiliary function to read numeric data
|
|
116
|
+
* @param size - Size of the element to read
|
|
117
|
+
* @param bufferReader - Function to read next value
|
|
118
|
+
* @return
|
|
119
|
+
*/
|
|
106
120
|
function readNumber(size, bufferReader) {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
121
|
+
if (size !== 1) {
|
|
122
|
+
const numbers = new Array(size);
|
|
123
|
+
for (let i = 0; i < size; i++) {
|
|
124
|
+
numbers[i] = bufferReader();
|
|
125
|
+
}
|
|
126
|
+
return numbers;
|
|
112
127
|
}
|
|
113
|
-
|
|
114
|
-
return numbers;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
return bufferReader();
|
|
128
|
+
return bufferReader();
|
|
118
129
|
}
|
|
119
|
-
|
|
130
|
+
/**
|
|
131
|
+
* Removes null terminate value
|
|
132
|
+
* @param value - String to trim
|
|
133
|
+
* @return - Trimmed string
|
|
134
|
+
*/
|
|
120
135
|
function trimNull(value) {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
return value;
|
|
136
|
+
if (value.charCodeAt(value.length - 1) === 0) {
|
|
137
|
+
return value.substring(0, value.length - 1);
|
|
138
|
+
}
|
|
139
|
+
return value;
|
|
126
140
|
}
|
|
127
|
-
//# sourceMappingURL=read-type.js.map
|