csv-to-pg 3.14.0 → 3.15.1
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/esm/parse.js +8 -3
- package/package.json +2 -2
- package/parse.d.ts +4 -0
- package/parse.js +8 -3
- package/parser.d.ts +1 -0
package/esm/parse.js
CHANGED
|
@@ -196,7 +196,7 @@ const makeNullOrThrow = (fieldName, rawValue, type, required, reason) => {
|
|
|
196
196
|
};
|
|
197
197
|
// type (int, text, etc)
|
|
198
198
|
// from Array of keys that map to records found (e.g., ['lon', 'lat'])
|
|
199
|
-
const getCoercionFunc = (type, from, opts, fieldName) => {
|
|
199
|
+
const getCoercionFunc = (type, from, opts, fieldName, globalOpts) => {
|
|
200
200
|
const parseFn = opts.parse || identity;
|
|
201
201
|
const required = opts.required || false;
|
|
202
202
|
switch (type) {
|
|
@@ -385,7 +385,9 @@ const getCoercionFunc = (type, from, opts, fieldName) => {
|
|
|
385
385
|
case 'text':
|
|
386
386
|
return (record) => {
|
|
387
387
|
const rawValue = record[from[0]];
|
|
388
|
-
const
|
|
388
|
+
const preserve = globalOpts?.preserveEmptyStrings !== false;
|
|
389
|
+
const cleansed = preserve ? rawValue : cleanseEmptyStrings(rawValue);
|
|
390
|
+
const value = parseFn(cleansed);
|
|
389
391
|
if (isEmpty(value)) {
|
|
390
392
|
return makeNullOrThrow(fieldName, rawValue, type, required, 'value is empty or null');
|
|
391
393
|
}
|
|
@@ -469,6 +471,9 @@ const getCoercionFunc = (type, from, opts, fieldName) => {
|
|
|
469
471
|
}
|
|
470
472
|
};
|
|
471
473
|
export const parseTypes = (config) => {
|
|
474
|
+
const globalOpts = {
|
|
475
|
+
preserveEmptyStrings: config.preserveEmptyStrings
|
|
476
|
+
};
|
|
472
477
|
return Object.entries(config.fields).reduce((m, v) => {
|
|
473
478
|
let [key, value] = v;
|
|
474
479
|
let type;
|
|
@@ -488,7 +493,7 @@ export const parseTypes = (config) => {
|
|
|
488
493
|
type = value.type;
|
|
489
494
|
from = getFromValue(value.from || key);
|
|
490
495
|
}
|
|
491
|
-
m[key] = getCoercionFunc(type, from, value, key);
|
|
496
|
+
m[key] = getCoercionFunc(type, from, value, key, globalOpts);
|
|
492
497
|
return m;
|
|
493
498
|
}, {});
|
|
494
499
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "csv-to-pg",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.15.1",
|
|
4
4
|
"author": "Dan Lynch <pyramation@gmail.com>",
|
|
5
5
|
"description": "csv to pg statements",
|
|
6
6
|
"main": "index.js",
|
|
@@ -50,5 +50,5 @@
|
|
|
50
50
|
"js-yaml": "^4.1.0",
|
|
51
51
|
"pgsql-deparser": "^17.18.2"
|
|
52
52
|
},
|
|
53
|
-
"gitHead": "
|
|
53
|
+
"gitHead": "0fcb26c8f3379de5c2bf486e7ad78bb61a76e497"
|
|
54
54
|
}
|
package/parse.d.ts
CHANGED
|
@@ -30,8 +30,12 @@ type CoercionFunc = (record: Record<string, unknown>) => Node;
|
|
|
30
30
|
interface ConfigFields {
|
|
31
31
|
[key: string]: string | FieldOptions;
|
|
32
32
|
}
|
|
33
|
+
export interface GlobalParseOptions {
|
|
34
|
+
preserveEmptyStrings?: boolean;
|
|
35
|
+
}
|
|
33
36
|
interface Config {
|
|
34
37
|
fields: ConfigFields;
|
|
38
|
+
preserveEmptyStrings?: boolean;
|
|
35
39
|
}
|
|
36
40
|
interface TypesMap {
|
|
37
41
|
[key: string]: CoercionFunc;
|
package/parse.js
CHANGED
|
@@ -205,7 +205,7 @@ const makeNullOrThrow = (fieldName, rawValue, type, required, reason) => {
|
|
|
205
205
|
};
|
|
206
206
|
// type (int, text, etc)
|
|
207
207
|
// from Array of keys that map to records found (e.g., ['lon', 'lat'])
|
|
208
|
-
const getCoercionFunc = (type, from, opts, fieldName) => {
|
|
208
|
+
const getCoercionFunc = (type, from, opts, fieldName, globalOpts) => {
|
|
209
209
|
const parseFn = opts.parse || identity;
|
|
210
210
|
const required = opts.required || false;
|
|
211
211
|
switch (type) {
|
|
@@ -394,7 +394,9 @@ const getCoercionFunc = (type, from, opts, fieldName) => {
|
|
|
394
394
|
case 'text':
|
|
395
395
|
return (record) => {
|
|
396
396
|
const rawValue = record[from[0]];
|
|
397
|
-
const
|
|
397
|
+
const preserve = globalOpts?.preserveEmptyStrings !== false;
|
|
398
|
+
const cleansed = preserve ? rawValue : cleanseEmptyStrings(rawValue);
|
|
399
|
+
const value = parseFn(cleansed);
|
|
398
400
|
if (isEmpty(value)) {
|
|
399
401
|
return makeNullOrThrow(fieldName, rawValue, type, required, 'value is empty or null');
|
|
400
402
|
}
|
|
@@ -478,6 +480,9 @@ const getCoercionFunc = (type, from, opts, fieldName) => {
|
|
|
478
480
|
}
|
|
479
481
|
};
|
|
480
482
|
const parseTypes = (config) => {
|
|
483
|
+
const globalOpts = {
|
|
484
|
+
preserveEmptyStrings: config.preserveEmptyStrings
|
|
485
|
+
};
|
|
481
486
|
return Object.entries(config.fields).reduce((m, v) => {
|
|
482
487
|
let [key, value] = v;
|
|
483
488
|
let type;
|
|
@@ -497,7 +502,7 @@ const parseTypes = (config) => {
|
|
|
497
502
|
type = value.type;
|
|
498
503
|
from = getFromValue(value.from || key);
|
|
499
504
|
}
|
|
500
|
-
m[key] = getCoercionFunc(type, from, value, key);
|
|
505
|
+
m[key] = getCoercionFunc(type, from, value, key, globalOpts);
|
|
501
506
|
return m;
|
|
502
507
|
}, {});
|
|
503
508
|
};
|