@apistudio/apim-cli
Version:
CLI for API Management Products
86 lines (64 loc) • 2.3 kB
Markdown
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]
[]: https://img.shields.io/npm/v/charset.svg?style=flat-square
[]: https://npmjs.org/package/charset
[]: https://img.shields.io/travis/node-modules/charset.svg?style=flat-square
[]: https://travis-ci.org/node-modules/charset
[]: https://codecov.io/gh/node-modules/charset/branch/master/graph/badge.svg
[]: https://codecov.io/gh/node-modules/charset
[]: https://img.shields.io/david/node-modules/charset.svg?style=flat-square
[]: https://david-dm.org/node-modules/charset
[]: https://img.shields.io/npm/dm/charset.svg?style=flat-square
[]: https://npmjs.org/package/charset

Get the content charset from header and html content-type.
```bash
$ npm install charset --save
```
```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` .
```js
charset(res.headers['content-type']);
```
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
```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.txt)
[]: https://github.com/aadsm/jschardet