We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 8c8708c commit d04246aCopy full SHA for d04246a
lib/buffer.js
@@ -780,13 +780,22 @@ function byteLength(string, encoding) {
780
if (len === 0)
781
return 0;
782
783
- if (encoding) {
784
- const ops = getEncodingOps(encoding);
785
- if (ops) {
786
- return ops.byteLength(string);
787
- }
+ if (!encoding || encoding === 'utf8') {
+ return byteLengthUtf8(string);
+ }
+
+ if (encoding === 'ascii') {
788
+ return len;
789
}
- return byteLengthUtf8(string);
790
791
+ const ops = getEncodingOps(encoding);
792
+ if (ops === undefined) {
793
+ // TODO (ronag): Makes more sense to throw here.
794
+ // throw new ERR_UNKNOWN_ENCODING(encoding);
795
796
797
798
+ return ops.byteLength(string);
799
800
801
Buffer.byteLength = byteLength;
0 commit comments