-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor runtime util into separate file, reader/writer uses runtime …
…util, fix packable float/double see #513
- Loading branch information
Showing
11 changed files
with
203 additions
and
253 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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
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,72 @@ | ||
"use strict"; | ||
|
||
var util = exports; | ||
|
||
var LongBits = util.LongBits = require("./longbits"); | ||
|
||
/** | ||
* Whether running within node or not. | ||
* @memberof util | ||
* @type {boolean} | ||
*/ | ||
var isNode = util.isNode = Boolean(global.process && global.process.versions && global.process.versions.node); | ||
|
||
/** | ||
* Optional buffer class to use. | ||
* If you assign any compatible buffer implementation to this property, the library will use it. | ||
* @type {?Function} | ||
*/ | ||
util.Buffer = null; | ||
|
||
if (isNode) | ||
try { util.Buffer = require("buffer").Buffer; } catch (e) {} // eslint-disable-line no-empty | ||
|
||
/** | ||
* Optional Long class to use. | ||
* If you assign any compatible long implementation to this property, the library will use it. | ||
* @type {?Function} | ||
*/ | ||
util.Long = global.dcodeIO && global.dcodeIO.Long || null; | ||
|
||
if (!util.Long && isNode) | ||
try { util.Long = require("long"); } catch (e) {} // eslint-disable-line no-empty | ||
|
||
/** | ||
* Converts a number or long to an 8 characters long hash string. | ||
* @param {Long|number} value Value to convert | ||
* @returns {string} Hash | ||
*/ | ||
util.longToHash = function longToHash(value) { | ||
return value | ||
? LongBits.from(value).toHash() | ||
: '\0\0\0\0\0\0\0\0'; | ||
}; | ||
|
||
/** | ||
* Converts an 8 characters long hash string to a long or number. | ||
* @param {string} hash Hash | ||
* @param {boolean} [unsigned=false] Whether unsigned or not | ||
* @returns {Long|number} Original value | ||
*/ | ||
util.longFromHash = function longFromHash(hash, unsigned) { | ||
var bits = LongBits.fromHash(hash); | ||
if (util.Long) | ||
return util.Long.fromBits(bits.lo, bits.hi, unsigned); | ||
return bits.toNumber(Boolean(unsigned)); | ||
}; | ||
|
||
/** | ||
* Tests if two possibly long values are not equal. | ||
* @param {number|Long} a First value | ||
* @param {number|Long} b Second value | ||
* @returns {boolean} `true` if not equal | ||
*/ | ||
util.longNeq = function longNeq(a, b) { | ||
return typeof a === 'number' | ||
? typeof b === 'number' | ||
? a !== b | ||
: (a = LongBits.fromNumber(a)).lo !== b.low || a.hi !== b.high | ||
: typeof b === 'number' | ||
? (b = LongBits.fromNumber(b)).lo !== a.low || b.hi !== a.high | ||
: a.low !== b.low || a.high !== b.high; | ||
}; |
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