-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 1189b8f
Showing
9 changed files
with
229 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": ["env"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.* | ||
test | ||
|
||
*.swp | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!'); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
|