@xyo-network/buffer-utils
Advanced tools
Comparing version 0.2.1 to 0.3.0
/// <reference types="node" /> | ||
/** Returns a pseudo-hash based off the contents of a buffer */ | ||
export declare function getBufferHash(buffer: Buffer): number; | ||
/** A helper function to write a number to a buffer that supports multiple sizes and signings */ | ||
export declare function writeNumberToBuffer(numberToWrite: number, bytes: number, isSigned: boolean, buffer?: Buffer, offset?: number): Buffer; | ||
export declare function writeIntegerToBuffer(numberToWrite: number, bytes: number, isSigned: boolean, buffer?: Buffer, offset?: number): Buffer; | ||
/** A helper function to read a number from a buffer based off of sign by */ | ||
export declare function readNumberFromBuffer(buffer: Buffer, bytes: number, isSigned: boolean, offset?: number): number; | ||
export declare function readIntegerFromBuffer(buffer: Buffer, bytes: number, isSigned: boolean, offset?: number): number; | ||
export declare function writePointTo32ByteBuffer(point: Buffer): Buffer; | ||
export declare function unsignedNumberToBuffer(num: number): Buffer; | ||
export declare function signedNumberToBuffer(num: number): Buffer; | ||
export declare function unsignedIntegerToBuffer(num: number): Buffer; | ||
export declare function signedIntegerToBuffer(num: number): Buffer; | ||
export declare function doubleToBuffer(num: number): Buffer; | ||
export declare function floatToBuffer(num: number): Buffer; | ||
export declare function readDoubleFromBuffer(buffer: Buffer, offset?: number): number; | ||
export declare function readFloatFromBuffer(buffer: Buffer, offset?: number): number; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -8,24 +8,14 @@ "use strict"; | ||
* @Last modified by: ryanxyo | ||
* @Last modified time: Wednesday, 28th November 2018 5:50:00 pm | ||
* @Last modified time: Tuesday, 11th December 2018 9:14:17 am | ||
* @License: All Rights Reserved | ||
* @Copyright: Copyright XY | The Findables Company | ||
*/ | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const errors_1 = require("@xyo-network/errors"); | ||
/** Returns a pseudo-hash based off the contents of a buffer */ | ||
function getBufferHash(buffer) { | ||
if (buffer.length === 0) { | ||
return 0; | ||
} | ||
let hash = buffer.length; | ||
buffer.forEach((byte, index) => { | ||
hash += index; | ||
hash += byte; | ||
return hash * 31 * (index + byte); | ||
}); | ||
return hash; | ||
} | ||
exports.getBufferHash = getBufferHash; | ||
const bn_js_1 = __importDefault(require("bn.js")); | ||
/** A helper function to write a number to a buffer that supports multiple sizes and signings */ | ||
function writeNumberToBuffer(numberToWrite, bytes, isSigned, buffer, offset) { | ||
function writeIntegerToBuffer(numberToWrite, bytes, isSigned, buffer, offset) { | ||
const buf = buffer || Buffer.alloc(bytes); | ||
@@ -58,2 +48,10 @@ const bufOffset = offset || 0; | ||
return buf; | ||
case 8: | ||
const bigNumberBuffer = new bn_js_1.default(numberToWrite).toBuffer('be', 8); | ||
let i = 0; | ||
while (i < 8) { | ||
buf[i + bufOffset] = bigNumberBuffer[i]; | ||
i += 1; | ||
} | ||
return buf; | ||
default: | ||
@@ -63,5 +61,5 @@ throw new errors_1.XyoError(`Could not write number to buffer`, errors_1.XyoErrors.CRITICAL); | ||
} | ||
exports.writeNumberToBuffer = writeNumberToBuffer; | ||
exports.writeIntegerToBuffer = writeIntegerToBuffer; | ||
/** A helper function to read a number from a buffer based off of sign by */ | ||
function readNumberFromBuffer(buffer, bytes, isSigned, offset) { | ||
function readIntegerFromBuffer(buffer, bytes, isSigned, offset) { | ||
const bufOffset = offset || 0; | ||
@@ -87,2 +85,4 @@ if (isSigned) { | ||
return buffer.readUInt32BE(bufOffset); | ||
case 8: | ||
return new bn_js_1.default(buffer.slice(bufOffset || 0, bufOffset + bytes)).toNumber(); | ||
default: | ||
@@ -92,3 +92,3 @@ throw new errors_1.XyoError(`Could not read number from buffer`, errors_1.XyoErrors.CRITICAL); | ||
} | ||
exports.readNumberFromBuffer = readNumberFromBuffer; | ||
exports.readIntegerFromBuffer = readIntegerFromBuffer; | ||
function writePointTo32ByteBuffer(point) { | ||
@@ -108,3 +108,3 @@ if (point.length === 32) { | ||
exports.writePointTo32ByteBuffer = writePointTo32ByteBuffer; | ||
function unsignedNumberToBuffer(num) { | ||
function unsignedIntegerToBuffer(num) { | ||
let buf; | ||
@@ -124,3 +124,3 @@ if (num <= Math.pow(2, 8) - 1) { | ||
else if (num > Math.pow(2, 32)) { | ||
throw new errors_1.XyoError('This is not yet supported', errors_1.XyoErrors.CRITICAL); | ||
buf = new bn_js_1.default(num).toBuffer('be'); | ||
} | ||
@@ -132,4 +132,4 @@ else { | ||
} | ||
exports.unsignedNumberToBuffer = unsignedNumberToBuffer; | ||
function signedNumberToBuffer(num) { | ||
exports.unsignedIntegerToBuffer = unsignedIntegerToBuffer; | ||
function signedIntegerToBuffer(num) { | ||
let buf; | ||
@@ -156,3 +156,23 @@ if (num <= Math.pow(2, 7) - 1) { | ||
} | ||
exports.signedNumberToBuffer = signedNumberToBuffer; | ||
exports.signedIntegerToBuffer = signedIntegerToBuffer; | ||
function doubleToBuffer(num) { | ||
const b = Buffer.alloc(8); | ||
b.writeDoubleBE(num, 0); | ||
return b; | ||
} | ||
exports.doubleToBuffer = doubleToBuffer; | ||
function floatToBuffer(num) { | ||
const b = Buffer.alloc(4); | ||
b.writeFloatBE(num, 0); | ||
return b; | ||
} | ||
exports.floatToBuffer = floatToBuffer; | ||
function readDoubleFromBuffer(buffer, offset = 0) { | ||
return buffer.readDoubleBE(offset); | ||
} | ||
exports.readDoubleFromBuffer = readDoubleFromBuffer; | ||
function readFloatFromBuffer(buffer, offset = 0) { | ||
return buffer.readFloatBE(offset); | ||
} | ||
exports.readFloatFromBuffer = readFloatFromBuffer; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@xyo-network/buffer-utils", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "Buffer Utilities", | ||
@@ -10,5 +10,7 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"@xyo-network/errors": "^0.2.1" | ||
"@xyo-network/errors": "^0.3.0", | ||
"bn.js": "^4.11.8" | ||
}, | ||
"devDependencies": { | ||
"@types/bn.js": "^4.11.2", | ||
"@types/jest": "^23.3.1", | ||
@@ -22,3 +24,3 @@ "@types/node": "^10.7.1", | ||
}, | ||
"gitHead": "0635d162b742fbb5ee4f09d4e3faf5149b43eaf0" | ||
"gitHead": "55319bda08512a2701fb6494ad2e2526791e6bb7" | ||
} |
@@ -7,3 +7,3 @@ /* | ||
* @Last modified by: ryanxyo | ||
* @Last modified time: Wednesday, 28th November 2018 5:50:00 pm | ||
* @Last modified time: Tuesday, 11th December 2018 9:14:17 am | ||
* @License: All Rights Reserved | ||
@@ -14,22 +14,6 @@ * @Copyright: Copyright XY | The Findables Company | ||
import { XyoError, XyoErrors } from "@xyo-network/errors" | ||
import BN from 'bn.js' | ||
/** Returns a pseudo-hash based off the contents of a buffer */ | ||
export function getBufferHash(buffer: Buffer): number { | ||
if (buffer.length === 0) { | ||
return 0 | ||
} | ||
let hash = buffer.length | ||
buffer.forEach((byte: number, index: number) => { | ||
hash += index | ||
hash += byte | ||
return hash * 31 * (index + byte) | ||
}) | ||
return hash | ||
} | ||
/** A helper function to write a number to a buffer that supports multiple sizes and signings */ | ||
export function writeNumberToBuffer( | ||
export function writeIntegerToBuffer( | ||
numberToWrite: number, | ||
@@ -70,2 +54,10 @@ bytes: number, | ||
return buf | ||
case 8: | ||
const bigNumberBuffer = new BN(numberToWrite).toBuffer('be', 8) | ||
let i = 0 | ||
while (i < 8) { | ||
buf[i + bufOffset] = bigNumberBuffer[i] | ||
i += 1 | ||
} | ||
return buf | ||
default: | ||
@@ -77,3 +69,3 @@ throw new XyoError(`Could not write number to buffer`, XyoErrors.CRITICAL) | ||
/** A helper function to read a number from a buffer based off of sign by */ | ||
export function readNumberFromBuffer(buffer: Buffer, bytes: number, isSigned: boolean, offset?: number) { | ||
export function readIntegerFromBuffer(buffer: Buffer, bytes: number, isSigned: boolean, offset?: number) { | ||
const bufOffset = offset || 0 | ||
@@ -101,2 +93,4 @@ | ||
return buffer.readUInt32BE(bufOffset) | ||
case 8: | ||
return new BN(buffer.slice(bufOffset || 0, bufOffset + bytes)).toNumber() | ||
default: | ||
@@ -123,3 +117,3 @@ throw new XyoError(`Could not read number from buffer`, XyoErrors.CRITICAL) | ||
export function unsignedNumberToBuffer(num: number): Buffer { | ||
export function unsignedIntegerToBuffer(num: number): Buffer { | ||
let buf: Buffer | ||
@@ -137,3 +131,3 @@ | ||
} else if (num > Math.pow(2, 32)) { | ||
throw new XyoError('This is not yet supported', XyoErrors.CRITICAL) | ||
buf = new BN(num).toBuffer('be') | ||
} else { | ||
@@ -146,3 +140,3 @@ throw new XyoError('This should never happen', XyoErrors.CRITICAL) | ||
export function signedNumberToBuffer(num: number): Buffer { | ||
export function signedIntegerToBuffer(num: number): Buffer { | ||
let buf: Buffer | ||
@@ -167,1 +161,21 @@ | ||
} | ||
export function doubleToBuffer(num: number): Buffer { | ||
const b = Buffer.alloc(8) | ||
b.writeDoubleBE(num, 0) | ||
return b | ||
} | ||
export function floatToBuffer(num: number): Buffer { | ||
const b = Buffer.alloc(4) | ||
b.writeFloatBE(num, 0) | ||
return b | ||
} | ||
export function readDoubleFromBuffer(buffer: Buffer, offset: number = 0): number { | ||
return buffer.readDoubleBE(offset) | ||
} | ||
export function readFloatFromBuffer(buffer: Buffer, offset: number = 0): number { | ||
return buffer.readFloatBE(offset) | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18134
358
2
8
+ Addedbn.js@^4.11.8
+ Added@xyo-network/base@0.3.1(transitive)
+ Added@xyo-network/errors@0.3.1(transitive)
+ Addedbn.js@4.12.1(transitive)
- Removed@xyo-network/base@0.2.1(transitive)
- Removed@xyo-network/errors@0.2.1(transitive)
Updated@xyo-network/errors@^0.3.0