1 /* Node.js 6.4.0 and up has full support */
2 var hasFullSupport = (function () {
4 if (!Buffer.isEncoding('latin1')) {
8 var buf = Buffer.alloc ? Buffer.alloc(4) : new Buffer(4)
10 buf.fill('ab', 'ucs2')
12 return (buf.toString('hex') === '61006200')
18 function isSingleByte (val) {
19 return (val.length === 1 && val.charCodeAt(0) < 256)
22 function fillWithNumber (buffer, val, start, end) {
23 if (start < 0 || end > buffer.length) {
24 throw new RangeError('Out of range index')
28 end = end === undefined ? buffer.length : end >>> 0
31 buffer.fill(val, start, end)
37 function fillWithBuffer (buffer, val, start, end) {
38 if (start < 0 || end > buffer.length) {
39 throw new RangeError('Out of range index')
47 end = end === undefined ? buffer.length : end >>> 0
51 while (pos <= (end - len)) {
57 val.copy(buffer, pos, 0, end - pos)
63 function fill (buffer, val, start, end, encoding) {
65 return buffer.fill(val, start, end, encoding)
68 if (typeof val === 'number') {
69 return fillWithNumber(buffer, val, start, end)
72 if (typeof val === 'string') {
73 if (typeof start === 'string') {
77 } else if (typeof end === 'string') {
82 if (encoding !== undefined && typeof encoding !== 'string') {
83 throw new TypeError('encoding must be a string')
86 if (encoding === 'latin1') {
90 if (typeof encoding === 'string' && !Buffer.isEncoding(encoding)) {
91 throw new TypeError('Unknown encoding: ' + encoding)
95 return fillWithNumber(buffer, 0, start, end)
98 if (isSingleByte(val)) {
99 return fillWithNumber(buffer, val.charCodeAt(0), start, end)
102 val = new Buffer(val, encoding)
105 if (Buffer.isBuffer(val)) {
106 return fillWithBuffer(buffer, val, start, end)
109 // Other values (e.g. undefined, boolean, object) results in zero-fill
110 return fillWithNumber(buffer, 0, start, end)
113 module.exports = fill