typeshi 1.7.11 → 1.7.12
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/utils/argumentValidation.d.ts +6 -2
- package/dist/utils/io/reading.d.ts +25 -8
- package/dist/utils/io/reading.js +53 -6
- package/dist/utils/io/types/Csv.d.ts +1 -1
- package/dist/utils/io/types/Csv.js +1 -1
- package/dist/utils/io/types/Io.d.ts +4 -0
- package/dist/utils/typeValidation.d.ts +5 -5
- package/package.json +1 -1
|
@@ -18,8 +18,12 @@ export declare function stringArgument(source: string, arg2: string | {
|
|
|
18
18
|
* @param labeledStrings `Record<string, any>` - map name of string param to value passed in for it
|
|
19
19
|
*/
|
|
20
20
|
export declare function multipleStringArguments(source: string, labeledStrings: Record<string, any>): void;
|
|
21
|
-
export declare function multipleExistingFileArguments(source: string, extension: string | string[], labeledFilePaths: Record<string, string>): void;
|
|
22
|
-
|
|
21
|
+
export declare function multipleExistingFileArguments(source: string, extension: FileExtension | FileExtension[] | string | string[], labeledFilePaths: Record<string, string>): void;
|
|
22
|
+
/**
|
|
23
|
+
* common file extensions handled as input/output
|
|
24
|
+
*/
|
|
25
|
+
type FileExtension = '.csv' | '.tsv' | '.txt' | '.json' | '.xlsx' | '.xls' | '.xml' | '.yaml' | '.yml';
|
|
26
|
+
export declare function existingFileArgument(source: string, extension: FileExtension | FileExtension[] | string | string[], arg3: string | {
|
|
23
27
|
[label: string]: any;
|
|
24
28
|
}, value?: any): void;
|
|
25
29
|
export declare function existingDirectoryArgument(source: string, arg2: string | {
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { StringCaseOptions, StringPadOptions, StringStripOptions, CleanStringOptions } from "../regex";
|
|
2
|
-
import { FileData } from "./types/Io";
|
|
2
|
+
import { FileData, FileExtension } from "./types/Io";
|
|
3
3
|
import { DelimiterCharacterEnum } from "./types";
|
|
4
|
-
/**
|
|
4
|
+
/** checks if `pathString (value)` points to an existing directory */
|
|
5
5
|
export declare function isDirectory(value: any): value is string;
|
|
6
|
-
/**
|
|
6
|
+
/** checks if `pathString (value)` points to an existing file */
|
|
7
7
|
export declare function isFile(value: string): value is string;
|
|
8
8
|
/**
|
|
9
9
|
* Determines the proper delimiter based on file type or extension
|
|
@@ -19,17 +19,34 @@ export declare function getDelimiterFromFilePath(filePath: string): DelimiterCha
|
|
|
19
19
|
*/
|
|
20
20
|
export declare const readJsonSync: typeof readJsonFileAsObject;
|
|
21
21
|
/**
|
|
22
|
+
* a.k.a. `readJsonSync`
|
|
22
23
|
* @param filePath `string`
|
|
23
|
-
* @returns **`jsonData`** — `Record<string, any>`
|
|
24
|
-
*
|
|
24
|
+
* @returns **`jsonData`** — `T extends Record<string, any>` - JSON data as an object
|
|
25
|
+
* @note returns empty object if error occurred while reading `filepath` or parsing json
|
|
26
|
+
* - use {@link readJsonSyncOrThrow} if throwing error is desired behavior
|
|
27
|
+
*/
|
|
28
|
+
export declare function readJsonFileAsObject<T extends Record<string, any> = {}>(filePath: string): T;
|
|
29
|
+
/**
|
|
30
|
+
* @param filePath `string`
|
|
31
|
+
* @returns **`jsonData`** — `T extends Record<string, any>`
|
|
32
|
+
* @throws {Error} if error occurred while reading `filepath` or parsing json
|
|
33
|
+
*/
|
|
34
|
+
export declare function readJsonSyncOrThrow<T extends Record<string, any> = {}>(filePath: string): T;
|
|
35
|
+
/**
|
|
36
|
+
* @param filePath `string`
|
|
37
|
+
* @param separator `string | RegExp` `default` = `/\r?\n/` (i.e. each line is an individual element in the array)
|
|
38
|
+
* @param encoding {@link BufferEncoding} `default` = `'utf8'`
|
|
39
|
+
* @returns **`arr`** `string[]` - the file content separated by specified param value
|
|
40
|
+
* - returns empty array if error occurs while reading file.
|
|
25
41
|
*/
|
|
26
|
-
export declare function
|
|
42
|
+
export declare function readFileToArraySync(filePath: string, separator?: string | RegExp, encoding?: BufferEncoding): string[];
|
|
27
43
|
/**
|
|
44
|
+
* @description adds `'.${expectedExtension}'` to end of `filePath` if not already present
|
|
28
45
|
* @param filePath `string`
|
|
29
|
-
* @param expectedExtension `string`
|
|
46
|
+
* @param expectedExtension `string | `{@link FileExtension}
|
|
30
47
|
* @returns **`validatedFilePath`** `string`
|
|
31
48
|
*/
|
|
32
|
-
export declare function coerceFileExtension(filePath: string, expectedExtension: string): string;
|
|
49
|
+
export declare function coerceFileExtension(filePath: string, expectedExtension: string | FileExtension): string;
|
|
33
50
|
/**
|
|
34
51
|
* - {@link getDirectoryFiles}
|
|
35
52
|
* @param arg1 `Array<`{@link FileData}` | string> | string`
|
package/dist/utils/io/reading.js
CHANGED
|
@@ -41,6 +41,8 @@ exports.isDirectory = isDirectory;
|
|
|
41
41
|
exports.isFile = isFile;
|
|
42
42
|
exports.getDelimiterFromFilePath = getDelimiterFromFilePath;
|
|
43
43
|
exports.readJsonFileAsObject = readJsonFileAsObject;
|
|
44
|
+
exports.readJsonSyncOrThrow = readJsonSyncOrThrow;
|
|
45
|
+
exports.readFileToArraySync = readFileToArraySync;
|
|
44
46
|
exports.coerceFileExtension = coerceFileExtension;
|
|
45
47
|
exports.concatenateFiles = concatenateFiles;
|
|
46
48
|
exports.getRows = getRows;
|
|
@@ -75,13 +77,13 @@ const types_1 = require("./types");
|
|
|
75
77
|
const typeValidation_1 = require("../typeValidation");
|
|
76
78
|
const validate = __importStar(require("../argumentValidation"));
|
|
77
79
|
const logging_1 = require("./logging");
|
|
78
|
-
/**
|
|
80
|
+
/** checks if `pathString (value)` points to an existing directory */
|
|
79
81
|
function isDirectory(value) {
|
|
80
82
|
return ((0, typeValidation_1.isNonEmptyString)(value)
|
|
81
83
|
&& fs_1.default.existsSync(value)
|
|
82
84
|
&& fs_1.default.statSync(value).isDirectory());
|
|
83
85
|
}
|
|
84
|
-
/**
|
|
86
|
+
/** checks if `pathString (value)` points to an existing file */
|
|
85
87
|
function isFile(value) {
|
|
86
88
|
return ((0, typeValidation_1.isNonEmptyString)(value)
|
|
87
89
|
&& fs_1.default.existsSync(value)
|
|
@@ -112,9 +114,11 @@ function getDelimiterFromFilePath(filePath) {
|
|
|
112
114
|
*/
|
|
113
115
|
exports.readJsonSync = readJsonFileAsObject;
|
|
114
116
|
/**
|
|
117
|
+
* a.k.a. `readJsonSync`
|
|
115
118
|
* @param filePath `string`
|
|
116
|
-
* @returns **`jsonData`** — `Record<string, any>`
|
|
117
|
-
*
|
|
119
|
+
* @returns **`jsonData`** — `T extends Record<string, any>` - JSON data as an object
|
|
120
|
+
* @note returns empty object if error occurred while reading `filepath` or parsing json
|
|
121
|
+
* - use {@link readJsonSyncOrThrow} if throwing error is desired behavior
|
|
118
122
|
*/
|
|
119
123
|
function readJsonFileAsObject(filePath) {
|
|
120
124
|
const source = (0, logging_1.getSourceString)(__filename, readJsonFileAsObject.name);
|
|
@@ -124,17 +128,60 @@ function readJsonFileAsObject(filePath) {
|
|
|
124
128
|
const jsonData = JSON.parse(data);
|
|
125
129
|
return jsonData;
|
|
126
130
|
}
|
|
131
|
+
catch (error) {
|
|
132
|
+
config_1.typeshiLogger.error([`${source} Error reading JSON file, returning empty object`,
|
|
133
|
+
`Given filePath: '${filePath}'`,
|
|
134
|
+
`caught error: ${error}`,
|
|
135
|
+
].join(config_1.INDENT_LOG_LINE));
|
|
136
|
+
return {};
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* @param filePath `string`
|
|
141
|
+
* @returns **`jsonData`** — `T extends Record<string, any>`
|
|
142
|
+
* @throws {Error} if error occurred while reading `filepath` or parsing json
|
|
143
|
+
*/
|
|
144
|
+
function readJsonSyncOrThrow(filePath) {
|
|
145
|
+
const source = (0, logging_1.getSourceString)(__filename, readJsonSyncOrThrow.name);
|
|
146
|
+
try {
|
|
147
|
+
filePath = coerceFileExtension(filePath, 'json');
|
|
148
|
+
const data = fs_1.default.readFileSync(filePath, 'utf8');
|
|
149
|
+
const jsonData = JSON.parse(data);
|
|
150
|
+
return jsonData;
|
|
151
|
+
}
|
|
127
152
|
catch (error) {
|
|
128
153
|
config_1.typeshiLogger.error([`${source} Error reading JSON file`,
|
|
129
154
|
`Given filePath: '${filePath}'`,
|
|
130
|
-
`error: `,
|
|
155
|
+
`caught error: ${error}`,
|
|
131
156
|
].join(config_1.INDENT_LOG_LINE));
|
|
132
157
|
throw new Error(JSON.stringify(error));
|
|
133
158
|
}
|
|
134
159
|
}
|
|
135
160
|
/**
|
|
136
161
|
* @param filePath `string`
|
|
137
|
-
* @param
|
|
162
|
+
* @param separator `string | RegExp` `default` = `/\r?\n/` (i.e. each line is an individual element in the array)
|
|
163
|
+
* @param encoding {@link BufferEncoding} `default` = `'utf8'`
|
|
164
|
+
* @returns **`arr`** `string[]` - the file content separated by specified param value
|
|
165
|
+
* - returns empty array if error occurs while reading file.
|
|
166
|
+
*/
|
|
167
|
+
function readFileToArraySync(filePath, separator = /\r?\n/, encoding = 'utf8') {
|
|
168
|
+
const source = (0, logging_1.getSourceString)(__filename, readFileToArraySync.name);
|
|
169
|
+
try {
|
|
170
|
+
const content = fs_1.default.readFileSync(filePath, { encoding });
|
|
171
|
+
return content.split(separator);
|
|
172
|
+
}
|
|
173
|
+
catch (error) {
|
|
174
|
+
config_1.typeshiLogger.error([`${source} Error reading file, returning empty array`,
|
|
175
|
+
`Given filePath: '${filePath}'`,
|
|
176
|
+
`caught error: ${error}`,
|
|
177
|
+
].join(config_1.INDENT_LOG_LINE));
|
|
178
|
+
return [];
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* @description adds `'.${expectedExtension}'` to end of `filePath` if not already present
|
|
183
|
+
* @param filePath `string`
|
|
184
|
+
* @param expectedExtension `string | `{@link FileExtension}
|
|
138
185
|
* @returns **`validatedFilePath`** `string`
|
|
139
186
|
*/
|
|
140
187
|
function coerceFileExtension(filePath, expectedExtension) {
|
|
@@ -12,7 +12,7 @@ export type RowSourceMetaData = {
|
|
|
12
12
|
* @enum {string} **`DelimitedFileTypeEnum`**
|
|
13
13
|
* @property {string} CSV = `'csv'` Comma-separated values -> `.csv` file.
|
|
14
14
|
* @property {string} TSV = `'tsv'` Tab-separated values -> `.tsv` file.
|
|
15
|
-
* @property {string} AUTO = `'auto'`- call
|
|
15
|
+
* @property {string} AUTO = `'auto'`- call `getDelimiterFromFilePath(filePath, fileType)` to detect file type based on file extension.
|
|
16
16
|
*/
|
|
17
17
|
export declare enum DelimitedFileTypeEnum {
|
|
18
18
|
CSV = "csv",
|
|
@@ -8,7 +8,7 @@ exports.DelimiterCharacterEnum = exports.DelimitedFileTypeEnum = void 0;
|
|
|
8
8
|
* @enum {string} **`DelimitedFileTypeEnum`**
|
|
9
9
|
* @property {string} CSV = `'csv'` Comma-separated values -> `.csv` file.
|
|
10
10
|
* @property {string} TSV = `'tsv'` Tab-separated values -> `.tsv` file.
|
|
11
|
-
* @property {string} AUTO = `'auto'`- call
|
|
11
|
+
* @property {string} AUTO = `'auto'`- call `getDelimiterFromFilePath(filePath, fileType)` to detect file type based on file extension.
|
|
12
12
|
*/
|
|
13
13
|
var DelimitedFileTypeEnum;
|
|
14
14
|
(function (DelimitedFileTypeEnum) {
|
|
@@ -47,3 +47,7 @@ export type ParseOneToManyOptions = {
|
|
|
47
47
|
keyPadOptions?: StringPadOptions;
|
|
48
48
|
valuePadOptions?: StringPadOptions;
|
|
49
49
|
};
|
|
50
|
+
/**
|
|
51
|
+
* common file extensions handled as input/output
|
|
52
|
+
*/
|
|
53
|
+
export type FileExtension = '.csv' | '.tsv' | '.txt' | '.json' | '.xlsx' | '.xls' | '.xml' | '.yaml' | '.yml';
|
|
@@ -208,19 +208,19 @@ export declare function isFunction(value: any): value is Function;
|
|
|
208
208
|
*/
|
|
209
209
|
export declare function isUndefined(value: any): value is undefined;
|
|
210
210
|
export declare function isUndefinedOrNull(value: unknown): value is undefined | null;
|
|
211
|
-
export type NumberKeys<T, Required extends
|
|
211
|
+
export type NumberKeys<T, Required extends boolean = false> = {
|
|
212
212
|
[K in keyof T]: Required extends true ? (T[K] extends number ? K : never) : (T[K] extends number | undefined ? K : never);
|
|
213
213
|
}[keyof T][];
|
|
214
|
-
export type ArrayKeys<T, Required extends
|
|
214
|
+
export type ArrayKeys<T, Required extends boolean = false> = {
|
|
215
215
|
[K in keyof T]: Required extends true ? (T[K] extends Array<any> ? K : never) : (T[K] extends Array<any> | undefined ? K : never);
|
|
216
216
|
}[keyof T][];
|
|
217
|
-
export type ArrayOfTypeKeys<T, U, Required extends
|
|
217
|
+
export type ArrayOfTypeKeys<T, U, Required extends boolean = false> = {
|
|
218
218
|
[K in keyof T]: Required extends true ? (T[K] extends Array<U> ? K : never) : (T[K] extends Array<U> | undefined ? K : never);
|
|
219
219
|
}[keyof T][];
|
|
220
|
-
export type StringKeys<T, Required extends
|
|
220
|
+
export type StringKeys<T, Required extends boolean = false> = {
|
|
221
221
|
[K in keyof T]: Required extends true ? (T[K] extends string ? K : never) : (T[K] extends string | undefined ? K : never);
|
|
222
222
|
}[keyof T][];
|
|
223
|
-
export type PrimitiveKeys<T, Required extends
|
|
223
|
+
export type PrimitiveKeys<T, Required extends boolean = false> = {
|
|
224
224
|
[K in keyof T]: Required extends true ? (T[K] extends string | number | boolean | null ? K : never) : (T[K] extends string | number | boolean | null | undefined ? K : never);
|
|
225
225
|
}[keyof T][];
|
|
226
226
|
export type Primitive = string | number | boolean | null | undefined;
|