@harmony-js/utils
Advanced tools
Comparing version 0.1.23 to 0.1.27
@@ -210,3 +210,3 @@ /** | ||
Units["wei"] = "wei"; | ||
Units["kwei"] = "kwei"; | ||
Units["Kwei"] = "Kwei"; | ||
Units["Mwei"] = "Mwei"; | ||
@@ -224,3 +224,3 @@ Units["Gwei"] = "Gwei"; | ||
["wei" /* wei */, '1'], | ||
["kwei" /* kwei */, '1000'], | ||
["Kwei" /* Kwei */, '1000'], | ||
["Mwei" /* Mwei */, '1000000'], | ||
@@ -345,3 +345,3 @@ ["Gwei" /* Gwei */, '1000000000'], | ||
if (fraction.length > baseNumDecimals) { | ||
throw new Error("Cannot convert " + inputStr + " to Qa."); | ||
throw new Error("Cannot convert " + inputStr + " to wei."); | ||
} | ||
@@ -404,2 +404,5 @@ while (fraction.length < baseNumDecimals) { | ||
} | ||
else if (str === '0x') { | ||
this.unit = hexToNumber('0x0'); | ||
} | ||
else { | ||
@@ -451,3 +454,3 @@ this.unit = str; | ||
Unit.prototype.asKwei = function () { | ||
this.wei = toWei(this.unit, "kwei" /* kwei */); | ||
this.wei = toWei(this.unit, "Kwei" /* Kwei */); | ||
return this; | ||
@@ -488,11 +491,16 @@ }; | ||
Unit.prototype.asTether = function () { | ||
this.wei = toWei(this.unit, "Gether" /* Gether */); | ||
this.wei = toWei(this.unit, "Tether" /* Tether */); | ||
return this; | ||
}; | ||
Unit.prototype.toWei = function () { | ||
return this.wei; | ||
if (this.wei) { | ||
return this.wei; | ||
} | ||
else { | ||
throw new Error('error transforming'); | ||
} | ||
}; | ||
Unit.prototype.toKwei = function () { | ||
if (this.wei) { | ||
return fromWei(this.wei, "kwei" /* kwei */); | ||
return fromWei(this.wei, "Kwei" /* Kwei */); | ||
} | ||
@@ -527,3 +535,3 @@ else { | ||
}; | ||
Unit.prototype.tofinney = function () { | ||
Unit.prototype.toFinney = function () { | ||
if (this.wei) { | ||
@@ -530,0 +538,0 @@ return fromWei(this.wei, "finney" /* finney */); |
@@ -206,3 +206,3 @@ /** | ||
Units["wei"] = "wei"; | ||
Units["kwei"] = "kwei"; | ||
Units["Kwei"] = "Kwei"; | ||
Units["Mwei"] = "Mwei"; | ||
@@ -220,3 +220,3 @@ Units["Gwei"] = "Gwei"; | ||
["wei" /* wei */, '1'], | ||
["kwei" /* kwei */, '1000'], | ||
["Kwei" /* Kwei */, '1000'], | ||
["Mwei" /* Mwei */, '1000000'], | ||
@@ -341,3 +341,3 @@ ["Gwei" /* Gwei */, '1000000000'], | ||
if (fraction.length > baseNumDecimals) { | ||
throw new Error("Cannot convert " + inputStr + " to Qa."); | ||
throw new Error("Cannot convert " + inputStr + " to wei."); | ||
} | ||
@@ -400,2 +400,5 @@ while (fraction.length < baseNumDecimals) { | ||
} | ||
else if (str === '0x') { | ||
this.unit = hexToNumber('0x0'); | ||
} | ||
else { | ||
@@ -447,3 +450,3 @@ this.unit = str; | ||
Unit.prototype.asKwei = function () { | ||
this.wei = toWei(this.unit, "kwei" /* kwei */); | ||
this.wei = toWei(this.unit, "Kwei" /* Kwei */); | ||
return this; | ||
@@ -484,11 +487,16 @@ }; | ||
Unit.prototype.asTether = function () { | ||
this.wei = toWei(this.unit, "Gether" /* Gether */); | ||
this.wei = toWei(this.unit, "Tether" /* Tether */); | ||
return this; | ||
}; | ||
Unit.prototype.toWei = function () { | ||
return this.wei; | ||
if (this.wei) { | ||
return this.wei; | ||
} | ||
else { | ||
throw new Error('error transforming'); | ||
} | ||
}; | ||
Unit.prototype.toKwei = function () { | ||
if (this.wei) { | ||
return fromWei(this.wei, "kwei" /* kwei */); | ||
return fromWei(this.wei, "Kwei" /* Kwei */); | ||
} | ||
@@ -523,3 +531,3 @@ else { | ||
}; | ||
Unit.prototype.tofinney = function () { | ||
Unit.prototype.toFinney = function () { | ||
if (this.wei) { | ||
@@ -526,0 +534,0 @@ return fromWei(this.wei, "finney" /* finney */); |
@@ -210,3 +210,3 @@ /** | ||
Units["wei"] = "wei"; | ||
Units["kwei"] = "kwei"; | ||
Units["Kwei"] = "Kwei"; | ||
Units["Mwei"] = "Mwei"; | ||
@@ -224,3 +224,3 @@ Units["Gwei"] = "Gwei"; | ||
["wei" /* wei */, '1'], | ||
["kwei" /* kwei */, '1000'], | ||
["Kwei" /* Kwei */, '1000'], | ||
["Mwei" /* Mwei */, '1000000'], | ||
@@ -345,3 +345,3 @@ ["Gwei" /* Gwei */, '1000000000'], | ||
if (fraction.length > baseNumDecimals) { | ||
throw new Error("Cannot convert " + inputStr + " to Qa."); | ||
throw new Error("Cannot convert " + inputStr + " to wei."); | ||
} | ||
@@ -404,2 +404,5 @@ while (fraction.length < baseNumDecimals) { | ||
} | ||
else if (str === '0x') { | ||
this.unit = hexToNumber('0x0'); | ||
} | ||
else { | ||
@@ -451,3 +454,3 @@ this.unit = str; | ||
Unit.prototype.asKwei = function () { | ||
this.wei = toWei(this.unit, "kwei" /* kwei */); | ||
this.wei = toWei(this.unit, "Kwei" /* Kwei */); | ||
return this; | ||
@@ -488,11 +491,16 @@ }; | ||
Unit.prototype.asTether = function () { | ||
this.wei = toWei(this.unit, "Gether" /* Gether */); | ||
this.wei = toWei(this.unit, "Tether" /* Tether */); | ||
return this; | ||
}; | ||
Unit.prototype.toWei = function () { | ||
return this.wei; | ||
if (this.wei) { | ||
return this.wei; | ||
} | ||
else { | ||
throw new Error('error transforming'); | ||
} | ||
}; | ||
Unit.prototype.toKwei = function () { | ||
if (this.wei) { | ||
return fromWei(this.wei, "kwei" /* kwei */); | ||
return fromWei(this.wei, "Kwei" /* Kwei */); | ||
} | ||
@@ -527,3 +535,3 @@ else { | ||
}; | ||
Unit.prototype.tofinney = function () { | ||
Unit.prototype.toFinney = function () { | ||
if (this.wei) { | ||
@@ -530,0 +538,0 @@ return fromWei(this.wei, "finney" /* finney */); |
import BN from 'bn.js'; | ||
export declare const enum Units { | ||
wei = "wei", | ||
kwei = "kwei", | ||
Kwei = "Kwei", | ||
Mwei = "Mwei", | ||
@@ -57,3 +57,3 @@ Gwei = "Gwei", | ||
toSzabo(): string; | ||
tofinney(): string; | ||
toFinney(): string; | ||
toEther(): string; | ||
@@ -60,0 +60,0 @@ toKether(): string; |
@@ -9,3 +9,3 @@ "use strict"; | ||
Units["wei"] = "wei"; | ||
Units["kwei"] = "kwei"; | ||
Units["Kwei"] = "Kwei"; | ||
Units["Mwei"] = "Mwei"; | ||
@@ -23,3 +23,3 @@ Units["Gwei"] = "Gwei"; | ||
["wei" /* wei */, '1'], | ||
["kwei" /* kwei */, '1000'], | ||
["Kwei" /* Kwei */, '1000'], | ||
["Mwei" /* Mwei */, '1000000'], | ||
@@ -144,3 +144,3 @@ ["Gwei" /* Gwei */, '1000000000'], | ||
if (fraction.length > baseNumDecimals) { | ||
throw new Error("Cannot convert " + inputStr + " to Qa."); | ||
throw new Error("Cannot convert " + inputStr + " to wei."); | ||
} | ||
@@ -203,2 +203,5 @@ while (fraction.length < baseNumDecimals) { | ||
} | ||
else if (str === '0x') { | ||
this.unit = exports.hexToNumber('0x0'); | ||
} | ||
else { | ||
@@ -250,3 +253,3 @@ this.unit = str; | ||
Unit.prototype.asKwei = function () { | ||
this.wei = exports.toWei(this.unit, "kwei" /* kwei */); | ||
this.wei = exports.toWei(this.unit, "Kwei" /* Kwei */); | ||
return this; | ||
@@ -287,11 +290,16 @@ }; | ||
Unit.prototype.asTether = function () { | ||
this.wei = exports.toWei(this.unit, "Gether" /* Gether */); | ||
this.wei = exports.toWei(this.unit, "Tether" /* Tether */); | ||
return this; | ||
}; | ||
Unit.prototype.toWei = function () { | ||
return this.wei; | ||
if (this.wei) { | ||
return this.wei; | ||
} | ||
else { | ||
throw new Error('error transforming'); | ||
} | ||
}; | ||
Unit.prototype.toKwei = function () { | ||
if (this.wei) { | ||
return exports.fromWei(this.wei, "kwei" /* kwei */); | ||
return exports.fromWei(this.wei, "Kwei" /* Kwei */); | ||
} | ||
@@ -326,3 +334,3 @@ else { | ||
}; | ||
Unit.prototype.tofinney = function () { | ||
Unit.prototype.toFinney = function () { | ||
if (this.wei) { | ||
@@ -329,0 +337,0 @@ return exports.fromWei(this.wei, "finney" /* finney */); |
{ | ||
"name": "@harmony-js/utils", | ||
"version": "0.1.23", | ||
"version": "0.1.27", | ||
"description": "utils for harmony", | ||
@@ -24,3 +24,3 @@ "main": "dist/index.js", | ||
}, | ||
"gitHead": "3dbcc4b173e690f08cc0c4e632aab65454babd50" | ||
"gitHead": "6e1abec93bbdb2ca5fd1acfc8d0093165688ef91" | ||
} |
import BN from 'bn.js'; | ||
import {isString, isNumber, isHex} from './validators'; | ||
import { isString, isNumber, isHex } from './validators'; | ||
export const enum Units { | ||
wei = 'wei', | ||
kwei = 'kwei', | ||
Kwei = 'Kwei', | ||
Mwei = 'Mwei', | ||
@@ -20,3 +20,3 @@ Gwei = 'Gwei', | ||
[Units.wei, '1'], | ||
[Units.kwei, '1000'], // 1e3 wei | ||
[Units.Kwei, '1000'], // 1e3 wei | ||
[Units.Mwei, '1000000'], // 1e6 wei | ||
@@ -37,6 +37,3 @@ [Units.Gwei, '1000000000'], // 1e9 wei | ||
export const numberToString = ( | ||
obj: BN | number | string, | ||
radix: number = 10, | ||
): string => { | ||
export const numberToString = (obj: BN | number | string, radix: number = 10): string => { | ||
if (BN.isBN(obj)) { | ||
@@ -153,3 +150,3 @@ return obj.toString(radix); | ||
if (fraction.length > baseNumDecimals) { | ||
throw new Error(`Cannot convert ${inputStr} to Qa.`); | ||
throw new Error(`Cannot convert ${inputStr} to wei.`); | ||
} | ||
@@ -175,7 +172,3 @@ | ||
export const fromWei = ( | ||
wei: BN | string, | ||
unit: Units, | ||
options: any = DEFAULT_OPTIONS, | ||
): string => { | ||
export const fromWei = (wei: BN | string, unit: Units, options: any = DEFAULT_OPTIONS): string => { | ||
try { | ||
@@ -268,2 +261,4 @@ const weiBN: BN = !BN.isBN(wei) ? new BN(wei) : wei; | ||
this.unit = str.toString(); | ||
} else if (str === '0x') { | ||
this.unit = hexToNumber('0x0'); | ||
} else { | ||
@@ -281,3 +276,3 @@ this.unit = str; | ||
asKwei() { | ||
this.wei = toWei(this.unit, Units.kwei); | ||
this.wei = toWei(this.unit, Units.Kwei); | ||
return this; | ||
@@ -318,3 +313,3 @@ } | ||
asTether() { | ||
this.wei = toWei(this.unit, Units.Gether); | ||
this.wei = toWei(this.unit, Units.Tether); | ||
return this; | ||
@@ -324,3 +319,7 @@ } | ||
toWei() { | ||
return this.wei; | ||
if (this.wei) { | ||
return this.wei; | ||
} else { | ||
throw new Error('error transforming'); | ||
} | ||
} | ||
@@ -330,3 +329,3 @@ | ||
if (this.wei) { | ||
return fromWei(this.wei, Units.kwei); | ||
return fromWei(this.wei, Units.Kwei); | ||
} else { | ||
@@ -357,3 +356,3 @@ throw new Error('error transforming'); | ||
} | ||
tofinney() { | ||
toFinney() { | ||
if (this.wei) { | ||
@@ -360,0 +359,0 @@ return fromWei(this.wei, Units.finney); |
@@ -19,3 +19,3 @@ import * as validators from '../src/validators'; | ||
describe('test transformer', () => { | ||
describe('test validator', () => { | ||
it('test isNumber', () => { | ||
@@ -102,2 +102,47 @@ const beTrue = ['zero', 'float', 'hexNumber']; | ||
}); | ||
it('test isHttp', () => { | ||
expect(validators.isHttp('http://www.google.com')).toEqual(true); | ||
expect(validators.isHttp('https://www.google.com')).toEqual(true); | ||
expect(validators.isHttp('ftp://www.google.com')).toEqual(false); | ||
try { | ||
validators.isHttp(123); | ||
} catch (error) { | ||
expect(error.message).toEqual(`123 is not valid url`); | ||
} | ||
}); | ||
it('test isWs', () => { | ||
expect(validators.isWs('ws://www.google.com')).toEqual(true); | ||
expect(validators.isWs('wss://www.google.com')).toEqual(true); | ||
expect(validators.isWs('ftp://www.google.com')).toEqual(false); | ||
try { | ||
validators.isWs(123); | ||
} catch (error) { | ||
expect(error.message).toEqual(`123 is not valid url`); | ||
} | ||
}); | ||
it('test isBech32Address', () => { | ||
expect(validators.isBech32Address('one1au4f9jectk52k24rnk9hjuygt22q2045wcpfdp')).toEqual(true); | ||
expect(validators.isBech32Address('xxx')).toEqual(false); | ||
}); | ||
it('test isBech32Address', () => { | ||
expect( | ||
validators.isBech32TestNetAddress('tone1au4f9jectk52k24rnk9hjuygt22q2045wcpfdp'), | ||
).toEqual(true); | ||
expect(validators.isBech32TestNetAddress('xxx')).toEqual(false); | ||
}); | ||
it('test isValidAddress', () => { | ||
expect(validators.isValidAddress('tone1au4f9jectk52k24rnk9hjuygt22q2045wcpfdp')).toEqual(true); | ||
expect(validators.isValidAddress('one1au4f9jectk52k24rnk9hjuygt22q2045wcpfdp')).toEqual(true); | ||
expect(validators.isValidAddress(advanceType.checkSumAddress)).toEqual(true); | ||
expect(validators.isValidAddress(advanceType.address)).toEqual(true); | ||
expect(validators.isValidAddress(advanceType.hexAddress)).toEqual(true); | ||
expect(validators.isValidAddress('888')).toEqual(false); | ||
try { | ||
validators.isValidAddress(<any | string>123); | ||
} catch (error) { | ||
expect(error.message).toEqual(`123 is not string`); | ||
} | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
331468
50
4727