Skip to content

Commit 4b19067

Browse files
committed
🚀 Initial commit
0 parents  commit 4b19067

File tree

5 files changed

+116
-0
lines changed

5 files changed

+116
-0
lines changed

‎.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/node_modules/
2+
/npm-debug.log

‎index.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function alloc (size, fill, encoding) {
2+
if (typeof size !== 'number') {
3+
throw new TypeError('"size" argument must be a number')
4+
}
5+
6+
if (size < 0) {
7+
throw new RangeError('"size" argument must not be negative')
8+
}
9+
10+
var buffer = new Buffer(size)
11+
12+
if (size === 0) {
13+
return buffer
14+
}
15+
16+
if (fill === undefined) {
17+
buffer.fill(0)
18+
return buffer
19+
}
20+
21+
if (typeof encoding !== 'string') {
22+
encoding = undefined
23+
}
24+
25+
buffer.fill(fill, encoding)
26+
return buffer
27+
}
28+
29+
module.exports = (Buffer.alloc || alloc)

‎package.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "buffer-alloc",
3+
"version": "0.0.0",
4+
"license": "MIT",
5+
"repository": "LinusU/buffer-alloc",
6+
"scripts": {
7+
"test": "standard && node test"
8+
},
9+
"devDependencies": {
10+
"standard": "^7.1.2"
11+
},
12+
"engineStrict": true,
13+
"engines": {
14+
"node": ">= 0.12"
15+
},
16+
"keywords": [
17+
"alloc",
18+
"allocate",
19+
"buffer alloc",
20+
"buffer allocate",
21+
"buffer"
22+
]
23+
}

‎readme.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Buffer Alloc
2+
3+
A ponyfill for `Buffer.alloc`, uses native implementation if available.
4+
5+
## Installation
6+
7+
```sh
8+
npm install --save buffer-alloc
9+
```
10+
11+
## Usage
12+
13+
```js
14+
const alloc = require('buffer-alloc')
15+
16+
console.log(alloc(4))
17+
//=> <Buffer 00 00 00 00>
18+
19+
console.log(alloc(6, 0x41))
20+
//=> <Buffer 41 41 41 41 41 41>
21+
22+
console.log(alloc(10, 'linus', 'utf8'))
23+
//=> <Buffer 6c 69 6e 75 73 6c 69 6e 75 73>
24+
```
25+
26+
## API
27+
28+
### alloc(size[, fill[, encoding]])
29+
30+
- `size` &lt;Integer&gt; The desired length of the new `Buffer`
31+
- `fill` &lt;String&gt; | &lt;Buffer&gt; | &lt;Integer&gt; A value to pre-fill the new `Buffer` with. **Default:** `0`
32+
- `encoding` &lt;String&gt; If `fill` is a string, this is its encoding. **Default:** `'utf8'`
33+
34+
Allocates a new `Buffer` of `size` bytes. If `fill` is `undefined`, the `Buffer` will be zero-filled.
35+
36+
## See also
37+
38+
- [buffer-from](https://github.com/LinusU/buffer-from) A ponyfill for `Buffer.from`
39+
- [buffer-alloc-unsafe](https://github.com/LinusU/buffer-alloc-unsafe) A ponyfill for `Buffer.allocUnsafe`

‎test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var alloc = require('./')
2+
var assert = require('assert')
3+
4+
var b1 = alloc(4)
5+
assert.ok(Buffer.isBuffer(b1))
6+
assert.equal(b1.length, 4)
7+
assert.equal(b1.toString('hex'), '00000000')
8+
9+
var b2 = alloc(6, 0x41)
10+
assert.ok(Buffer.isBuffer(b2))
11+
assert.equal(b2.length, 6)
12+
assert.equal(b2.toString('hex'), '414141414141')
13+
14+
var b3 = alloc(10, 'linus', 'utf8')
15+
assert.ok(Buffer.isBuffer(b3))
16+
assert.equal(b3.length, 10)
17+
assert.equal(b3.toString('hex'), '6c696e75736c696e7573')
18+
19+
var b4 = alloc(8192)
20+
assert.ok(Buffer.isBuffer(b4))
21+
for (var i = 0; i < 8192; i++) {
22+
assert.equal(b4[i], 0)
23+
}

0 commit comments

Comments
 (0)