MCPcopy
hub / github.com/feross/buffer / fromString

Function fromString

index.js:232–254  ·  view source on GitHub ↗
(string, encoding)

Source from the content-addressed store, hash-verified

230}
231
232function fromString (string, encoding) {
233 if (typeof encoding !== 'string' || encoding === '') {
234 encoding = 'utf8'
235 }
236
237 if (!Buffer.isEncoding(encoding)) {
238 throw new TypeError('Unknown encoding: ' + encoding)
239 }
240
241 const length = byteLength(string, encoding) | 0
242 let buf = createBuffer(length)
243
244 const actual = buf.write(string, encoding)
245
246 if (actual !== length) {
247 // Writing a hex string, for example, that contains invalid characters will
248 // cause everything after the first invalid character to be ignored. (e.g.
249 // 'abxxcd' will be treated as 'ab')
250 buf = buf.slice(0, actual)
251 }
252
253 return buf
254}
255
256function fromArrayLike (array) {
257 const length = array.length < 0 ? 0 : checked(array.length) | 0

Callers 1

fromFunction · 0.85

Calls 2

byteLengthFunction · 0.85
createBufferFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…