]> zoso.dev Git - buffer.git/commitdiff
add Buffer.isEncoding method
authorAndreas Madsen <amwebdk@gmail.com>
Tue, 20 Aug 2013 13:46:25 +0000 (15:46 +0200)
committerAndreas Madsen <amwebdk@gmail.com>
Tue, 20 Aug 2013 13:46:25 +0000 (15:46 +0200)
index.js
test/is-encoding.js [new file with mode: 0644]

index ad42d4165b58c905560254992da19b01b5d4c260..aa3e5245bba79030e519c142d4be1db176d1d5a0 100644 (file)
--- 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 (file)
index 0000000..5cf04b3
--- /dev/null
@@ -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);
+});