├── .gitignore ├── .travis.yml ├── test.js ├── package.json ├── index.js ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | coverage/ 3 | tmp/ 4 | dist/ 5 | npm-debug.log* 6 | .DS_Store 7 | .nyc_output 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | node_js: 2 | - "4" 3 | - "5" 4 | - "6" 5 | - "7" 6 | sudo: false 7 | language: node_js 8 | script: "npm run test" 9 | after_success: "npm i -g codecov && npm run coverage && codecov" 10 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var tape = require('tape') 2 | 3 | var rank = require('./') 4 | 5 | tape('should rank stuff', function (t) { 6 | var start = new Date(1, 12, 2000) 7 | var a, b 8 | 9 | a = rank(1, 3, new Date(12, 12, 2000), start) 10 | b = rank(5, 1, new Date(12, 12, 2000), start) 11 | t.ok(b > a, 'larger') 12 | 13 | a = rank(2, 0, new Date(1, 12, 2000), start) 14 | b = rank(1, 0, new Date(1, 12, 2000), start) 15 | t.ok(a > b, 'larger') 16 | 17 | a = rank(1, 0, new Date(1, 12, 2000), start) 18 | b = rank(1, 1, new Date(1, 12, 2000), start) 19 | t.ok(a === b, 'single downvote is not disastrous') 20 | 21 | t.end() 22 | }) 23 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "pretty-hot-ranking-algorithm", 3 | "description": "Algorithm that measures how relevant a given data set is, kinda like Reddit", 4 | "repository": "yoshuawuyts/pretty-hot-ranking-algorithm", 5 | "version": "1.0.1", 6 | "scripts": { 7 | "deps": "dependency-check . && dependency-check . --extra --no-dev", 8 | "start": "node .", 9 | "test": "standard && npm run deps && nyc node test.js", 10 | "coverage": "nyc report --reporter=text-lcov > coverage.lcov" 11 | }, 12 | "dependencies": {}, 13 | "devDependencies": { 14 | "dependency-check": "^2.8.0", 15 | "nyc": "^10.2.0", 16 | "standard": "^10.0.0", 17 | "tape": "^4.6.3" 18 | }, 19 | "keywords": [ 20 | "algorithm", 21 | "cool" 22 | ] 23 | } 24 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert') 2 | 3 | var HALF_LIFE = 45000 // 12.5 hours 4 | 5 | module.exports = rank 6 | 7 | function rank (upvotes, downvotes, date, start) { 8 | assert.equal(typeof upvotes, 'number', 'rank: upvotes should be type number') 9 | assert.equal(typeof downvotes, 'number', 'rank: downvotes should be type number') 10 | assert.ok(date instanceof Date, 'rank: date should be an instance of Date') 11 | assert.ok(start instanceof Date, 'rank: start should be an instance of Date') 12 | 13 | var seconds = (date.getTime() - start.getTime()) / 1000 14 | var score = upvotes - downvotes 15 | var order = Math.log(Math.max(score, 1), 10) 16 | var sign = score > 0 ? 1 : score === 0 ? 0 : -1 17 | return Math.round(sign * order + seconds / HALF_LIFE, 7) 18 | } 19 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Yoshua Wuyts 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # pretty-hot-ranking-algorithm [![stability][0]][1] 2 | [![npm version][2]][3] [![build status][4]][5] 3 | [![downloads][8]][9] [![js-standard-style][10]][11] 4 | 5 | Algorithm that measures how relevant a given data set is, kinda like Reddit 6 | 7 | Doesn't do any jitter stuff and can probably be gamed if you try hard enough, 8 | but it should be useful enough if you want to wire up community-driven news 9 | site that always has fresh, relevant content for people to discover. 10 | 11 | ## Usage 12 | ```js 13 | var rank = require('pretty-hot-ranking-algorithm') 14 | 15 | var start = new Date(20, 2, 2006) 16 | var upvotes = 15 17 | var downvotes = 4 18 | var date = new Date() 19 | 20 | var itemRank = rank(upvotes, downvotes, date, start) 21 | ``` 22 | 23 | ## API 24 | ### `itemRank = rank(upvotes, downvotes, date, start)` 25 | Generate an item rank based off upvotes, downvotes and the current date. The 26 | last argument is the time of the first post, generally this should be close to 27 | when the server first goes live. 28 | 29 | ## See Also 30 | - [How the reddit ranking algorithm works](https://medium.com/hacking-and-gonzo/how-reddit-ranking-algorithms-work-ef111e33d0d9) 31 | 32 | ## License 33 | [MIT](https://tldrlegal.com/license/mit-license) 34 | 35 | [0]: https://img.shields.io/badge/stability-experimental-orange.svg?style=flat-square 36 | [1]: https://nodejs.org/api/documentation.html#documentation_stability_index 37 | [2]: https://img.shields.io/npm/v/pretty-hot-ranking-algorithm.svg?style=flat-square 38 | [3]: https://npmjs.org/package/pretty-hot-ranking-algorithm 39 | [4]: https://img.shields.io/travis/yoshuawuyts/pretty-hot-ranking-algorithm/master.svg?style=flat-square 40 | [5]: https://travis-ci.org/yoshuawuyts/pretty-hot-ranking-algorithm 41 | [6]: https://img.shields.io/codecov/c/github/yoshuawuyts/pretty-hot-ranking-algorithm/master.svg?style=flat-square 42 | [7]: https://codecov.io/github/yoshuawuyts/pretty-hot-ranking-algorithm 43 | [8]: http://img.shields.io/npm/dm/pretty-hot-ranking-algorithm.svg?style=flat-square 44 | [9]: https://npmjs.org/package/pretty-hot-ranking-algorithm 45 | [10]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square 46 | [11]: https://github.com/feross/standard 47 | --------------------------------------------------------------------------------