ag-common 0.0.707 → 0.0.708
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.
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function loadCsvAsJson<T extends Record<string | number, any>>(filePath: string): T[];
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.loadCsvAsJson = void 0;
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const math_1 = require("./math");
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
10
|
+
function loadCsvAsJson(filePath) {
|
|
11
|
+
const fileData = fs_1.default.readFileSync(filePath, 'utf8');
|
|
12
|
+
const lines = fileData.split(/[\r]?\n/);
|
|
13
|
+
let sep = ',';
|
|
14
|
+
if (lines[0].split(',').length === 1 && lines[0].split(';').length > 1) {
|
|
15
|
+
sep = ';';
|
|
16
|
+
}
|
|
17
|
+
const headers = lines[0].split(sep);
|
|
18
|
+
const jsonData = lines.slice(1).map((line) => {
|
|
19
|
+
const values = [];
|
|
20
|
+
let inQuote = false;
|
|
21
|
+
let currentValue = '';
|
|
22
|
+
for (const char of line) {
|
|
23
|
+
if (char === '"') {
|
|
24
|
+
inQuote = !inQuote;
|
|
25
|
+
}
|
|
26
|
+
else if (char === sep && !inQuote) {
|
|
27
|
+
values.push(currentValue.trim());
|
|
28
|
+
currentValue = '';
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
currentValue += char;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
values.push(currentValue.trim());
|
|
35
|
+
const obj = {};
|
|
36
|
+
for (let i = 0; i < headers.length; i++) {
|
|
37
|
+
const v = values[i];
|
|
38
|
+
if (v === '-') {
|
|
39
|
+
//ignore
|
|
40
|
+
}
|
|
41
|
+
else if ((0, math_1.isNumber)(v)) {
|
|
42
|
+
//@ts-ignore
|
|
43
|
+
obj[headers[i]] = Number(v);
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
//@ts-ignore
|
|
47
|
+
obj[headers[i]] = v;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return obj;
|
|
51
|
+
});
|
|
52
|
+
return jsonData;
|
|
53
|
+
}
|
|
54
|
+
exports.loadCsvAsJson = loadCsvAsJson;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Recursively gets all file paths in a directory and its subdirectories.
|
|
3
|
+
*
|
|
4
|
+
* @param {string} dirPath - The path to the directory to search.
|
|
5
|
+
* @param {string} [extension] - Optional file extension to filter by (e.g., 'js', 'txt').
|
|
6
|
+
* @returns {string[]} An array of file paths matching the specified criteria.
|
|
7
|
+
*/
|
|
8
|
+
export declare function getAllFiles(dirPath: string, extension?: string): string[];
|
|
9
|
+
/**
|
|
10
|
+
* Extracts the file name and extension from a given file path.
|
|
11
|
+
*
|
|
12
|
+
* @param {string} filePath - The absolute or relative file path.
|
|
13
|
+
* @returns {Object} An object with two properties: `fileName` (string) and `extension` (string).
|
|
14
|
+
*/
|
|
15
|
+
export declare function getFileNameAndExtension(filePath: string): {
|
|
16
|
+
fileName: string;
|
|
17
|
+
extension: string;
|
|
18
|
+
directory: string;
|
|
19
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getFileNameAndExtension = exports.getAllFiles = void 0;
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
/**
|
|
10
|
+
* Recursively gets all file paths in a directory and its subdirectories.
|
|
11
|
+
*
|
|
12
|
+
* @param {string} dirPath - The path to the directory to search.
|
|
13
|
+
* @param {string} [extension] - Optional file extension to filter by (e.g., 'js', 'txt').
|
|
14
|
+
* @returns {string[]} An array of file paths matching the specified criteria.
|
|
15
|
+
*/
|
|
16
|
+
function getAllFiles(dirPath, extension) {
|
|
17
|
+
const files = [];
|
|
18
|
+
if (extension && !extension.startsWith('.')) {
|
|
19
|
+
extension = '.' + extension;
|
|
20
|
+
}
|
|
21
|
+
const getFilesRecursively = (currentPath) => {
|
|
22
|
+
const entries = fs_1.default.readdirSync(currentPath, { withFileTypes: true });
|
|
23
|
+
for (const entry of entries) {
|
|
24
|
+
const entryPath = path_1.default.join(currentPath, entry.name);
|
|
25
|
+
if (entry.isDirectory()) {
|
|
26
|
+
getFilesRecursively(entryPath);
|
|
27
|
+
}
|
|
28
|
+
else if (!extension || path_1.default.extname(entryPath) === extension) {
|
|
29
|
+
files.push(entryPath);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
getFilesRecursively(dirPath);
|
|
34
|
+
return files;
|
|
35
|
+
}
|
|
36
|
+
exports.getAllFiles = getAllFiles;
|
|
37
|
+
/**
|
|
38
|
+
* Extracts the file name and extension from a given file path.
|
|
39
|
+
*
|
|
40
|
+
* @param {string} filePath - The absolute or relative file path.
|
|
41
|
+
* @returns {Object} An object with two properties: `fileName` (string) and `extension` (string).
|
|
42
|
+
*/
|
|
43
|
+
function getFileNameAndExtension(filePath) {
|
|
44
|
+
const ext = path_1.default.extname(filePath);
|
|
45
|
+
const baseName = path_1.default.basename(filePath, ext);
|
|
46
|
+
const directory = path_1.default.dirname(filePath);
|
|
47
|
+
return {
|
|
48
|
+
fileName: baseName,
|
|
49
|
+
extension: ext.slice(1), // Remove the leading dot from the extension
|
|
50
|
+
directory,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
exports.getFileNameAndExtension = getFileNameAndExtension;
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
export * from './array';
|
|
2
2
|
export * from './async';
|
|
3
3
|
export * from './binary';
|
|
4
|
+
export * from './csv';
|
|
4
5
|
export * from './date';
|
|
5
6
|
export * from './email';
|
|
6
7
|
export * from './fetch';
|
|
8
|
+
export * from './fs';
|
|
7
9
|
export * from './func';
|
|
8
10
|
export * from './generator';
|
|
9
11
|
export * from './groupBy';
|
|
@@ -17,9 +17,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
17
|
__exportStar(require("./array"), exports);
|
|
18
18
|
__exportStar(require("./async"), exports);
|
|
19
19
|
__exportStar(require("./binary"), exports);
|
|
20
|
+
__exportStar(require("./csv"), exports);
|
|
20
21
|
__exportStar(require("./date"), exports);
|
|
21
22
|
__exportStar(require("./email"), exports);
|
|
22
23
|
__exportStar(require("./fetch"), exports);
|
|
24
|
+
__exportStar(require("./fs"), exports);
|
|
23
25
|
__exportStar(require("./func"), exports);
|
|
24
26
|
__exportStar(require("./generator"), exports);
|
|
25
27
|
__exportStar(require("./groupBy"), exports);
|
package/package.json
CHANGED