From 1164d691501fbc309193a015b393f529a725ea3d Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Mon, 20 Apr 2015 13:46:41 -0700 Subject: [PATCH] improve Buffer.byteLength(string, encoding) From https://github.com/iojs/io.js/pull/1441/files --- index.js | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/index.js b/index.js index 46f5a98..bbb7343 100644 --- a/index.js +++ b/index.js @@ -205,35 +205,31 @@ Buffer.concat = function concat (list, totalLength) { return buf } -Buffer.byteLength = function byteLength (str, encoding) { - var ret - str = str + '' +Buffer.byteLength = function byteLength (string, encoding) { + if (typeof string !== 'string') string = String(string) + + if (string.length === 0) return 0 + switch (encoding || 'utf8') { case 'ascii': case 'binary': case 'raw': - ret = str.length - break + return string.length case 'ucs2': case 'ucs-2': case 'utf16le': case 'utf-16le': - ret = str.length * 2 - break + return string.length * 2 case 'hex': - ret = str.length >>> 1 - break + return string.length >>> 1 case 'utf8': case 'utf-8': - ret = utf8ToBytes(str).length - break + return utf8ToBytes(string).length case 'base64': - ret = base64ToBytes(str).length - break + return base64ToBytes(string).length default: - ret = str.length + return string.length } - return ret } // pre-set for values that may exist in the future -- 2.34.1