Skip to content

Commit f7d8479

Browse files
committed
fix: hashrate parsing without H/s
1 parent ed5285c commit f7d8479

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mining-numbers",
3-
"version": "0.0.3",
3+
"version": "0.0.4",
44
"description": "Mining numbers converter",
55
"type": "module",
66
"main": "dist/index.js",

src/hashrate.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export type HashrateUnit = "H" | "KH" | "MH" | "GH" | "TH" | "PH" | "EH";
44
const UNITS: HashrateUnit[] = ["H", "KH", "MH", "GH", "TH", "PH", "EH"];
55

66
export class Hashrate extends Base {
7-
private static regex = /^(\d+(?:\.\d+)?)\s*([A-Za-z]*)\/s$/;
7+
private static regex = /^(\d+(?:\.\d+)?)\s*([A-Za-z]*)(?:\/s)?$/;
88

99
constructor(
1010
public value: number,
@@ -14,7 +14,13 @@ export class Hashrate extends Base {
1414
}
1515

1616
static parse(input: number | string): Hashrate {
17-
const base = Base.parse(input, Hashrate.regex, UNITS);
17+
let hashrateInput = input;
18+
if (typeof input === "string") {
19+
if (!input.endsWith("H/s")) {
20+
hashrateInput = `${input}H/s`;
21+
}
22+
}
23+
const base = Base.parse(hashrateInput, Hashrate.regex, UNITS);
1824
return new Hashrate(base.getValue, base.getUnit as HashrateUnit);
1925
}
2026

test/hashrate.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,16 @@ describe("parsing from string", () => {
187187
const hashrate = Hashrate.parse("1.5 KH/s");
188188
expect(hashrate.toNumber()).toBe(1500);
189189
});
190+
191+
it('should parse "52.7K" to 52700', () => {
192+
const hashrate = Hashrate.parse("52.7K");
193+
expect(hashrate.toNumber()).toBe(52700);
194+
});
195+
196+
it('should parse "426G" to 426000000000', () => {
197+
const hashrate = Hashrate.parse("426G");
198+
expect(hashrate.toNumber()).toBe(426000000000);
199+
});
190200
});
191201

192202
describe("error handling", () => {

0 commit comments

Comments
 (0)