From: Andreas Madsen Date: Tue, 20 Aug 2013 13:46:25 +0000 (+0200) Subject: add Buffer.isEncoding method X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=03ee1c1660d2aeab5fc7a51a7921c9f411138ea5;p=buffer.git add Buffer.isEncoding method --- diff --git a/index.js b/index.js index ad42d41..aa3e524 100644 --- a/index.js +++ b/index.js @@ -417,6 +417,26 @@ Buffer.concat = function (list, totalLength) { return buffer; }; +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; + } +}; + // helpers function coerce(length) { diff --git a/test/is-encoding.js b/test/is-encoding.js new file mode 100644 index 0000000..5cf04b3 --- /dev/null +++ b/test/is-encoding.js @@ -0,0 +1,9 @@ + +var B = require('../index.js').Buffer; +var test = require('tape'); + +test('Buffer.isEncoding', function (t) { + t.equal(B.isEncoding('HEX'), true); + t.equal(B.isEncoding('hex'), true); + t.equal(B.isEncoding('bad'), false); +});