# charset

> Get the content charset from header and html content-type.

Latest version **1.0.1** (published 2017-09-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install charset
pnpm add charset
yarn add charset
bun add charset
```

## Health

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

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2017-09-07 |
| First published | 2012-10-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/charset) |
| Module format | CommonJS |
| Node | >=4.0.0 |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 32 |
| Author | fengmk2 |
| Maintainers | fengmk2 |
| Keywords | charset, content-type, ContentType, Content-Type, xml, encoding |

## Links

- npm: https://www.npmjs.com/package/charset
- Repository: https://github.com/node-modules/charset
- Issues: https://github.com/node-modules/charset/issues
- npm.io page: https://npm.io/package/charset

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 1.0.1 (latest) — 2017-09-07
- 1.0.0 — 2014-09-17
- 0.1.0 — 2014-07-05
- 0.0.2 — 2014-01-19
- 0.0.1 — 2012-10-08

## README

charset
=======

[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][codecov-image]][codecov-url]
[![David deps][david-image]][david-url]
[![npm download][download-image]][download-url]

[npm-image]: https://img.shields.io/npm/v/charset.svg?style=flat-square
[npm-url]: https://npmjs.org/package/charset
[travis-image]: https://img.shields.io/travis/node-modules/charset.svg?style=flat-square
[travis-url]: https://travis-ci.org/node-modules/charset
[codecov-image]: https://codecov.io/gh/node-modules/charset/branch/master/graph/badge.svg
[codecov-url]: https://codecov.io/gh/node-modules/charset
[david-image]: https://img.shields.io/david/node-modules/charset.svg?style=flat-square
[david-url]: https://david-dm.org/node-modules/charset
[download-image]: https://img.shields.io/npm/dm/charset.svg?style=flat-square
[download-url]: https://npmjs.org/package/charset

![logo](https://raw.github.com/node-modules/charset/master/logo.png)

Get the content charset from header and html content-type.

## Install

```bash
$ npm install charset --save
```

## Usage

### Detect charset from http client response and content

```js
var charset = require('charset');
var http = require('http');

http.get('http://nodejs.org', function (res) {
  res.on('data', function (chunk) {
    console.log(charset(res.headers, chunk));
    // or `console.log(charset(res, chunk));`
    res.destroy();
  });
});
```

Stdout will should log: `utf8` .

### Detect from String

```js
charset(res.headers['content-type']);
```

### Detect combine with [jschardet]

As you know, `charset` only detect from http response headers and html content-type meta tag.
You can combine with [jschardet] to help you detect the finally charset.

This example codes come from [stackoverflow#12326688](http://stackoverflow.com/a/18712021/2496088):

```js
var request = require('request');
var charset = require('charset');
var jschardet = require('jschardet');

request({
  url: 'http://www.example.com',
  encoding: null
}, function (err, res, body) {
  if (err) {
    throw err;
  }
  enc = charset(res.headers, body);
  enc = enc || jschardet.detect(body).encoding.toLowerCase();
  console.log(enc);
});
```

## License

[MIT](LICENSE.txt)

[jschardet]: https://github.com/aadsm/jschardet

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