How to use utf8 - 10 common examples

To help you get started, we’ve selected a few utf8 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ooooevan / Vmail / src / renderer / common / javascript / parseEmail.js View on Github external
}
        const contentType = text.match(contentTypeReg)[1].trim()
        const charset = (text.match(secondChartsetReg) || text.match(charsetReg))[1].trim().replace(/"/g, '')
        const transferEncoding = text.match(contentTransferEncodingReg)[1].trim()
        const subTextArr = text.split(blankLineReg)
        subTextArr.shift()
        const contentText = subTextArr.filter(item => (item.trim())).join('')
        if (contentType.match(htmlTypeReg)) {
          if (transferEncoding === 'base64') {
            const buff = Buffer.from(contentText, transferEncoding)
            result.bodyHtml = iconv.decode(buff, charset || 'utf-8')
          } else if (transferEncoding === 'quoted-printable') {
            // 用try包裹是因为发现有邮件写着quoted-printable编码,正文却没有编码。quotedPrintable模块decode没有编码的内容就报错,进入catch
            try {
              // 编码为quoted-printable且charset为utf8。暂时遇到为utf8的,未看到其他字符集
              const quotedHtml = utf8.decode(quotedPrintable.decode(contentText))
              result.bodyHtml = quotedHtml
            } catch (e) {
              result.bodyHtml = contentText
            }
          } else {
            result.bodyHtml = contentText
          }
        } else if (contentType.match(plainTypeReg)) {
          if (transferEncoding === 'base64') {
            const buff = Buffer.from(contentText, transferEncoding)
            result.bodyText += iconv.decode(buff, charset || 'utf-8')
          } else {
            try {
              // 编码为quoted-printable且charset为utf8。其他字符集用quotedPrintable会报错,进入catch
              const quotedText = utf8.decode(quotedPrintable.decode(contentText))
              result.bodyText += quotedText
github ethereum / web3.js / modules / core / src / utility / Utils.js View on Github external
hex = hex.replace(/^(?:00)*/, '');
    hex = hex
        .split('')
        .reverse()
        .join('');

    const l = hex.length;

    for (let i = 0; i < l; i += 2) {
        code = parseInt(hex.substr(i, 2), 16);
        // if (code !== 0) {
        string += String.fromCharCode(code);
        // }
    }

    return utf8.decode(string);
};
github mozilla / addons-frontend / src / core / api / index.js View on Github external
}
  }
  if (auth) {
    if (apiState.token) {
      options.headers.authorization = `Bearer ${apiState.token}`;
    }
  }

  adjustedEndpoint = adjustedEndpoint.endsWith('/')
    ? adjustedEndpoint
    : `${adjustedEndpoint}/`;
  let apiURL = `${config.get('apiHost')}${adjustedEndpoint}${queryString}`;
  if (_config.get('server')) {
    _log.debug('Encoding `apiURL` in UTF8 before fetch().');
    // Workaround for https://github.com/bitinn/node-fetch/issues/245
    apiURL = utf8.encode(apiURL);
  }

  return fetch(apiURL, options)
    .then((response) => {
      // There isn't always a 'Content-Type' in headers, e.g., with a DELETE
      // method or 5xx responses.
      let contentType = response.headers.get('Content-Type');
      contentType = contentType && contentType.toLowerCase();

      // This is a bit paranoid, but we ensure the API returns a JSON response
      // (see https://github.com/mozilla/addons-frontend/issues/1701).
      // If not we'll store the text response in JSON and log an error.
      // If the JSON parsing fails; we log the error and return an "unknown
      // error".
      if (contentType === 'application/json') {
        return response
github iamdjones / sublime-text-browser-sync / node_modules / browser-sync / node_modules / engine.io-parser / lib / index.js View on Github external
exports.decodePacket = function (data, binaryType, utf8decode) {
  // String data
  if (typeof data == 'string' || data === undefined) {
    if (data.charAt(0) == 'b') {
      return exports.decodeBase64Packet(data.substr(1), binaryType);
    }

    var type = data.charAt(0);
    if (utf8decode) {
      try {
        data = utf8.decode(data);
      } catch (e) {
        return err;
      }
    }

    if (Number(type) != type || !packetslist[type]) {
      return err;
    }

    if (data.length > 1) {
      return { type: packetslist[type], data: data.substring(1) };
    } else {
      return { type: packetslist[type] };
    }
  }
github jucimarjr / html5games / phaser / DominoMultiplayer / shared / socket.io / node_modules / engine.io / node_modules / engine.io-parser / lib / browser.js View on Github external
exports.decodePacket = function (data, binaryType, utf8decode) {
  // String data
  if (typeof data == 'string' || data === undefined) {
    if (data.charAt(0) == 'b') {
      return exports.decodeBase64Packet(data.substr(1), binaryType);
    }

    if (utf8decode) {
      try {
        data = utf8.decode(data);
      } catch (e) {
        return err;
      }
    }
    var type = data.charAt(0);

    if (Number(type) != type || !packetslist[type]) {
      return err;
    }

    if (data.length > 1) {
      return { type: packetslist[type], data: data.substring(1) };
    } else {
      return { type: packetslist[type] };
    }
  }
github popstarfreas / Dimensions / app / node_modules / dimensions / packets / bufferreader.ts View on Github external
public readString(): string {
        // Read string length
        const firstByte: number = this.readByte();
        let strLength: number = firstByte;
        if (firstByte >= 128) {
            const secondByte: number = this.readByte();
            strLength = (firstByte - 128) + secondByte << 7;
        }

        // Read string content using length
        const rawText = bufferToText(this._data.slice(this.head, this.head + strLength));
        const strContent: string = utf8.decode(rawText);
        this.head += strLength;
        return strContent;
    }
github ooooevan / Vmail / src / renderer / common / javascript / parseEmail.js View on Github external
const quotedHtml = utf8.decode(quotedPrintable.decode(contentText))
              result.bodyHtml = quotedHtml
            } catch (e) {
              result.bodyHtml = contentText
            }
          } else {
            result.bodyHtml = contentText
          }
        } else if (contentType.match(plainTypeReg)) {
          if (transferEncoding === 'base64') {
            const buff = Buffer.from(contentText, transferEncoding)
            result.bodyText += iconv.decode(buff, charset || 'utf-8')
          } else {
            try {
              // 编码为quoted-printable且charset为utf8。其他字符集用quotedPrintable会报错,进入catch
              const quotedText = utf8.decode(quotedPrintable.decode(contentText))
              result.bodyText += quotedText
            } catch (e) {
              // 不是quoted-printable,那html本身应该是utf-8编码而不是gb18030
              result.bodyText += iconv.decode(iconv.encode(contentText, 'gb18030'), 'utf-8')
            }
          }
        }
      })
    } else if (contentType.match(mixedMultipart) || (contentType.match(relatedMultipart) && textArr.length > 2)) {
github booknds / sge / node_modules / browser-sync / node_modules / socket.io / node_modules / engine.io / node_modules / engine.io-parser / lib / index.js View on Github external
exports.decodePacket = function (data, binaryType, utf8decode) {
  // String data
  if (typeof data == 'string' || data === undefined) {
    if (data.charAt(0) == 'b') {
      return exports.decodeBase64Packet(data.substr(1), binaryType);
    }

    var type = data.charAt(0);
    if (utf8decode) {
      try {
        data = utf8.decode(data);
      } catch (e) {
        return err;
      }
    }

    if (Number(type) != type || !packetslist[type]) {
      return err;
    }

    if (data.length > 1) {
      return { type: packetslist[type], data: data.substring(1) };
    } else {
      return { type: packetslist[type] };
    }
  }
github DefinitelyTyped / DefinitelyTyped / utf8 / utf8-tests.ts View on Github external
function test_version(): void {
    if (typeof utf8.version === "string") {
        console.log(utf8.version);
    }
}
github DefinitelyTyped / DefinitelyTyped / utf8 / utf8-tests.ts View on Github external
function test_version(): void {
    if (typeof utf8.version === "string") {
        console.log(utf8.version);
    }
}

utf8

A well-tested UTF-8 encoder/decoder written in JavaScript.

MIT
Latest version published 6 years ago

Package Health Score

71 / 100
Full package analysis

Popular utf8 functions