diff --git a/doc/api/buffer.md b/doc/api/buffer.md index 4488ae2ad9bf8d..3a04ad514b4d4e 100644 --- a/doc/api/buffer.md +++ b/doc/api/buffer.md @@ -908,6 +908,44 @@ console.log(buf2.toString()); A `TypeError` will be thrown if `str` is not a string. +### Class Method: Buffer.from(object[, offsetOrEncoding[, length]]) + + +* `object` {Object} An object supporting `Symbol.toPrimitive` or `valueOf()` +* `offsetOrEncoding` {number|string} A byte-offset or encoding, depending on + the value returned either by `object.valueOf()` or + `object[Symbol.toPrimitive]()`. +* `length` {number} A length, depending on the value returned either by + `object.valueOf()` or `object[Symbol.toPrimitive]()`. + +For objects whose `valueOf()` function returns a value not strictly equal to +`object`, returns `Buffer.from(object.valueOf(), offsetOrEncoding, length)`. + +For example: + +```js +const buf = Buffer.from(new String('this is a test')); +// +``` + +For objects that support `Symbol.toPrimitive`, returns +`Buffer.from(object[Symbol.toPrimitive](), offsetOrEncoding, length)`. + +For example: + +```js +class Foo { + [Symbol.toPrimitive]() { + return 'this is a test'; + } +} + +const buf = Buffer.from(new Foo(), 'utf8'); +// +``` + ### Class Method: Buffer.isBuffer(obj)