@soga/utils 0.0.2 → 0.0.3
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/charcode.d.ts +1 -2
- package/dist/charcode.js +1 -1
- package/package.json +1 -1
package/dist/charcode.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { AnalyseResult } from 'chardet';
|
|
2
|
-
export declare const isUtf8Text: (text: string) => Promise<boolean>;
|
|
3
|
-
export declare const isUtf8File: (filepath: string) => Promise<boolean>;
|
|
4
2
|
export declare const getFileAllCharcode: (filepath: string) => Promise<AnalyseResult>;
|
|
5
3
|
export declare const getFileCharcodeName: (filepath: string) => Promise<string>;
|
|
6
4
|
export declare const getBufferCharcodeName: (buffer: Buffer<ArrayBufferLike>) => string;
|
|
5
|
+
export declare const isUtf8File: (filepath: string) => Promise<boolean>;
|
|
7
6
|
export declare const saveFileAsUtf8: (inputFilePath: string, outputFilePath: string) => Promise<void>;
|
package/dist/charcode.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var __importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.saveFileAsUtf8=exports.getBufferCharcodeName=exports.getFileCharcodeName=exports.getFileAllCharcode=
|
|
1
|
+
"use strict";var __importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.saveFileAsUtf8=exports.isUtf8File=exports.getBufferCharcodeName=exports.getFileCharcodeName=exports.getFileAllCharcode=void 0;const chardet_1=__importDefault(require("chardet")),fs_1=require("fs"),iconv_lite_1=__importDefault(require("iconv-lite")),buffer_1=require("./buffer"),file_1=require("./file"),getSampleBuffer=async e=>{const t=await(0,file_1.getFileSize)(e);let r=t-1;t>20480&&(r=20480);return await(0,buffer_1.getFileBufferSlice)(e,0,r)},parseCharcodeInfo=e=>{const t=e.find((e=>"UTF-8"===e.name)),r=e.find((e=>e.name.startsWith("GB")));return t?r?t.confidence>=r.confidence?t:r:t:r||e[0]},getFileAllCharcode=async e=>{const t=await getSampleBuffer(e);return chardet_1.default.analyse(t)};exports.getFileAllCharcode=getFileAllCharcode;const getFileCharcodeName=async e=>{const t=await(0,exports.getFileAllCharcode)(e);return parseCharcodeInfo(t).name};exports.getFileCharcodeName=getFileCharcodeName;const getBufferCharcodeName=e=>{const t=chardet_1.default.analyse(e);return parseCharcodeInfo(t).name};exports.getBufferCharcodeName=getBufferCharcodeName;const isUtf8File=async e=>"UTF-8"===await(0,exports.getFileCharcodeName)(e);exports.isUtf8File=isUtf8File;const saveFileAsUtf8=async(e,t)=>{await new Promise(((r,a)=>{const o=(0,fs_1.createReadStream)(e),i=(0,fs_1.createWriteStream)(t);o.on("error",(e=>{console.error(`读取文件时发生错误: ${e.message}`),a(e)})),i.on("error",(e=>{console.error(`写入文件时发生错误: ${e.message}`),a(e)})),(async()=>{try{const t=await(0,file_1.getFileSize)(e),s=Math.min(1023,t),c=await(0,buffer_1.getFileBufferSlice)(e,0,s),f=(0,exports.getBufferCharcodeName)(c);if("UTF-8"===f)o.pipe(i);else{const e=iconv_lite_1.default.decodeStream(f),t=iconv_lite_1.default.encodeStream("utf8");o.pipe(e).pipe(t).pipe(i).on("finish",(()=>{r(!0)})).on("error",(e=>{console.error(`转换文件时发生错误: ${e.message}`),a(e)}))}}catch(e){a(e)}})()}))};exports.saveFileAsUtf8=saveFileAsUtf8;
|