npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

charset-changer

v2.0.0

Published

Change the charset for an entire folder recursively

Downloads

25

Readme

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:

const { charsetChanger } = require('charset-changer')

if you want to use sync (file-by-file) convertion, use:

const { charsetChangerSync } = require('charset-changer')

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

    {
        root: string, // root path.
        search?: GlobString, // glob string (see on "How it works" section).
        ignore?: GlobString, // glob string.
        from?: Charset|string, // From charset. Default: Charset Detector.
        to: Charset|string, // To charset.
        createBackup?: boolean, // create backup? Default is false.
        backupSuffix?: string, // backup suffix. Default is ".bkp".
        onList?: OnList, // On get list of files listener.
        onBeforeConvert?: OnBeforeConvert, // On before convert listener.
        onAfterConvert?: OnAfterConvert, // On after convert listener.
        onFinish?: OnFinish, // On finish listener.
        debug?: boolean, // Show messages.
        detectorFilter?: DetectorFilter // Filter files by charset detection.
    }

See the example here.

Class

Importing the class

To use charset-changer, import script with this code:

const { CharsetChanger } = require('charset-changer') // Uppercase 'C'
/* OR */
const CharsetChanger = require('charset-changer').Class

Methods

Instantiate the class and configure the instance using the accessors of the object:

public root(): FilePath;
public root(root: FilePath): this;

public search(): GlobString;
public search(search: GlobString): this;

public ignore(): GlobString;
public ignore(ignore: GlobString): this;

public from(): Charset;
public from(from: Charset): this;

public to(): Charset;
public to(to: Charset): this;

public debug(): boolean;
public debug(debug: boolean): this;

public backup(): string;
public backup(backupSuffix: string): this;
public backup(createBackup: boolean): this;
public backup(backupSuffix: string, createBackup: boolean): this;

public onList(onList: OnList): this;
public onBeforeConvert(onBeforeConvert: OnBeforeConvert): this;
public onAfterConvert(onAfterConvert: OnAfterConvert): this;
public onFinish(onFinish: OnFinish): this;
public setDetectorFilter(detectorFilter: DetectorFilter): this;

public setConfig(config: 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:

const { Charset } = require('charset-changer')

Usage Examples

Async static method

const { charsetChangerSync, charsetChanger, Charset } = require('charsetChanger');

charsetChanger({
    root: 'test/output', // Root path
    search: '**/example*.txt', // Glob string
    from: Charset.UTF8, // from utf-8
    to: Charset.ISO8859_1, // to iso8859-1
    createBackup: true,
    backupSuffix: '.backup',
    onFinish: () => { console.log("utf8 to iso8859-1"); }
});

Async Class Method

const { CharsetChanger, Charset } = require('charsetChanger');

let charsetChanger = new CharsetChanger();

/* ---------------------- */

charsetChanger
    .root('test/output')
    .search('**/example*.txt')
    .from(Charset.UTF8 /* OR */ 'utf8')
    .to(Charset.ISO8859_1 /* OR */ 'iso8859-1')
    .backup('.backup'       // OR
            '.backup', true // OR JUST 
            true)           // DEFAULT SUFFIX IS .bkp
    .onFinish(() => { console.log("utf8 to iso8859-1"); })
    .convert(); // <====== CONVERTING

/* OR */

charsetChanger.setConfig({
    root: 'test/output', // Root path
    search: '**/example*.txt', // Glob string
    from: Charset.UTF8, // from utf-8
    to: Charset.ISO8859_1, // to iso8859-1
    createBackup: true,
    backupSuffix: '.backup',
    onFinish: () => { console.log("utf8 to iso8859-1"); }
}).convert(); // <====== CONVERTING

/* ---------------------- */

See more usages into the test folder.

See more

See CharsetChanger GUI.

Author

License

MIT License