excel-csv-handler 1.0.11 → 1.1.0

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/package.json CHANGED
@@ -1,31 +1,31 @@
1
1
  {
2
- "name": "excel-csv-handler",
3
- "version": "1.0.11",
4
- "description": "A Node.js utility to read/write Excel and CSV files with GBK encoding support",
5
- "main": "src/index.js",
6
- "types": "src/excel-csv-handler.d.ts",
7
- "type": "module",
8
- "files": [
9
- "src/",
10
- "README.md"
11
- ],
12
- "keywords": [
13
- "excel",
14
- "csv",
15
- "xlsx",
16
- "gbk",
17
- "node",
18
- "file"
19
- ],
20
- "author": "Chao_bei",
21
- "license": "MIT",
22
- "dependencies": {
23
- "chardet": "^2.1.1",
24
- "fast-csv": "^5.0.5",
25
- "iconv-lite": "^0.7.0",
26
- "xlsx": "^0.18.5"
27
- },
28
- "scripts": {
29
- "test": "echo \"Error: no test specified\" && exit 1"
30
- }
31
- }
2
+ "name": "excel-csv-handler",
3
+ "version": "1.1.0",
4
+ "description": "A Node.js utility to read/write Excel and CSV files with GBK encoding support",
5
+ "main": "src/index.js",
6
+ "types": "src/excel-csv-handler.d.ts",
7
+ "type": "module",
8
+ "files": [
9
+ "src/",
10
+ "README.md"
11
+ ],
12
+ "scripts": {
13
+ "test": "echo \"Error: no test specified\" && exit 1"
14
+ },
15
+ "keywords": [
16
+ "excel",
17
+ "csv",
18
+ "xlsx",
19
+ "gbk",
20
+ "node",
21
+ "file"
22
+ ],
23
+ "author": "Chao_bei",
24
+ "license": "MIT",
25
+ "dependencies": {
26
+ "chardet": "^2.1.1",
27
+ "fast-csv": "^5.0.5",
28
+ "iconv-lite": "^0.7.0",
29
+ "xlsx": "^0.18.5"
30
+ }
31
+ }
@@ -44,4 +44,19 @@ export default class ExcelCsvHandler {
44
44
  headers?: string[] | null,
45
45
  encoding?: string
46
46
  ): Promise<void>;
47
+
48
+ /**
49
+ * 从调用文件的相对路径获取绝对路径
50
+ * @param importMetaUrl - 调用文件的 import.meta.url
51
+ * @param relativePath - 相对路径(如 './dataset/file.docx' 或 'dataset/file.docx')
52
+ * @returns 绝对路径
53
+ *
54
+ * @example
55
+ * ```ts
56
+ * import ExcelCsvHandler from 'excel-csv-handler';
57
+ * const docxPath = ExcelCsvHandler.getAbsolutePath(import.meta.url, './dataset/sci-high-question.docx');
58
+ * const csvPath = ExcelCsvHandler.getAbsolutePath(import.meta.url, 'data/output.csv');
59
+ * ```
60
+ */
61
+ static getAbsolutePath(importMetaUrl: string, relativePath: string): string;
47
62
  }
package/src/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  // excel-csv-handler.js
2
2
  import { createRequire } from 'module';
3
+ import { fileURLToPath } from 'url';
3
4
 
4
5
  import * as fs from 'fs';
5
6
  import * as path from 'path';
@@ -18,33 +19,33 @@ class ExcelCsvHandler {
18
19
  detectEncoding(filePath) {
19
20
  // 读取文件的前几个字节来检测编码
20
21
  const buffer = fs.readFileSync(filePath);
21
-
22
+
22
23
  // 检查 UTF-8 BOM
23
- if (buffer.length >= 3 &&
24
- buffer[0] === 0xEF &&
25
- buffer[1] === 0xBB &&
24
+ if (buffer.length >= 3 &&
25
+ buffer[0] === 0xEF &&
26
+ buffer[1] === 0xBB &&
26
27
  buffer[2] === 0xBF) {
27
28
  return 'utf8';
28
29
  }
29
-
30
+
30
31
  // 使用 chardet 库检测编码
31
32
  const detected = chardet.detect(buffer);
32
-
33
+
33
34
  // 将检测结果映射到 iconv-lite 支持的编码名称
34
35
  if (detected) {
35
36
  const encoding = detected.toLowerCase();
36
-
37
+
37
38
  // GB2312, GBK, GB18030 都映射为 gbk
38
39
  if (encoding.includes('gb') || encoding.includes('gbk')) {
39
40
  return 'gbk';
40
41
  }
41
-
42
+
42
43
  // UTF-8 相关
43
44
  if (encoding.includes('utf-8') || encoding.includes('utf8')) {
44
45
  return 'utf8';
45
46
  }
46
47
  }
47
-
48
+
48
49
  // 默认返回 gbk(考虑到中国用户常用 GBK)
49
50
  return 'gbk';
50
51
  }
@@ -138,7 +139,7 @@ class ExcelCsvHandler {
138
139
  async #readCsvFile(filePath, headerRow) {
139
140
  // 自动检测编码
140
141
  const encoding = this.detectEncoding(filePath);
141
-
142
+
142
143
  return new Promise((resolve, reject) => {
143
144
  const allRows = [];
144
145
  const stream = fs.createReadStream(filePath)
@@ -195,7 +196,7 @@ class ExcelCsvHandler {
195
196
  });
196
197
 
197
198
  const buffer = iconv.encode(csvString, encoding);
198
-
199
+
199
200
  // 如果是 UTF-8,添加 BOM 标记以确保 Excel 正确识别
200
201
  if (encoding === 'utf8') {
201
202
  const bom = Buffer.from([0xEF, 0xBB, 0xBF]);
@@ -227,7 +228,7 @@ class ExcelCsvHandler {
227
228
  await this.#writeCsvFile(filePath, data, headers, encoding);
228
229
  return;
229
230
  }
230
-
231
+
231
232
  // 文件存在时,检测现有文件的编码
232
233
  const detectedEncoding = this.detectEncoding(filePath);
233
234
 
@@ -256,6 +257,26 @@ class ExcelCsvHandler {
256
257
  }
257
258
  }
258
259
 
260
+ /**
261
+ * 从调用文件的相对路径获取绝对路径
262
+ * @param {string} importMetaUrl - 调用文件的 import.meta.url
263
+ * @param {string} relativePath - 相对路径(如 './dataset/file.docx' 或 'dataset/file.docx')
264
+ * @returns {string} 绝对路径
265
+ *
266
+ * @example
267
+ * // 在你的文件中使用
268
+ * import ExcelCsvHandler from 'excel-csv-handler';
269
+ * const docxPath = ExcelCsvHandler.getAbsolutePath(import.meta.url, './dataset/sci-high-question.docx');
270
+ * const csvPath = ExcelCsvHandler.getAbsolutePath(import.meta.url, 'data/output.csv');
271
+ */
272
+ ExcelCsvHandler.getAbsolutePath = function (importMetaUrl, relativePath) {
273
+ const __filename = fileURLToPath(importMetaUrl);
274
+ const __dirname = path.dirname(__filename);
275
+ // 去除开头的 './' 如果存在
276
+ const cleanPath = relativePath.startsWith('./') ? relativePath.slice(2) : relativePath;
277
+ return path.join(__dirname, cleanPath);
278
+ };
279
+
259
280
  export default ExcelCsvHandler;
260
281
 
261
282
  // 示例用法(取消注释可测试)