Skip to content

Commit 78ba61e

Browse files
authored
Merge pull request #512 from DoctorLai/patch-1
Add more digits to steem.formatter.reputation and Add some tests
2 parents 13523bc + efed2cf commit 78ba61e

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

src/formatter.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,19 @@ module.exports = steemAPI => {
155155
}
156156

157157
return {
158-
reputation: function(reputation) {
158+
reputation: function(reputation, decimal_places = 0) {
159159
if (reputation == 0) return 25;
160160
if (!reputation) return reputation;
161161
let neg = reputation < 0;
162162
let rep = String(reputation);
163163
rep = neg ? rep.substring(1) : rep;
164164
let v = (Math.log10((rep > 0 ? rep : -rep) - 10) - 9);
165165
v = neg ? -v : v;
166-
return parseInt(v * 9 + 25);
166+
v = v * 9 + 25;
167+
if (decimal_places > 0) {
168+
return +(Math.round(v + "e+" + decimal_places) + "e-" + decimal_places);
169+
}
170+
return parseInt(v);
167171
},
168172

169173
vestToSteem: function(

test/reputation.test.js

+33-1
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,37 @@ describe('steem.format.reputation', ()=> {
3232
});
3333
it('rep -22233344455 => 12', () => {
3434
assert.equal(reputation(-22233344455), 12);
35-
});
35+
});
36+
37+
// with decimal places
38+
it('rep 0 => 25', () => {
39+
assert.equal(reputation(0, 1), 25);
40+
});
41+
it('rep 95832978796820 => 69.83', () => {
42+
assert.equal(reputation(95832978796820, 2), 69.83);
43+
});
44+
it('rep 10004392664120 => 61.002', () => {
45+
assert.equal(reputation(10004392664120, 3), 61.002);
46+
});
47+
it('rep 30999525306309 => 65.4222', () => {
48+
assert.equal(reputation(30999525306309, 4), 65.4222);
49+
});
50+
it('rep -37765258368568 => -16.19383', () => {
51+
assert.equal(reputation(-37765258368568, 5), -16.19383);
52+
});
53+
it('rep 334486135407077 => 74.719403', () => {
54+
assert.equal(reputation(334486135407077, 6), 74.719403);
55+
});
56+
it('rep null => null', () => {
57+
assert.equal(reputation(null, 7), null);
58+
});
59+
it('rep undefined => undefined', () => {
60+
assert.equal(reputation(undefined, 8), undefined);
61+
});
62+
it('rep -1234123412342234 => -29.822227322', () => {
63+
assert.equal(reputation(-1234123412342234, 9), -29.822227322);
64+
});
65+
it('rep -22233344455 => 12.8769568338', () => {
66+
assert.equal(reputation(-22233344455, 10), 12.8769568338);
67+
});
3668
})

0 commit comments

Comments
 (0)