Browse Source

Buffer.isEncoding(enc)

Re: #3918
v0.9.1-release
isaacs 12 years ago
parent
commit
05282588e0
  1. 7
      doc/api/buffer.markdown
  2. 23
      lib/buffer.js
  3. 21
      test/simple/test-buffer.js

7
doc/api/buffer.markdown

@ -65,6 +65,13 @@ Allocates a new buffer using an `array` of octets.
Allocates a new buffer containing the given `str`.
`encoding` defaults to `'utf8'`.
### Class Method: Buffer.isEncoding(encoding)
* `encoding` {String} The encoding string to test
Returns true if the `encoding` is a valid encoding argument, or false
otherwise.
### buf.write(string, [offset], [length], [encoding])
* `string` String - data to be written to buffer

23
lib/buffer.js

@ -274,6 +274,29 @@ function isArrayIsh(subject) {
exports.SlowBuffer = SlowBuffer;
exports.Buffer = Buffer;
Buffer.isEncoding = function(encoding) {
switch (encoding && 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;
}
};
Buffer.poolSize = 8 * 1024;
var pool;

21
test/simple/test-buffer.js

@ -727,3 +727,24 @@ assert.equal(b.toString(), 'xxx');
// issue GH-3416
Buffer(Buffer(0), 0, 0);
[ 'hex',
'utf8',
'utf-8',
'ascii',
'binary',
'base64',
'ucs2',
'ucs-2',
'utf16le',
'utf-16le' ].forEach(function(enc) {
assert.equal(Buffer.isEncoding(enc), true);
});
[ 'utf9',
'utf-7',
'Unicode-FTW',
'new gnu gun' ].forEach(function(enc) {
assert.equal(Buffer.isEncoding(enc), false);
});

Loading…
Cancel
Save