You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* Node.js 6.4.0 and up has full support */
  2. var hasFullSupport = (function () {
  3. try {
  4. if (!Buffer.isEncoding('latin1')) {
  5. return false
  6. }
  7. var buf = Buffer.alloc ? Buffer.alloc(4) : new Buffer(4)
  8. buf.fill('ab', 'ucs2')
  9. return (buf.toString('hex') === '61006200')
  10. } catch (_) {
  11. return false
  12. }
  13. }())
  14. function isSingleByte (val) {
  15. return (val.length === 1 && val.charCodeAt(0) < 256)
  16. }
  17. function fillWithNumber (buffer, val, start, end) {
  18. if (start < 0 || end > buffer.length) {
  19. throw new RangeError('Out of range index')
  20. }
  21. start = start >>> 0
  22. end = end === undefined ? buffer.length : end >>> 0
  23. if (end > start) {
  24. buffer.fill(val, start, end)
  25. }
  26. return buffer
  27. }
  28. function fillWithBuffer (buffer, val, start, end) {
  29. if (start < 0 || end > buffer.length) {
  30. throw new RangeError('Out of range index')
  31. }
  32. if (end <= start) {
  33. return buffer
  34. }
  35. start = start >>> 0
  36. end = end === undefined ? buffer.length : end >>> 0
  37. var pos = start
  38. var len = val.length
  39. while (pos <= (end - len)) {
  40. val.copy(buffer, pos)
  41. pos += len
  42. }
  43. if (pos !== end) {
  44. val.copy(buffer, pos, 0, end - pos)
  45. }
  46. return buffer
  47. }
  48. function fill (buffer, val, start, end, encoding) {
  49. if (hasFullSupport) {
  50. return buffer.fill(val, start, end, encoding)
  51. }
  52. if (typeof val === 'number') {
  53. return fillWithNumber(buffer, val, start, end)
  54. }
  55. if (typeof val === 'string') {
  56. if (typeof start === 'string') {
  57. encoding = start
  58. start = 0
  59. end = buffer.length
  60. } else if (typeof end === 'string') {
  61. encoding = end
  62. end = buffer.length
  63. }
  64. if (encoding !== undefined && typeof encoding !== 'string') {
  65. throw new TypeError('encoding must be a string')
  66. }
  67. if (encoding === 'latin1') {
  68. encoding = 'binary'
  69. }
  70. if (typeof encoding === 'string' && !Buffer.isEncoding(encoding)) {
  71. throw new TypeError('Unknown encoding: ' + encoding)
  72. }
  73. if (val === '') {
  74. return fillWithNumber(buffer, 0, start, end)
  75. }
  76. if (isSingleByte(val)) {
  77. return fillWithNumber(buffer, val.charCodeAt(0), start, end)
  78. }
  79. val = new Buffer(val, encoding)
  80. }
  81. if (Buffer.isBuffer(val)) {
  82. return fillWithBuffer(buffer, val, start, end)
  83. }
  84. // Other values (e.g. undefined, boolean, object) results in zero-fill
  85. return fillWithNumber(buffer, 0, start, end)
  86. }
  87. module.exports = fill