Skip to content

Buffer.byteLength's implementation is not consistent with the documentation #38536

Closed
@pd4d10

Description

@pd4d10

node/lib/buffer.js

Lines 729 to 752 in f71166b

function byteLength(string, encoding) {
if (typeof string !== 'string') {
if (isArrayBufferView(string) || isAnyArrayBuffer(string)) {
return string.byteLength;
}
throw new ERR_INVALID_ARG_TYPE(
'string', ['string', 'Buffer', 'ArrayBuffer'], string
);
}
const len = string.length;
const mustMatch = (arguments.length > 2 && arguments[2] === true);
if (!mustMatch && len === 0)
return 0;
if (!encoding)
return (mustMatch ? -1 : byteLengthUtf8(string));
const ops = getEncodingOps(encoding);
if (ops === undefined)
return (mustMatch ? -1 : byteLengthUtf8(string));
return ops.byteLength(string);
}

In line 741 it checks the third argument mustMatch, which is not mentioned in the document

So I guess either the implementation or the documentation should be updated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bufferIssues and PRs related to the buffer subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions