ag-common 0.0.707 → 0.0.709

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';
@@ -16,4 +18,6 @@ export * from './object';
16
18
  export * from './random';
17
19
  export * from './secondsInNearest';
18
20
  export * from './sleep';
21
+ export * from './stream';
19
22
  export * from './string';
23
+ export * from './xml';
@@ -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);
@@ -32,4 +34,6 @@ __exportStar(require("./object"), exports);
32
34
  __exportStar(require("./random"), exports);
33
35
  __exportStar(require("./secondsInNearest"), exports);
34
36
  __exportStar(require("./sleep"), exports);
37
+ __exportStar(require("./stream"), exports);
35
38
  __exportStar(require("./string"), exports);
39
+ __exportStar(require("./xml"), exports);
@@ -0,0 +1,9 @@
1
+ export declare const convertXmlToJson: (xmlFilePath: string) => Promise<any>;
2
+ export declare class XMLParser {
3
+ private stack;
4
+ private currentObject;
5
+ parse(xmlData: string): any;
6
+ private onStartElement;
7
+ private onEndElement;
8
+ private onText;
9
+ }
@@ -0,0 +1,126 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ Object.defineProperty(exports, "__esModule", { value: true });
35
+ exports.XMLParser = exports.convertXmlToJson = void 0;
36
+ /* eslint-disable @typescript-eslint/no-explicit-any */
37
+ const fs = __importStar(require("fs"));
38
+ const convertXmlToJson = (xmlFilePath) => __awaiter(void 0, void 0, void 0, function* () {
39
+ const xmlData = yield fs.readFileSync(xmlFilePath, 'utf8');
40
+ const parser = new XMLParser();
41
+ const ret = parser.parse(xmlData);
42
+ return ret;
43
+ });
44
+ exports.convertXmlToJson = convertXmlToJson;
45
+ class XMLParser {
46
+ constructor() {
47
+ this.stack = [];
48
+ this.currentObject = {};
49
+ }
50
+ parse(xmlData) {
51
+ const parser = new XMLReader();
52
+ parser.onStartElement = this.onStartElement.bind(this);
53
+ parser.onEndElement = this.onEndElement.bind(this);
54
+ parser.onText = this.onText.bind(this);
55
+ parser.parse(xmlData);
56
+ return this.currentObject;
57
+ }
58
+ onStartElement(name, attributes) {
59
+ let newObject = {};
60
+ if (Object.keys(attributes).length > 0) {
61
+ newObject = Object.assign(Object.assign({}, newObject), attributes);
62
+ }
63
+ this.stack.push(this.currentObject);
64
+ const parent = this.stack[this.stack.length - 1];
65
+ if (!parent[name]) {
66
+ parent[name] = newObject;
67
+ }
68
+ else if (Array.isArray(parent[name])) {
69
+ parent[name].push(newObject);
70
+ }
71
+ else {
72
+ parent[name] = [parent[name], newObject];
73
+ }
74
+ this.currentObject = newObject;
75
+ }
76
+ onEndElement() {
77
+ this.currentObject = this.stack.pop() || {};
78
+ }
79
+ onText(text) {
80
+ if (text.trim().length > 0) {
81
+ if (!this.currentObject['$text']) {
82
+ this.currentObject['$text'] = text.trim();
83
+ }
84
+ else {
85
+ this.currentObject['$text'] += text.trim();
86
+ }
87
+ }
88
+ }
89
+ }
90
+ exports.XMLParser = XMLParser;
91
+ class XMLReader {
92
+ constructor() {
93
+ this.buffer = '';
94
+ this.onStartElement = () => { };
95
+ this.onEndElement = () => { };
96
+ this.onText = () => { };
97
+ }
98
+ parse(xmlData) {
99
+ this.buffer = xmlData.replace(/\r\n/g, '').replace(/\n/g, '');
100
+ const regex = /<\/?([^\s>]+)(\s*[^>]*?)?>|([^<>]+)/g;
101
+ let match;
102
+ while ((match = regex.exec(this.buffer))) {
103
+ const [line, startTag, attributes, text] = match;
104
+ if (text) {
105
+ this.onText(text);
106
+ }
107
+ else if (line.startsWith('</')) {
108
+ this.onEndElement(startTag.slice(1));
109
+ }
110
+ else {
111
+ const attributeObj = {};
112
+ if (attributes) {
113
+ const attrRegex = /\s*([^=\s]+)=(?:"([^"]+)"|'([^']+)')/g;
114
+ let attrMatch;
115
+ while ((attrMatch = attrRegex.exec(attributes))) {
116
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
117
+ const [_, key, value1, value2] = attrMatch;
118
+ const cleanValue = value1 || value2;
119
+ attributeObj[key] = cleanValue;
120
+ }
121
+ }
122
+ this.onStartElement(startTag, attributeObj);
123
+ }
124
+ }
125
+ }
126
+ }
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.0.707",
2
+ "version": "0.0.709",
3
3
  "name": "ag-common",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",