charset
Get the content charset from header and html content-type.
Last updated 7 years ago by fengmk2 .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ gnpm install charset 
SYNC missed versions from official npm registry.

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

Current Tags

  • 1.0.1                                ...           latest (7 years ago)

5 Versions

  • 1.0.1                                ...           7 years ago
  • 1.0.0                                ...           10 years ago
  • 0.1.0                                ...           10 years ago
  • 0.0.2                                ...           11 years ago
  • 0.0.1                                ...           12 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (4)
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |