From 03ee1c1660d2aeab5fc7a51a7921c9f411138ea5 Mon Sep 17 00:00:00 2001 From: Andreas Madsen Date: Tue, 20 Aug 2013 15:46:25 +0200 Subject: [PATCH] add Buffer.isEncoding method --- index.js | 20 ++++++++++++++++++++ test/is-encoding.js | 9 +++++++++ 2 files changed, 29 insertions(+) create mode 100644 test/is-encoding.js 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); +}); -- 2.34.1