# node-icu-charset-detector

> Simple binding for ICU charset detector

Latest version **0.2.0** (published 2016-05-10) · 0 weekly downloads

## Install

```sh
npm install node-icu-charset-detector
pnpm add node-icu-charset-detector
yarn add node-icu-charset-detector
bun add node-icu-charset-detector
```

## Health

**Score 10/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; low quality score; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2016-05-10 |
| First published | 2012-05-17 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=0.6 |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | yes |
| GitHub stars | 84 |
| Author | Masafumi Oyamada |
| Maintainers | mooz |
| Keywords | charset-detection, icu |

## Links

- npm: https://www.npmjs.com/package/node-icu-charset-detector
- Repository: https://github.com/mooz/node-icu-charset-detector
- Homepage: http://github.com/mooz/node-icu-charset-detector
- Issues: http://github.com/mooz/node-icu-charset-detector/issues
- npm.io page: https://npm.io/package/node-icu-charset-detector

## Dependencies (1)

- [nan](https://npm.io/package/nan.md) ^2.3.3

## Recent versions

- 0.2.0 (latest) — 2016-05-10
- 0.1.4 — 2015-12-13
- 0.1.3 — 2015-09-22
- 0.1.2 — 2015-09-20
- 0.1.1 — 2015-09-09
- 0.1.0 — 2015-02-18
- 0.0.7 — 2013-09-04
- 0.0.6 — 2013-02-23
- 0.0.5 — 2012-11-26
- 0.0.4 — 2012-11-24
- 0.0.3 — 2012-05-17
- 0.0.2 — 2012-05-17
- 0.0.1 — 2012-05-17

## README

# ICU Character Set Detection for Node.js

>Character set detection is the process of determining the character set, or encoding, of character data in an unknown format.

A simple binding of ICU character set detection (http://userguide.icu-project.org/conversion/detection) for Node.js.

## Installation

At first, install `libicu` into your system (See [this instruction](#installing-icu) for details).

After that, install `node-icu-charset-detector` from npm.

    npm install node-icu-charset-detector

### Installing ICU

#### Linux

* Debian (Ubuntu)

    apt-get install libicu-dev

* Gentoo

    emerge icu

* Fedora/CentOS

    yum install libicu-devel

#### OSX

* MacPorts

    port install icu +devel

* Homebrew

```shell
brew install icu4c
brew link icu4c --force
```

If experiencing issues with 'homebrew' installing version 50.1 of icu4c, try the following:

```shell
brew search icu4c
brew tap homebrew/versions
brew versions icu4c
cd $(brew --prefix) && git pull --rebase
git checkout c25fd2f $(brew --prefix)/Library/Formula/icu4c.rb
brew install icu4c
```

* From source

```shell
curl -O http://download.icu-project.org/files/icu4c/52.1/icu4c-52_1-src.tgz
tar xzvf icu4c-4_4_2-src.tgz
cd icu/source
chmod +x runConfigureICU configure install-sh
./runConfigureICU MacOSX
make
sudo make install
xcode-select --install
```

## Usage

### Simple usage

`node-icu-charset-detector` provides a function `detectCharset(buffer)`, where `buffer` is an instance of `Buffer` whose charset should be detected.

```javascript
var charsetDetector = require("node-icu-charset-detector");

var buffer = fs.readFileSync("/path/to/the/file");
var charset = charsetDetector.detectCharset(buffer);

console.log("charset name: " + charset.toString());
console.log("language: " + charset.language);
console.log("detection confidence: " + charset.confidence);
```

`detectCharset(buffer)` returns the detected charset name for `buffer`, and the returned charset name has two extra properties `language` and `confidence`:

- `charset.language`
  - language name for the detected character set.
- `charset.confidence`
  - confidence of the charset detection for `charset`.

### Leveraging node-iconv

Since ICU itself does not have a feature to convert character sets, you may need to use `node-iconv` (https://github.com/bnoordhuis/node-iconv), which has a powerful character sets converting feature.

Here is a simple example to leverage `node-iconv` to convert character sets not supported by Node itself.

```javascript
function bufferToString(buffer) {
  var charsetDetector = require("node-icu-charset-detector");
  var charset = charsetDetector.detectCharset(buffer).toString();

  try {
    return buffer.toString(charset);
  } catch (x) {
    var Iconv = require("iconv").Iconv;
    var charsetConverter = new Iconv(charset, "utf8");
    return charsetConverter.convert(buffer).toString();
  }
}

var buffer = fs.readFileSync("/path/to/the/file");
var bufferString = bufferToString(buffer);
```

---
_Source: https://npm.io/package/node-icu-charset-detector · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
