Skip to content
Permalink
Browse files

feat: 🎸 add controversialReddit() function

  • Loading branch information
streamich committed Apr 15, 2019
1 parent ea1fa46 commit 8c84f9d8933c7a04e98abf844bcdfe310b4f7990
Showing with 48 additions and 3 deletions.
  1. +31 −1 src/__tests__/index.spec.ts
  2. +17 −2 src/index.ts
@@ -1,4 +1,4 @@
import {hotReddit, bestReddit, hotYCombinator} from '..';
import {hotReddit, bestReddit, hotYCombinator, controversialReddit} from '..';

describe('hotReddit', () => {
it('gives higher rating to never content', async () => {
@@ -83,3 +83,33 @@ describe('hotYCombinator', () => {
expect(score2).toBeGreaterThan(score1);
});
});

describe('controversialReddit', () => {
it('gives higher score to content with more down votes', async () => {
const score1 = controversialReddit(100, 10);
const score2 = controversialReddit(100, 50);

expect(score2).toBeGreaterThan(score1);
});

it('gives higher score to content with more down votes - 2', async () => {
const score1 = controversialReddit(100, 50);
const score2 = controversialReddit(100, 120);

expect(score2).toBeGreaterThan(score1);
});

it('ranks higher content with more activity', async () => {
const score1 = controversialReddit(2, 2);
const score2 = controversialReddit(100, 100);

expect(score2).toBeGreaterThan(score1);
});

it('ranks higher content with more activity - 2', async () => {
const score1 = controversialReddit(10, 20);
const score2 = controversialReddit(100, 110);

expect(score2).toBeGreaterThan(score1);
});
});
@@ -2,7 +2,7 @@
* Reddit's hot content scoring function. Used for raking higher most recent and
* highest voted content. More recent content items get higher score automatically.
*
* - See: https://medium.com/hacking-and-gonzo/how-reddit-ranking-algorithms-work-ef111e33d0d9
* - See: https://github.com/reddit-archive/reddit/blob/d990533d0b57a499cefcec70f4c51d8c5593c497/r2/r2/lib/db/_sorts.pyx#L47-L58
*
* @param ups Number of up votes.
* @param downs Number of down votes.
@@ -33,7 +33,7 @@ export function hotYCombinator (ups: number, ts: number = Date.now()) {
* Reddit's content scoring function for finding best replies regardless of time
* when content was created. This function does not penalize old content.
*
* - See: https://medium.com/hacking-and-gonzo/how-reddit-ranking-algorithms-work-ef111e33d0d9
* - See: https://github.com/reddit-archive/reddit/blob/d990533d0b57a499cefcec70f4c51d8c5593c497/r2/r2/lib/db/_sorts.pyx#L70-L85
*
* @param ups Number of up votes.
* @param downs Number of down votes.
@@ -48,3 +48,18 @@ export function bestReddit (ups: number, downs: number) {
const under = 1 + 1 / n * z * z;
return (left - right) / under;
}

/**
* Reddit's algorithm for scoring high controversial content.
*
* - See: https://github.com/reddit-archive/reddit/blob/d990533d0b57a499cefcec70f4c51d8c5593c497/r2/r2/lib/db/_sorts.pyx#L60-L68
*
* @param ups Number of up votes.
* @param downs Number of down votes.
*/
export function controversialReddit (ups: number, downs: number) {
if (!ups || !downs) return 0;
const magnitude = ups + downs;
const balance = ups > downs ? (downs / ups) : (ups / downs);
return magnitude ** balance;
}

0 comments on commit 8c84f9d

Please sign in to comment.
You can’t perform that action at this time.