Skip to content

Commit

Permalink
Merge pull request #512 from DoctorLai/patch-1
Browse files Browse the repository at this point in the history
Add more digits to steem.formatter.reputation and Add some tests
  • Loading branch information
ety001 authored Sep 26, 2024
2 parents 13523bc + efed2cf commit 78ba61e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,19 @@ module.exports = steemAPI => {
}

return {
reputation: function(reputation) {
reputation: function(reputation, decimal_places = 0) {
if (reputation == 0) return 25;
if (!reputation) return reputation;
let neg = reputation < 0;
let rep = String(reputation);
rep = neg ? rep.substring(1) : rep;
let v = (Math.log10((rep > 0 ? rep : -rep) - 10) - 9);
v = neg ? -v : v;
return parseInt(v * 9 + 25);
v = v * 9 + 25;
if (decimal_places > 0) {
return +(Math.round(v + "e+" + decimal_places) + "e-" + decimal_places);
}
return parseInt(v);
},

vestToSteem: function(
Expand Down
34 changes: 33 additions & 1 deletion test/reputation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,37 @@ describe('steem.format.reputation', ()=> {
});
it('rep -22233344455 => 12', () => {
assert.equal(reputation(-22233344455), 12);
});
});

// with decimal places
it('rep 0 => 25', () => {
assert.equal(reputation(0, 1), 25);
});
it('rep 95832978796820 => 69.83', () => {
assert.equal(reputation(95832978796820, 2), 69.83);
});
it('rep 10004392664120 => 61.002', () => {
assert.equal(reputation(10004392664120, 3), 61.002);
});
it('rep 30999525306309 => 65.4222', () => {
assert.equal(reputation(30999525306309, 4), 65.4222);
});
it('rep -37765258368568 => -16.19383', () => {
assert.equal(reputation(-37765258368568, 5), -16.19383);
});
it('rep 334486135407077 => 74.719403', () => {
assert.equal(reputation(334486135407077, 6), 74.719403);
});
it('rep null => null', () => {
assert.equal(reputation(null, 7), null);
});
it('rep undefined => undefined', () => {
assert.equal(reputation(undefined, 8), undefined);
});
it('rep -1234123412342234 => -29.822227322', () => {
assert.equal(reputation(-1234123412342234, 9), -29.822227322);
});
it('rep -22233344455 => 12.8769568338', () => {
assert.equal(reputation(-22233344455, 10), 12.8769568338);
});
})

0 comments on commit 78ba61e

Please sign in to comment.