Skip to content

Commit 237b124

Browse files
committed
init commit
0 parents  commit 237b124

File tree

9 files changed

+173
-0
lines changed

9 files changed

+173
-0
lines changed

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
root = true
2+
3+
[*]
4+
indent_style = tab
5+
end_of_line = lf
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
insert_final_newline = true
9+
10+
[{package.json,*.yml}]
11+
indent_style = space
12+
indent_size = 2
13+
14+
[*.md]
15+
trim_trailing_whitespace = false

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
.nyc_output
3+
coverage

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
language: node_js
2+
node_js:
3+
- 'stable'
4+
- '0.12'
5+
- '0.10'

index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
module.exports = function (buffer, limit) {
3+
limit = limit || 20;
4+
return buffer.toString('hex').split('')
5+
.reduce(function (arr, char) {
6+
if (arr.length && arr[arr.length - 1].length === 1) {
7+
arr[arr.length - 1] += char;
8+
if (arr.length && arr.length % limit === 0) {
9+
arr[arr.length - 1] += '\n';
10+
} else {
11+
arr[arr.length - 1] += ' ';
12+
}
13+
} else {
14+
arr.push(char);
15+
}
16+
return arr;
17+
}, []).join('').trim();
18+
};

license

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Isaac Z. Schlueter <i@izs.me>, James Talmage <james@talmage.io> (github.com/jamestalmage), and Contributors
4+
5+
Extracted from code in node-tap http://www.node-tap.org/
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.

package.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "buffer-to-string",
3+
"version": "0.0.0",
4+
"description": "A better Buffer.toString()",
5+
"license": "MIT",
6+
"repository": "tapjs/buffer-to-string",
7+
"author": {
8+
"name": "James Talmage",
9+
"email": "james@talmage.io",
10+
"url": "github.com/jamestalmage"
11+
},
12+
"engines": {
13+
"node": ">=0.10.0"
14+
},
15+
"scripts": {
16+
"test": "xo && nyc --cache --reporter lcov --reporter text ava"
17+
},
18+
"files": [
19+
"index.js"
20+
],
21+
"keywords": [
22+
"buffer",
23+
"string",
24+
"beautify",
25+
"pretty",
26+
"print"
27+
],
28+
"dependencies": {},
29+
"devDependencies": {
30+
"ava": "^0.10.0",
31+
"nyc": "^5.3.0",
32+
"xo": "^0.12.1"
33+
}
34+
}

readme.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# buffer-to-string [![Build Status](https://travis-ci.org/tapjs/buffer-to-string.svg?branch=master)](https://travis-ci.org/tapjs/buffer-to-string)
2+
3+
> A better Buffer.toString()
4+
5+
Displays buffer content in hex. Characters are grouped into two-character bytes, with a customizable line wrap width.
6+
7+
## Install
8+
9+
```
10+
$ npm install --save buffer-to-string
11+
```
12+
13+
14+
## Usage
15+
16+
```js
17+
const bufferToString = require('buffer-to-string');
18+
19+
bufferToString(new Buffer([0, 1, 2, 3, 4, 5, 6]), 3);
20+
/*
21+
00 01 02
22+
03 04 05
23+
06
24+
*/
25+
```
26+
27+
28+
## API
29+
30+
### bufferToString(buffer, [width])
31+
32+
#### input
33+
34+
Type: `Buffer`
35+
36+
The Buffer to stringify.
37+
38+
#### width
39+
40+
Type: `number`
41+
Default: `20`
42+
43+
The maximum width before wrapping a newline. It is measured in bytes displayed. The maximum character width can be determined by computing `(3 * width) - 1`.
44+
45+
## License
46+
47+
MIT © [Isaac Z. Schlueter](http://github.com/isaacs), [James Talmage](http://github.com/jamestalmage)

test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import test from 'ava';
2+
import fn from './';
3+
4+
test('defaults to width of 20', t => {
5+
const arr = [];
6+
for (var i = 0; i < 50; i++) {
7+
arr[i] = i;
8+
}
9+
t.is(fn(new Buffer(arr)), [
10+
'00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13',
11+
'14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27',
12+
'28 29 2a 2b 2c 2d 2e 2f 30 31'
13+
].join('\n'));
14+
});
15+
16+
test('width is customizable', t => {
17+
const arr = [];
18+
for (var i = 0; i < 10; i++) {
19+
arr[i] = i;
20+
}
21+
t.is(fn(new Buffer(arr), 3), [
22+
'00 01 02',
23+
'03 04 05',
24+
'06 07 08',
25+
'09'
26+
].join('\n'));
27+
});

0 commit comments

Comments
 (0)