Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

fs-iconv

bluelovers611ISC3.1.18TypeScript support: included

save file with charset/encoding/iconv

async, big-5, big5, buffer, charset, chinese, convert, copy, create, decode, delete, directory, encode, encoding, euc-jp, extra, file, file system, fs, gb2312, gbk, iconv, iconv-lite, icu, json, mkdir, mkdirp, mkdirs, move, output, read, recursive, remove, shift_jis, stream, string, sync, text, touch, unicode, utf-8, write

readme

fs-iconv

save file with charset/encoding/iconv

npm install fs-iconv

demo

fs same as fs-extra

import * as fs from 'fs-iconv';

fs.saveFile

(async ()=>
{

    /**
     * no need set encoding
     */
    let big5_2 = await fs.readFile('./res/big5.txt');
    fs.saveFile('./temp/out/_big5_2.txt', big5_2);

    /**
     * will fail if didn't set encoding
     */
    let big5 = "\xa6\xb8\xb1\x60\xa5\xce\xb0\xea\xa6\x72\xbc\xd0\xb7\xc7\xa6\x72\xc5\xe9\xaa\xed";
    fs.saveFile('./temp/out/_big5.txt', big5, {
        encoding: 'big5',
    });

})();

fs.loadFile

fs.loadFile('./res/big5.txt', {
    autoDecode: true,
})
    .then(function (buf)
    {
        // autoDecode, so will try decode/encode to utf8 buffer

        console.log('[autoDecode]');
        console.log(Buffer.isBuffer(buf), buf);
        console.log(fs.iconv.detect(buf));
        console.log(buf.toString());
    })
;

fs.loadFile('./res/big5.txt', {
        encoding: 'big5',
    })
    .then(function (buf)
    {
        // encoding is set, so will return string

        console.log('[encoding:big5]');
        console.log(Buffer.isBuffer(buf), buf);
        console.log(fs.iconv.detect(buf));
        console.log(buf.toString());
    })
;

fs.loadFile('./res/big5.txt', {
        autoDecode: ['gbk'],
    })
    .then(function (buf)
    {
        // buf is big5, but only allow decode gbk, so skip decode/encode

        console.log('[autoDecode:gbk]');
        console.log(Buffer.isBuffer(buf), buf);
        console.log(fs.iconv.detect(buf));
        console.log(buf.toString());
    })
;
[autoDecode]
true <Buffer e6 ac a1 e5 b8 b8 e7 94 a8 e5 9c 8b e5 ad 97 e6 a8 99 e6 ba 96 e5 ad 97 e9 ab 94 e8 a1 a8>
{ encoding: 'UTF-8', confidence: 0.99, name: 'UTF-8' }
次常用國字標準字體表
[encoding:big5]
false '次常用國字標準字體表'
{ encoding: 'ascii', confidence: 1, name: 'ascii' }
次常用國字標準字體表
[autoDecode:gbk]
true <Buffer a6 b8 b1 60 a5 ce b0 ea a6 72 bc d0 b7 c7 a6 72 c5 e9 aa ed>
{ encoding: 'Big5', confidence: 0.99, name: 'Big5' }
���`�ΰ�r�зǦr���

iconv

import { iconv } from 'fs-iconv';

import * as fs from 'fs-iconv';
const iconv = fs.iconv;

see iconv-jschardet

demo 2

自動判斷 檔案編碼 並且轉成 UTF8

//import fs from 'fs-iconv';
import fs = require('fs-iconv');
import Bluebird = require('bluebird');
// @ts-ignore
import globby from 'node-novel-globby/g';
import { EnumEncoding } from 'iconv-jschardet';

import { SymFSLib } from 'fs-iconv/core';

Bluebird.mapSeries(globby.async([
    '*.txt',
], {
    cwd: 'C:/Home/link/dist_novel/syosetu/悠閑農家與亂碼技能/00000_null',
    absolute: true,
}), async function (file: string)
{
    let buf = await fs.loadFile(file);

    /**
     * 自動判斷 檔案編碼 並且轉成 UTF8
     * 適合用在檔案的編碼不統一混亂時
     * 一個一個去肉眼檢查編碼會很累人
     */
    return fs.saveFile(file, buf, {
            encoding: EnumEncoding.UTF8
        })
        .tap(function ()
        {
            console.log(file);
        })
})
;

changelog

Change Log

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

3.1.18 (2024-08-29)

🛠 Build System

📌 Dependencies

3.1.17 (2022-10-16)

🛠 Build System

3.1.16 (2022-10-02)

🔖 Miscellaneous

3.1.15 (2022-09-29)

🔖 Miscellaneous

3.1.14 (2022-09-29)

🔖 Miscellaneous

3.1.13 (2022-09-28)

🚨 Tests

🔖 Miscellaneous

3.1.12 (2022-08-11)

♻️ Chores

📌 Dependencies

3.1.11 (2022-07-30)

Note: Version bump only for package fs-iconv

3.1.10 (2022-07-26)

📌 Dependencies

3.1.9 (2022-05-11)

♻️ Chores

3.1.8 (2021-12-29)

🛠 Build System

⚙️ Continuous Integration

🔖 Miscellaneous

3.1.7 (2021-12-06)

♻️ Chores

3.1.6 (2021-08-30)

Note: Version bump only for package fs-iconv

3.1.5 (2021-08-20)

Note: Version bump only for package fs-iconv

3.1.4 (2021-08-20)

Note: Version bump only for package fs-iconv

3.1.3 (2021-08-13)

♻️ Chores

3.1.2 (2021-07-23)

♻️ Chores

3.1.1 (2021-07-17)

📦 Code Refactoring

  • use @lazy-node/sanitize-filename (93c98c8)

BREAKING CHANGE

  • _ => !

3.0.16 (2021-07-08)

🛠 Build System

3.0.15 (2021-06-16)

♻️ Chores

🔖 Miscellaneous

3.0.14 (2021-06-15)

♻️ Chores

🔖 Miscellaneous

3.0.13 (2021-06-15)

♻️ Chores

3.0.12 (2021-02-12)

♻️ Chores

3.0.11 (2020-08-08)

🛠 Build System

♻️ Chores

3.0.10 (2020-07-05)

🔖 Miscellaneous

3.0.9 (2020-06-20)

📦 Code Refactoring

3.0.8 (2020-06-16)

🔖 Miscellaneous

3.0.7 (2020-06-09)

Bug Fixes

3.0.6 (2020-06-09)

Note: Version bump only for package fs-iconv

3.0.5 (2020-05-29)

Note: Version bump only for package fs-iconv

3.0.4 (2020-05-03)

Note: Version bump only for package fs-iconv

3.0.3 (2020-05-01)

Note: Version bump only for package fs-iconv

3.0.2 (2020-05-01)

Note: Version bump only for package fs-iconv

3.0.1 (2020-05-01)

Bug Fixes

Code Refactoring

  • update typescript and test (97dcebd)
  • update typescript and test (5b661d9)

BREAKING CHANGES

  • update typescript
  • update typescript