Comparing version
{ | ||
"name": "bigbit", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Implementation of BigBit standard", | ||
@@ -8,3 +8,5 @@ "main": "index.js", | ||
"test": "jasmine tests/*test.js", | ||
"coverage": "nyc jasmine tests/*test.js; nyc report --reporter=lcov" | ||
"coverage": "nyc jasmine tests/*test.js; nyc report --reporter=lcov", | ||
"bundle": "browserify index.js -s bigbit > dist/bigbit.js;", | ||
"webbundle": "webpack" | ||
}, | ||
@@ -17,4 +19,9 @@ "keywords": [ | ||
"linked bytes", | ||
"number", | ||
"big" | ||
"big number", | ||
"big", | ||
"binary32", | ||
"binary64", | ||
"utf8", | ||
"character encoding", | ||
"ieee754" | ||
], | ||
@@ -25,8 +32,16 @@ "author": "Amit Gupta (http://amitkumargupta.work)", | ||
"type": "git", | ||
"url": "https://github.com/nimndata/BigBit" | ||
"url": "https://github.com/bigbit/bigbitjs" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "~7.0.0-rc.2", | ||
"@babel/plugin-transform-runtime": "~7.0.0-rc.2", | ||
"@babel/preset-env": "~7.0.0-rc.2", | ||
"@babel/register": "~7.0.0-rc.2", | ||
"babel-loader": "~8.0.0-beta.6", | ||
"webpack": "~4.17.0", | ||
"webpack-cli": "^3.1.0", | ||
"big.js": "^5.1.2", | ||
"ieee754": "^1.1.12", | ||
"jasmine": "^3.2.0", | ||
"browserify": "^16.2.3", | ||
"nyc": "^13.1.0" | ||
@@ -33,0 +48,0 @@ }, |
# BigBit | ||
An improved data type to save large numbers in less space without precision loss. check more detail [here](https://github.com/amitguptagwl/bigbit). | ||
An improved data type to save large numbers in less space without precision loss. check more detail [here](https://github.com/amitguptagwl/bigbit). Or try the [demo](https://bigbit.github.io/bigbitjs/) | ||
@@ -8,3 +8,3 @@ ```JavaScript | ||
let hb = new HBSequence('167770021700.00'); | ||
expect( hb.toDecimalString() ).toEqual('167770021700'); | ||
expect( hb.toString() ).toEqual('167770021700'); | ||
expect( hb.toByteArray() ).toEqual([69, 2, 121, 172, 255, 99]); | ||
@@ -18,3 +18,3 @@ expect( hb.exponent ).toEqual( 2 ); | ||
let ehb = new EHBSequence('167770021700.00'); | ||
expect( ehb.toDecimalString() ).toEqual('167770021700'); | ||
expect( ehb.toString() ).toEqual('167770021700'); | ||
expect( ehb.toByteArray() ).toEqual([69, 2, 121, 172, 255, 99]); | ||
@@ -21,0 +21,0 @@ expect( ehb.exponent ).toEqual( 2 ); |
@@ -1,46 +0,14 @@ | ||
/** | ||
* reset the counter once reach to max (inclusive) | ||
* @param {number} max | ||
* @param {number} min | ||
* @returns 0 if increments. 1 if resets | ||
*/ | ||
function cyclicCounter(val, max, min){ | ||
this.max = max; | ||
this.min = min || 0; | ||
this.value = val || min; | ||
this.add = function(num){ | ||
if(num > this.max || num < 0 ){ | ||
throw Error("Number should not be out of the range"); | ||
} | ||
if(this.value + num <= this.max){ | ||
this.value += num; | ||
return true; | ||
}else{ | ||
this.value = this.min + ( this.value + num - this.max); | ||
return false; | ||
} | ||
module.exports = function(x, base){ | ||
if(x > base || x < 0 ){ | ||
throw Error("Number should not be out of the range"); | ||
} | ||
this.up = function(){ | ||
if(this.value < this.max){ | ||
this.value++; | ||
return true; | ||
}else{ | ||
this.value = this.min; | ||
return false; | ||
return { | ||
by : function(y){ | ||
if(x + y <= base){ | ||
return x + y; | ||
}else{ | ||
return x + y - base; | ||
} | ||
} | ||
} | ||
this.down = function(){ | ||
if(this.value > this.min){ | ||
this.value--; | ||
return true; | ||
}else{ | ||
this.value = this.max; | ||
return false; | ||
} | ||
} | ||
} | ||
module.exports = cyclicCounter; | ||
}; |
@@ -0,1 +1,2 @@ | ||
const increase = require("../common/CyclicCounter"); | ||
const LBSequence = require("../LB/LBSequencePremitive"); | ||
@@ -158,3 +159,3 @@ const contants = { | ||
this._levelUp = function(level, quotient, remainder){ | ||
this.coffecient[level] = increase(this.coffecient[level] | 0).by(remainder); | ||
this.coffecient[level] = increase(this.coffecient[level] | 0, base).by(remainder); | ||
if( quotient.isGreaterThan(base - 1) ){//still divisible | ||
@@ -169,3 +170,3 @@ remainder = quotient.modulo( base ).toNumber(); | ||
this.coffecient[ level + 1 ] = increase(this.coffecient[ level + 1 ]).by( quotient.toNumber() ); | ||
this.coffecient[ level + 1 ] = increase(this.coffecient[ level + 1 ], base).by( quotient.toNumber() ); | ||
} | ||
@@ -211,6 +212,11 @@ } | ||
this.toDecimalString = function(){ | ||
return ByteBit.toBigNumber( this.toByteArray() ).toString(); | ||
this.toExponentString = function(range){ | ||
return ByteBit.toBigNumber( this.toByteArray() ).toExponential(range); | ||
} | ||
this.toString = function(){ | ||
return ByteBit.toBigNumber( this.toByteArray() ).toFixed(); | ||
} | ||
} | ||
@@ -271,3 +277,3 @@ | ||
//const coffecientsArrLength = headByte; | ||
if( !byteSequence[ index + (headByte -1) ] ) throw new Error("Invalid HB Bytes array. All coffecient bytes are not present."); | ||
if( byteSequence[ index + (headByte -1) ] === undefined) throw new Error("Invalid EHB Bytes array. All coffecient bytes are not present."); | ||
@@ -296,17 +302,2 @@ var coffecientIndex = (exponent.len ? index + exponent.len : index) + 1; | ||
const increase = function(x){ | ||
if(x > base || x < 0 ){ | ||
throw Error("Number should not be out of the range"); | ||
} | ||
return { | ||
by : function(y){ | ||
if(x + y <= base){ | ||
return x + y; | ||
}else{ | ||
return x + y - base; | ||
} | ||
} | ||
} | ||
} | ||
//TODO: | ||
@@ -313,0 +304,0 @@ ByteBit.prototype.from = function(byteArr, from){ |
@@ -0,1 +1,2 @@ | ||
const increase = require("../common/CyclicCounter"); | ||
const BigNumber = require("bignumber.js"); | ||
@@ -145,3 +146,3 @@ | ||
this.coffecient[level] = increase(this.coffecient[level] | 0).by(remainder); | ||
this.coffecient[level] = increase(this.coffecient[level] | 0, base).by(remainder); | ||
if( quotient.isGreaterThan(base - 1) ){//still divisible | ||
@@ -156,3 +157,3 @@ remainder = quotient.modulo( base ).toNumber(); | ||
this.coffecient[ level + 1 ] = increase(this.coffecient[ level + 1 ]).by( quotient.toNumber() ); | ||
this.coffecient[ level + 1 ] = increase(this.coffecient[ level + 1 ], base).by( quotient.toNumber() ); | ||
} | ||
@@ -189,5 +190,9 @@ } | ||
this.toDecimalString = function(){ | ||
return ByteBit.toBigNumber( this.toByteArray() ).toString(); | ||
this.toExponentString = function(range){ | ||
return ByteBit.toBigNumber( this.toByteArray() ).toExponential(range); | ||
} | ||
this.toString = function(){ | ||
return ByteBit.toBigNumber( this.toByteArray() ).toFixed(); | ||
} | ||
@@ -210,3 +215,3 @@ | ||
let bytesCount = headByte & 63; | ||
if( bytesCount && !headByteArray[ index + bytesCount ] ) throw new Error("Invalid HB Bytes sequence. All coffecient bytes are not present."); | ||
if( bytesCount && headByteArray[ index + bytesCount ] === undefined ) throw new Error("Invalid HB Bytes sequence. All coffecient bytes are not present."); | ||
@@ -232,4 +237,4 @@ //read for special values | ||
if( (headByte & 64) === 64){//exponent byte is present | ||
if( headByteArray[ index+1 ] === undefined) throw new Error("Invalid HB Bytes array. exponent byte was expected."); | ||
if(headByteArray[ index+1 ] & 128 === 128){//negative | ||
//if( headByteArray[ index+1 ] === undefined) throw new Error("Invalid HB Bytes array. exponent byte was expected."); | ||
if( (headByteArray[ index+1 ] & 128) === 128){//negative | ||
exponentValue = -(headByteArray[ index+1 ] ^ 128); | ||
@@ -269,20 +274,2 @@ }else{//positive | ||
const increase = function(x){ | ||
if(x > base || x < 0 ){ | ||
throw Error("Number should not be out of the range"); | ||
} | ||
return { | ||
by : function(y){ | ||
if(x + y <= base){ | ||
return x + y; | ||
}else{ | ||
return x + y - base; | ||
} | ||
} | ||
} | ||
} | ||
//TODO: | ||
@@ -289,0 +276,0 @@ ByteBit.prototype.from = function(byteArr, from){ |
@@ -0,1 +1,3 @@ | ||
const increase = require("../common/CyclicCounter"); | ||
const BigNumber = require("bignumber.js"); | ||
@@ -61,3 +63,3 @@ const powerOf128 = [ //exponentTableOf128 | ||
if( quotient.isGreaterThan(base - 1) ){//still divisible | ||
sequence[level] = increase(sequence[level]).by(remainder) | 128; | ||
sequence[level] = increase(sequence[level] | 0, base).by(remainder) | 128; | ||
remainder = quotient.modulo( base ).toNumber(); | ||
@@ -70,7 +72,7 @@ | ||
}else{ | ||
sequence[level] = increase(sequence[level] | 0).by(remainder) | 128; | ||
sequence[level] = increase(sequence[level] | 0, base).by(remainder) | 128; | ||
if( !sequence[ level +1 ] ) { | ||
sequence.push( 0 ); | ||
} | ||
sequence[ level + 1 ] = increase(sequence[ level + 1 ]).by( quotient.toNumber() ); | ||
sequence[ level + 1 ] = increase(sequence[ level + 1 ] | 0, base).by( quotient.toNumber() ); | ||
} | ||
@@ -115,3 +117,3 @@ } | ||
return { | ||
val: decimalValue.toString(), | ||
val: decimalValue.toFixed(), | ||
len: i + 1 | ||
@@ -122,18 +124,32 @@ } | ||
const increase = function(x){ | ||
if(x > base || x < 0 ){ | ||
throw Error("Number should not be out of the range"); | ||
LBSequence.strToByteArr = function(str){ | ||
const byteArr = []; | ||
for(let i=0; i< str.length; i++){ | ||
let code = str.charCodeAt(i); | ||
byteArr.push(...LBSequence.encode(code)); | ||
} | ||
return { | ||
by : function(y){ | ||
if(x + y <= base){ | ||
return x + y; | ||
}else{ | ||
return x + y - base; | ||
} | ||
} | ||
return byteArr; | ||
} | ||
LBSequence.byteArrToStr = function(byteArr, start, end){ | ||
start || (start = 0); | ||
end || (end = byteArr.length); | ||
let str = ''; | ||
while(start< end){ | ||
let code = LBSequence.decode( byteArr, start ); | ||
str += String.fromCharCode( code.val ); | ||
start += code.len; | ||
} | ||
return str; | ||
} | ||
LBSequence.convert = function(){ | ||
if( typeof arguments[0] === 'string'){ | ||
return LBSequence.strToByteArr(arguments[0]); | ||
}else{ | ||
return LBSequence.byteArrToStr( ...arguments); | ||
} | ||
} | ||
module.exports = LBSequence; |
@@ -0,1 +1,2 @@ | ||
const increase = require("../common/CyclicCounter"); | ||
const powerOf128 = [ //exponentTableOf128 | ||
@@ -58,3 +59,3 @@ Math.pow(128, 0), | ||
if( quotient> base - 1 ){//still divisible | ||
sequence[level] = increase(sequence[level]).by(remainder) | 128; | ||
sequence[level] = increase(sequence[level] | 0, base).by(remainder) | 128; | ||
remainder = quotient % base; | ||
@@ -67,7 +68,7 @@ | ||
}else{ | ||
sequence[level] = increase(sequence[level] | 0).by(remainder) | 128; | ||
sequence[level] = increase(sequence[level] | 0, base).by(remainder) | 128; | ||
if( !sequence[ level +1 ] ) { | ||
sequence.push( 0 ); | ||
} | ||
sequence[ level + 1 ] = increase(sequence[ level + 1 ]).by( quotient ); | ||
sequence[ level + 1 ] = increase(sequence[ level + 1 ] | 0, base).by( quotient ); | ||
} | ||
@@ -118,18 +119,31 @@ } | ||
const increase = function(x){ | ||
if(x > base || x < 0 ){ | ||
throw Error("Number should not be out of the range"); | ||
LBSequence.strToByteArr = function(str){ | ||
const byteArr = []; | ||
for(let i=0; i< str.length; i++){ | ||
let code = str.charCodeAt(i); | ||
byteArr.push(...LBSequence.encode(code)); | ||
} | ||
return { | ||
by : function(y){ | ||
if(x + y <= base){ | ||
return x + y; | ||
}else{ | ||
return x + y - base; | ||
} | ||
} | ||
return byteArr; | ||
} | ||
LBSequence.byteArrToStr = function(byteArr, start, end){ | ||
start || (start = 0); | ||
end || (end = byteArr.length); | ||
let str = ''; | ||
while(start< end){ | ||
let code = LBSequence.decode( byteArr, start ); | ||
str += String.fromCharCode( code.val ); | ||
start += code.len; | ||
} | ||
return str; | ||
} | ||
LBSequence.convert = function(){ | ||
if( typeof arguments[0] === 'string'){ | ||
return LBSequence.strToByteArr(arguments[0]); | ||
}else{ | ||
return LBSequence.byteArrToStr( ...arguments); | ||
} | ||
} | ||
module.exports = LBSequence; |
Sorry, the diff of this file is not supported yet
34827
12.19%786
0.26%12
200%