@nu-art/ts-common 0.203.74 → 0.203.76
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/core/module.js +2 -0
- package/csv.d.ts +1 -0
- package/csv.js +1 -0
- package/modules/CSVModuleV3.d.ts +17 -0
- package/modules/CSVModuleV3.js +77 -0
- package/modules/JSONCSVModule.d.ts +16 -0
- package/modules/JSONCSVModule.js +69 -0
- package/package.json +3 -2
package/core/module.js
CHANGED
|
@@ -85,11 +85,13 @@ class Module extends Logger_1.Logger {
|
|
|
85
85
|
setName(name) {
|
|
86
86
|
this.name = name;
|
|
87
87
|
}
|
|
88
|
+
// @ts-ignore
|
|
88
89
|
setConfig(config) {
|
|
89
90
|
// @ts-ignore
|
|
90
91
|
this.config = this.config ? (0, merge_tools_1.merge)(this.config, config || {}) : config;
|
|
91
92
|
this.config.minLogLevel && this.setMinLevel(this.config.minLogLevel);
|
|
92
93
|
}
|
|
94
|
+
// @ts-ignore
|
|
93
95
|
setManager(manager) {
|
|
94
96
|
// @ts-ignore
|
|
95
97
|
this.manager = manager;
|
package/csv.d.ts
CHANGED
package/csv.js
CHANGED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Created by tacb0ss on 07/05/2018.
|
|
3
|
+
*/
|
|
4
|
+
import { Readable, Writable } from 'stream';
|
|
5
|
+
import { Module } from '../core/module';
|
|
6
|
+
import * as csv from 'fast-csv';
|
|
7
|
+
export declare class CSVModuleV3_Class extends Module {
|
|
8
|
+
constructor();
|
|
9
|
+
protected init(): void;
|
|
10
|
+
readFromStream: <T>(stream: Readable, processor: (item: T, rowNumber: number, stream: csv.CsvParserStream<csv.ParserRow<any>, csv.ParserRow<any>>) => void) => Promise<void>;
|
|
11
|
+
protected readImpl: <T>(stream: Readable, processor: (item: T, rowNumber: number, stream: csv.CsvParserStream<csv.ParserRow<any>, csv.ParserRow<any>>) => void) => Promise<void>;
|
|
12
|
+
writeToStream: <I extends csv.FormatterRow = csv.FormatterRow, O extends csv.FormatterRow = csv.FormatterRow>(writable: Writable, items: I[], options?: csv.FormatterOptionsArgs<I, O> | undefined) => Promise<void>;
|
|
13
|
+
protected writeImpl: <I extends unknown>(writable: Writable, items: I[], options?: csv.FormatterOptionsArgs<any, any>) => Promise<void>;
|
|
14
|
+
provideFormatter: (transformer?: (item: any) => any) => csv.CsvFormatterStream<csv.FormatterRow, csv.FormatterRow>;
|
|
15
|
+
provideFormatterFromCsv: (transformer?: (item: any) => any) => csv.CsvParserStream<csv.ParserRow<any>, csv.ParserRow<any>>;
|
|
16
|
+
}
|
|
17
|
+
export declare const CSVModuleV3: CSVModuleV3_Class;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* ts-common is the basic building blocks of our typescript projects
|
|
4
|
+
*
|
|
5
|
+
* Copyright (C) 2020 Adam van der Kruk aka TacB0sS
|
|
6
|
+
*
|
|
7
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
8
|
+
* you may not use this file except in compliance with the License.
|
|
9
|
+
* You may obtain a copy of the License at
|
|
10
|
+
*
|
|
11
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
*
|
|
13
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
+
* See the License for the specific language governing permissions and
|
|
17
|
+
* limitations under the License.
|
|
18
|
+
*/
|
|
19
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
20
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
21
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
22
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
23
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
24
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
25
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.CSVModuleV3 = exports.CSVModuleV3_Class = void 0;
|
|
30
|
+
const module_1 = require("../core/module");
|
|
31
|
+
const csv = require("fast-csv");
|
|
32
|
+
class CSVModuleV3_Class extends module_1.Module {
|
|
33
|
+
constructor() {
|
|
34
|
+
super();
|
|
35
|
+
this.readFromStream = (stream, processor) => __awaiter(this, void 0, void 0, function* () {
|
|
36
|
+
return this.readImpl(stream, processor);
|
|
37
|
+
});
|
|
38
|
+
this.readImpl = (stream, processor) => __awaiter(this, void 0, void 0, function* () {
|
|
39
|
+
return new Promise((resolve, reject) => {
|
|
40
|
+
let rowIndex = 0;
|
|
41
|
+
const csvParser = csv.parse({ headers: true, trim: true });
|
|
42
|
+
csvParser
|
|
43
|
+
.on('data', (instance) => {
|
|
44
|
+
processor(instance, rowIndex++, csvParser);
|
|
45
|
+
})
|
|
46
|
+
.on('error', (err) => reject(err))
|
|
47
|
+
.on('end', () => {
|
|
48
|
+
this.logInfo('read ended');
|
|
49
|
+
resolve();
|
|
50
|
+
});
|
|
51
|
+
stream.pipe(csvParser);
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
this.writeToStream = (writable, items, options) => {
|
|
55
|
+
return this.writeImpl(writable, items, options);
|
|
56
|
+
};
|
|
57
|
+
this.writeImpl = (writable, items, options) => {
|
|
58
|
+
return new Promise((resolve, reject) => {
|
|
59
|
+
csv.writeToStream(writable, items, options)
|
|
60
|
+
.on('finish', () => resolve())
|
|
61
|
+
.on('error', err => reject(err));
|
|
62
|
+
});
|
|
63
|
+
};
|
|
64
|
+
this.provideFormatter = (transformer = item => item) => {
|
|
65
|
+
return csv.format({ headers: true })
|
|
66
|
+
.transform(transformer);
|
|
67
|
+
};
|
|
68
|
+
this.provideFormatterFromCsv = (transformer = item => item) => {
|
|
69
|
+
return csv.parse({ headers: true, trim: true })
|
|
70
|
+
.transform(transformer);
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
init() {
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
exports.CSVModuleV3_Class = CSVModuleV3_Class;
|
|
77
|
+
exports.CSVModuleV3 = new CSVModuleV3_Class();
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { CSVModuleV3_Class } from './CSVModuleV3';
|
|
2
|
+
import { TS_Object } from '../utils/types';
|
|
3
|
+
import { Readable, Writable } from 'stream';
|
|
4
|
+
import * as csv from 'fast-csv';
|
|
5
|
+
type Output<T extends TS_Object> = {
|
|
6
|
+
[K in keyof T]: T[K] extends object ? string : T[K];
|
|
7
|
+
};
|
|
8
|
+
export declare class JSONCSVModule_Class<I extends TS_Object, O extends Output<I> = Output<I>> extends CSVModuleV3_Class {
|
|
9
|
+
private keysToStringify;
|
|
10
|
+
constructor(keysToStringify: (keyof I)[]);
|
|
11
|
+
private processToJSON;
|
|
12
|
+
private processFromJSON;
|
|
13
|
+
protected writeImpl: (writable: Writable, items: I[], options?: csv.FormatterOptionsArgs<I, O>) => Promise<void>;
|
|
14
|
+
protected readImpl: (stream: Readable, processor: (item: I, rowNumber: number, stream: csv.CsvParserStream<csv.ParserRow<any>, csv.ParserRow<any>>) => void) => Promise<void>;
|
|
15
|
+
}
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.JSONCSVModule_Class = void 0;
|
|
13
|
+
const CSVModuleV3_1 = require("./CSVModuleV3");
|
|
14
|
+
const object_tools_1 = require("../utils/object-tools");
|
|
15
|
+
const tools_1 = require("../utils/tools");
|
|
16
|
+
const csv = require("fast-csv");
|
|
17
|
+
class JSONCSVModule_Class extends CSVModuleV3_1.CSVModuleV3_Class {
|
|
18
|
+
constructor(keysToStringify) {
|
|
19
|
+
super();
|
|
20
|
+
this.processToJSON = (i) => {
|
|
21
|
+
const o = {};
|
|
22
|
+
(0, object_tools_1._keys)(i).forEach(k => {
|
|
23
|
+
if (this.keysToStringify.includes(k))
|
|
24
|
+
return o[k] = (0, tools_1.__stringify)(i[k]);
|
|
25
|
+
o[k] = i[k];
|
|
26
|
+
});
|
|
27
|
+
return o;
|
|
28
|
+
};
|
|
29
|
+
this.processFromJSON = (o) => {
|
|
30
|
+
const i = {};
|
|
31
|
+
(0, object_tools_1._keys)(o).forEach(_k => {
|
|
32
|
+
const k = _k;
|
|
33
|
+
if (this.keysToStringify.includes(k)) {
|
|
34
|
+
return i[k] = JSON.parse(o[k]);
|
|
35
|
+
}
|
|
36
|
+
i[k] = o[k];
|
|
37
|
+
});
|
|
38
|
+
return i;
|
|
39
|
+
};
|
|
40
|
+
// @ts-ignore
|
|
41
|
+
this.writeImpl = (writable, items, options) => __awaiter(this, void 0, void 0, function* () {
|
|
42
|
+
return new Promise((resolve, reject) => {
|
|
43
|
+
const _options = Object.assign(Object.assign({}, options), { transform: this.processToJSON });
|
|
44
|
+
csv.writeToStream(writable, items, _options)
|
|
45
|
+
.on('finish', () => resolve())
|
|
46
|
+
.on('error', err => reject(err));
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
// @ts-ignore
|
|
50
|
+
this.readImpl = (stream, processor) => __awaiter(this, void 0, void 0, function* () {
|
|
51
|
+
return new Promise((resolve, reject) => {
|
|
52
|
+
let rowIndex = 0;
|
|
53
|
+
const csvParser = csv.parse({ headers: true, trim: true });
|
|
54
|
+
csvParser
|
|
55
|
+
.on('data', (instance) => {
|
|
56
|
+
processor(this.processFromJSON(instance), rowIndex++, csvParser);
|
|
57
|
+
})
|
|
58
|
+
.on('error', (err) => reject(err))
|
|
59
|
+
.on('end', () => {
|
|
60
|
+
this.logInfo('read ended');
|
|
61
|
+
resolve();
|
|
62
|
+
});
|
|
63
|
+
stream.pipe(csvParser);
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
this.keysToStringify = keysToStringify;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
exports.JSONCSVModule_Class = JSONCSVModule_Class;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nu-art/ts-common",
|
|
3
|
-
"version": "0.203.
|
|
3
|
+
"version": "0.203.76",
|
|
4
4
|
"description": "js and ts infra",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"TacB0sS",
|
|
@@ -37,7 +37,8 @@
|
|
|
37
37
|
"moment": "^2.29.4",
|
|
38
38
|
"node-forge": "^1.2.1",
|
|
39
39
|
"uuid": "^9.0.0",
|
|
40
|
-
"csv-parser": "^2.3.3"
|
|
40
|
+
"csv-parser": "^2.3.3",
|
|
41
|
+
"xlsx": "^0.18.5"
|
|
41
42
|
},
|
|
42
43
|
"devDependencies": {
|
|
43
44
|
"@types/mocha": "^10.0.6",
|