Change the charset for an entire folder recursively
Objective
The objective of this library is help to solve some charset issues in projects with many workers. For this purpose, this library helps to convert all necessary files to the correct charset.
Install
npm i charset-changer
Getting Started
Static
Importing the static instance
To use charset-changer directly, import script with this code:
if you want to use sync (file-by-file) convertion, use:
If async call are used, the onFinish
status always will be true
and you will not be able to cancel conversion with onAfterConvert
.
Using
The charsetChanger
have only one argument, the CharsetChanger.Config
. Use it to configure the library the way you want. This call will configure and convert the files automaticaly.
CharsetChanger.Config
See the example here.
Class
Importing the class
To use charset-changer, import script with this code:
/* OR */
Methods
Instantiate the class and configure the instance using the accessors
of the object:
public root: FilePath;public rootroot: FilePath: this; public search: GlobString;public searchsearch: GlobString: this; public ignore: GlobString;public ignoreignore: GlobString: this; public from: Charset;public fromfrom: Charset: this; public to: Charset;public toto: Charset: this; public debug: boolean;public debugdebug: boolean: this; public backup: string;public backupbackupSuffix: string: this;public backupcreateBackup: boolean: this;public backupbackupSuffix: string, createBackup: boolean: this; public onListonList: OnList: this;public onBeforeConvertonBeforeConvert: OnBeforeConvert: this;public onAfterConvertonAfterConvert: OnAfterConvert: this;public onFinishonFinish: OnFinish: this;public setDetectorFilterdetectorFilter: DetectorFilter: this; public setConfigconfig: CharsetChanger.Config: this;
To convert you folder (or project) use the convertion classes:
public convert: void;public async convertSync: Promise<void>; // use await
See the example here.
How it works
This lib uses glob
, iconv-lite
and chardet
to automate charset changes into an folder (or project) recursively.
With the glob
features, you gonna use an complex search string to get filtered files into the root path.
Glob string example
- *.* - All files into the root path;
- *.txt - All
.txt
files into the root path; - **/* - All files (recursively) into the root path;
- **/*.txt - All
.txt
files (recursively) into the root path.
The iconv-lite
is responsible to the charset decode and encode, so every charset supported by iconv is supported by CharsetChanger. (See the supported encodings here [pay attention to the version used]).
The CharsetChanger has an enum with the principal charsets supported but not limited to that. The Charset
enum is disponible into the module.
To use the Charset
enum import using:
Usage Examples
Async static method
; charsetChanger;
Async Class Method
; ; /* ---------------------- */ charsetChanger .root'test/output' .search'**/example*.txt' .fromCharset.UTF8 /* OR */ 'utf8' .toCharset.ISO8859_1 /* OR */ 'iso8859-1' .backup'.backup' // OR '.backup', true // OR JUST true // DEFAULT SUFFIX IS .bkp .onFinish .convert; // <====== CONVERTING /* OR */ charsetChanger.setConfig.convert; // <====== CONVERTING /* ---------------------- */
See more usages into the test folder.
See more
See CharsetChanger GUI.
Author
- Created and maintained by Emerson C. Romaneli (@ECRomaneli).