Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

charset

node-modules4.7mMIT1.0.1TypeScript support: definitely-typed

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

charset, content-type, ContentType, Content-Type, xml, encoding

readme

charset

NPM version build status Test coverage David deps npm download

logo

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

Install

$ npm install charset --save

Usage

Detect charset from http client response and content

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

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:

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

changelog

1.0.1 / 2017-09-07

fixes

others

1.0.0 / 2014-09-17

  • add peek size, default is 512. fixed #4

0.1.0 / 2014-07-05

  • support charset(content-type-string)
  • update AUTHORS with new version of contributors

0.0.2 / 2014-01-19

  • add contributors
  • 1 #2 read charset from encoding="utf8" for xml, handle spaces between = and inside of utf8 (@kof)

0.0.1 / 2012-10-08

  • first commit for charset.
  • Initial commit