Calculating a user's Steemit reputation score in JavaScript [bonus content: an Angular Pipe that does it for you]


Here's another tip for Steemit devs! When you retrieve a user's information from Steemit the reputation is a huge number, as you can see on sites like Steemd. This is usually called the "raw" reputation score.


My reputation at the moment of writing this post is over 1.2 trillion, but Steemit only shows it as 52

Simplified Reputation Score

Steemit and apps like eSteem show a "simplified" reputation score instead, a more human readable number roughly between -25 and 100*, with new users starting at 25.

The calculation to do this uses a base 10 logarithmic scale and a multiplication by 9, which effectively makes the reputation needed for going up 9 levels about 10x harder. Or to make it a little easier, see the following table

Simplified ReputationRaw Reputation
251,000,000,000
3410,000,000,000
43100,000,000,000
521,000,000,000,000
6110,000,000,000,000
70100,000,000,000,000

et cetera.

* Theoretically this number can go up to 100 and even above, but practically this probably won't happen for years. I don't think anyone has even reached 80 yet

The JavaScript

Here's the code that you can use to calculate this score:

    function simplifyReputation(raw) {
        const isNegative = (raw < 0);
        let reputation = Math.log10(Math.abs(raw));

        reputation = Math.max(reputation - 9, 0);
        reputation *= isNegative ? -9 : 9;
        reputation += 25;

        return Math.floor(reputation);
    }


The base 10 logarithm of a negative number always results in NaN, which is why we remember whether it was negative at first and then turn it positive on line 2, only to turn it back to negative again when multiplying by 9.

By the way, the original code from which I adapted this can be found on GitHub starting at the export const repLog10 line (line 46 as of this writing). It's a lot messier and even a little less exact, though probably more performant than my version.

Bonus content: an Angular Pipe

If you're developing an Angular app like me, you probably want to hide this all behind a pipe, so you can simply use something like this in your templates:

<span class="rep">{{user.reputation | reputation}}</span>

So here it is:

reputation.pipe.ts

import {Pipe, PipeTransform} from '@angular/core';

@Pipe({
    name: 'reputation'
})
export class ReputationPipe implements PipeTransform {

    transform(value: number) {
        const isNegative = (value < 0);
        let reputation = Math.log10(Math.abs(value));

        reputation = Math.max(reputation - 9, 0);
        reputation *= isNegative ? -9 : 9;
        reputation += 25;

        return Math.floor(reputation);
    }
}


I hope this will be of use to Steemit developers! If you have any questions or suggestions for improvement let me know in the comments ⬇️

H2
H3
H4
Upload from PC
Video gallery
3 columns
2 columns
1 column
13 Comments