Code coverage report for charset/index.js

# all files / charset/ index.js

100% Statements 25/25
100% Branches 24/24
100% Functions 1/1
100% Lines 25/25

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60

    1×

    1×

    1×
    18×
    18×
    18×
    15×

    15×

    18×
    18×

    16×
    16×

    1×

    16×

    18×

    9×

    18×

    9×
    9×

    18×
    18×
    15×
    15×
    8×

    18×

     'use strict';

    const CHARTSET_RE = /(?:charset|encoding)\s*=\s*['"]? *([\w\-]+)/i;

    module.exports = charset;

    /**
     * guest data charset from req.headers, xml, html content-type meta tag
     * headers:
     *  'content-type': 'text/html;charset=gbk'
     * meta tag:
     *  <meta http-equiv="Content-Type" content="text/html; charset=xxxx"/>
     * xml file:
     *  <?xml version="1.0" encoding="UTF-8"?>
     *
     * @param {Object} obj `Content-Type` String, or `res.headers`, or `res` Object
     * @param {Buffer} [data] content buffer
     * @param {Number} [peekSize] max content peek size, default is 512
     * @return {String} charset, lower case, e.g.: utf8, gbk, gb2312, ....
     *  If can\'t guest, return null
     * @api public
     */
    function charset(obj, data, peekSize) {
      let matchs = null;
      let end = 0;
      if (data) {
        peekSize = peekSize || 512;
        // https://github.com/node-modules/charset/issues/4
        end = data.length > peekSize ? peekSize : data.length;
      }
      // charset('text/html;charset=gbk')
      let contentType = obj;
      if (contentType && typeof contentType !== 'string') {
        // charset(res.headers)
        let headers = obj;
        if (obj.headers) {
          // charset(res)
          headers = obj.headers;
        }
        contentType = headers['content-type'] || headers['Content-Type'];
      }
      if (contentType) {
        // guest from obj first
        matchs = CHARTSET_RE.exec(contentType);
      }
      if (!matchs && end > 0) {
        // guest from content body (html/xml) header
        contentType = data.slice(0, end).toString();
        matchs = CHARTSET_RE.exec(contentType);
      }
      let cs = null;
      if (matchs) {
        cs = matchs[1].toLowerCase();
        if (cs === 'utf-8') {
          cs = 'utf8';
        }
      }
      return cs;
    }