From 8c6804beecbd7ff9a4e40ecbb0489c7edf3a55ad Mon Sep 17 00:00:00 2001 From: Eric Naeseth Date: Fri, 13 Jan 2017 11:40:42 -0800 Subject: [PATCH] Add types for decay (#13982) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add an optional extended description… --- decay/decay-tests.ts | 22 +++++++++++++++++++ decay/index.d.ts | 52 ++++++++++++++++++++++++++++++++++++++++++++ decay/tsconfig.json | 20 +++++++++++++++++ decay/tslint.json | 1 + 4 files changed, 95 insertions(+) create mode 100644 decay/decay-tests.ts create mode 100644 decay/index.d.ts create mode 100644 decay/tsconfig.json create mode 100644 decay/tslint.json diff --git a/decay/decay-tests.ts b/decay/decay-tests.ts new file mode 100644 index 0000000000..41b67b6068 --- /dev/null +++ b/decay/decay-tests.ts @@ -0,0 +1,22 @@ +import {redditHot, hackerHot, wilsonScore} from 'decay'; + +const upvotes = 42; +const downvotes = 12; +const posted = new Date(2017, 1, 1); + +let score: number; + +let redditCalculator = redditHot(); +score = redditCalculator(upvotes, downvotes, posted); +redditCalculator = redditHot(10000); +score = redditCalculator(upvotes, downvotes, posted); + +let hackerCalculator = hackerHot(); +score = hackerCalculator(upvotes, posted); +hackerCalculator = hackerHot(1.6); +score = hackerCalculator(upvotes, posted); + +let wilsonCalculator = wilsonScore(); +score = wilsonCalculator(upvotes, downvotes); +wilsonCalculator = wilsonScore(1.0); +score = wilsonCalculator(upvotes, downvotes); \ No newline at end of file diff --git a/decay/index.d.ts b/decay/index.d.ts new file mode 100644 index 0000000000..1f8fcabe09 --- /dev/null +++ b/decay/index.d.ts @@ -0,0 +1,52 @@ +// Type definitions for decay 1.0 +// Project: https://github.com/clux/decay +// Definitions by: Eric Naeseth +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/** + * Creates a function to rank posts using Reddit's "hot" algorithm. + * @param decay controls how quickly rankings drop with time + * @return calculator function + */ +export function redditHot(decay?: number): RedditHotFunction; + +/** + * Creates a function to rank posts using the Hacker News "hot" algorithm. + * @param gravity controls how quickly rankings drop with time + * @return calculator function + */ +export function hackerHot(gravity?: number): HackerNewsHotFunction; + +/** + * Creates a function to rank posts using the Wilson score interval sort (Reddit's "best" + * algorithm). + * @param confidence statistical confidence + * @return calculator function + * @see {@link https://redditblog.com/2009/10/15/reddits-new-comment-sorting-system/ Reddit's writeup} + */ +export function wilsonScore(confidence?: number): WilsonScoreFunction; + +/** + * Computes a ranking using Reddit's "hot" algorithm. + * @param upvotes number of upvotes the post has received + * @param downvotes number of upvotes the post has received + * @param date when the post was posted + * @return ranking + */ +export type RedditHotFunction = (upvotes: number, downvotes: number, date: Date) => number; + +/** + * Computes a ranking using the Hacker News "hot" algorithm. + * @param votes number of upvotes the post has received + * @param date when the post was posted + * @return ranking + */ +export type HackerNewsHotFunction = (votes: number, date: Date) => number; + +/** + * Computes a ranking using the Wilson score (Reddit's "best" algorithm). + * @param upvotes number of upvotes the post has received + * @param downvotes number of upvotes the post has received + * @return ranking + */ +export type WilsonScoreFunction = (upvotes: number, downvotes: number) => number; diff --git a/decay/tsconfig.json b/decay/tsconfig.json new file mode 100644 index 0000000000..bd236be020 --- /dev/null +++ b/decay/tsconfig.json @@ -0,0 +1,20 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es6", + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "decay-tests.ts" + ] +} diff --git a/decay/tslint.json b/decay/tslint.json new file mode 100644 index 0000000000..377cc837d4 --- /dev/null +++ b/decay/tslint.json @@ -0,0 +1 @@ +{ "extends": "../tslint.json" }