DIV CSS 佈局教程網

Node.js 中 Buffer.isEncoding 方法詳解與使用教學
編輯:關於JavaScript     

方法說明

用來檢測某個字串是否為 Node 支援的有效編碼名稱,回傳 true 或 false。呼叫 toString、write 等方法前,用它驗證編碼參數是很實用的防禦寫法。

語法

Buffer.isEncoding(encoding)

參數說明

encoding {String}:被檢測的編碼格式名稱。

範例

var a = Buffer.isEncoding('base64');

console.log(a);   // true

原始碼

從實作可以列出所有支援的編碼名稱,注意它會先把輸入轉小寫再比對,因此大小寫不敏感。

Buffer.isEncoding = function(encoding) {
  switch ((encoding + '').toLowerCase()) {
    case 'hex':
    case 'utf8':
    case 'utf-8':
    case 'ascii':
    case 'binary':
    case 'base64':
    case 'ucs2':
    case 'ucs-2':
    case 'utf16le':
    case 'utf-16le':
    case 'raw':
      return true;
    default:
      return false;
  }
};
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved