Skip to content

Commit

Permalink
feature(read-uint) add
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Jul 31, 2018
0 parents commit 1189b8f
Show file tree
Hide file tree
Showing 9 changed files with 229 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["env"]
}
16 changes: 16 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"env": {
"node": true
},
"globals": {
"BigInt": true
},
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module"
},
"rules": {
"indent": ["error", 4]
},
"extends": ["eslint:recommended"]
}
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package-lock.json
node_modules
npm-debug.log*
legacy

.nyc_output
*.swp

5 changes: 5 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.*
test

*.swp

15 changes: 15 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
language: node_js
node_js:
- 10

script:
- npm run lint
- npm run coverage && npm run report

notifications:
email:
on_success: never
on_failure: change

sudo: false

59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Read UInt [![License][LicenseIMGURL]][LicenseURL] [![NPM version][NPMIMGURL]][NPMURL] [![Dependency Status][DependencyStatusIMGURL]][DependencyStatusURL] [![Build Status][BuildStatusIMGURL]][BuildStatusURL]

Read an unsigned integer from `buffer`. Similar to [buf.readIntBE](https://nodejs.org/dist/latest-v10.x/docs/api/buffer.html#buffer_buf_readintbe_offset_bytelength) but without `byteLength` limitation.

## Install

```
npm i read-uint
```

## How to use?

```js
const readUint = require('read-uint');
const buf = Buffer.from([0xff, 0xfe, 0xff, 0xfd, 0xfb, 0xfa, 0xf0, 0xf1]);

readUIntBE(buf, 0);
// returns
'fffefffdfbfaf0f1';

readUIntLE(buf, 0);
// returns
'f1f0fafbfdfffe';

// set byte length
readUIntBE(buf, 0, 4);
// returns
'fffefffd';

readUIntLE(buf, 0, 4);
// returns
'f1f0fafb';

// convert to BigInt
const a = readUIntBE(buf, 0);
const number = BigInt(parseInt(a, 16));
```

## Environments

In old `node.js` environments that not fully supports `es2015`, `read-uint` could be used with:

```js
var readUInt = require('readUInt/legacy');
```

## License

MIT

[NPMIMGURL]: https://img.shields.io/npm/v/read-uint.svg?style=flat&longCache=true
[BuildStatusIMGURL]: https://img.shields.io/travis/coderaiser/read-uint/master.svg?style=flat&longCache=true
[DependencyStatusIMGURL]: https://img.shields.io/david/coderaiser/read-uint.svg?style=flat&longCache=true
[LicenseIMGURL]: https://img.shields.io/badge/license-MIT-317BF9.svg?style=flat&longCache=true
[NPMURL]: https://npmjs.org/package/read-uint "npm"
[BuildStatusURL]: https://travis-ci.org/coderaiser/read-uint "Build Status"
[DependencyStatusURL]: https://david-dm.org/coderaiser/read-uint "Dependency Status"
[LicenseURL]: https://tldrlegal.com/license/mit-license "MIT License"

35 changes: 35 additions & 0 deletions lib/read-uint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

module.exports.readUIntBE = (buf, offset, length = 8) => {
check(buf);

const data = [];
let i = -1;

while (++i < length) {
const current = buf.readUInt8(offset + i).toString(16);
data.push(current);
}

return data.join('');
};

module.exports.readUIntLE = (buf, offset, length = 8) => {
check(buf);

const data = [];
let i = length;

while (--i > -1) {
const current = buf.readUInt8(offset + i).toString(16);
data.push(current);
}

return data.join('');
};

function check(buf) {
if (!(buf instanceof Buffer))
throw Error('buf should be a buffer!');
}

50 changes: 50 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"name": "read-uint",
"version": "1.0.0",
"description": "read an unsigned integer from buffer",
"main": "lib/read-uint.js",
"scripts": {
"test": "tape test/*.js",
"coverage": "nyc npm test",
"lint": "eslint lib test",
"report": "nyc report --reporter=text-lcov | coveralls",
"build": "redrun clean 6to5 legacy:*",
"6to5": "babel -d legacy/lib lib",
"wisdom": "npm run build",
"clean": "rimraf legacy/*",
"watcher": "nodemon -w test -w lib --exec",
"watch:test": "npm run watcher -- npm test",
"watch:lint": "npm run watcher -- 'npm run lint'",
"watch:tape": "nodemon -w test -w lib --exec tape",
"watch:coverage:base": "npm run watcher -- nyc npm test",
"watch:coverage:tape": "npm run watcher -- nyc tape",
"watch:coverage": "bin/redrun.js watch:coverage:base",
"legacy:index": "echo \"module.exports = require('./lib/read-uint');\" > legacy/index.js"
},
"repository": {
"type": "git",
"url": "git://github.com/coderaiser/read-uint.git"
},
"keywords": [
"read",
"uint",
"buffer"
],
"author": "coderaiser <mnemonic.enemy@gmail.com> (http://coderaiser.github.io/)",
"license": "MIT",
"bugs": {
"url": "https://github.com/coderaiser/read-uint/issues"
},
"homepage": "https://github.com/coderaiser/read-uint",
"devDependencies": {
"babel-cli": "^6.1.1",
"babel-preset-env": "^1.6.1",
"coveralls": "^3.0.0",
"eslint": "^5.0.0",
"nodemon": "^1.11.0",
"nyc": "^12.0.0",
"redrun": "^6.0.0",
"rimraf": "^2.4.3",
"tape": "^4.2.0"
}
}
38 changes: 38 additions & 0 deletions test/read-uint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';

const test = require('tape');
const {
readUIntBE,
readUIntLE,
} = require('..');

test('readUIntBE: no args', (t) => {
t.throws(readUIntBE, /buf should be a buffer!/, 'should throw when no args');
t.end();
});

test('readUIntLE: no args', (t) => {
t.throws(readUIntLE, /buf should be a buffer!/, 'should throw when no args');
t.end();
});

test('readUIntBE', (t) => {
const buf = Buffer.from([0xff, 0xfe, 0xff, 0xfd, 0xfb, 0xfa, 0xf0, 0xf1]);

const result = readUIntBE(buf, 0);
const expected = 'fffefffdfbfaf0f1';

t.deepEqual(result, expected, 'shoudl equal');
t.end();
});

test('readUIntLE', (t) => {
const buf = Buffer.from([0xff, 0xfe, 0xff, 0xfd, 0xfb, 0xfa, 0xf0, 0xf1]);

const result = readUIntLE(buf, 0);
const expected = 'f1f0fafbfdfffeff';

t.deepEqual(result, expected, 'shoudl equal');
t.end();
});

0 comments on commit 1189b8f

Please sign in to comment.