-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
83 lines (71 loc) · 1.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* This file contains helpers and rankers used by bin/wiki2gource.js
*/
'use strict';
var Color = require('color');
/**
* Categories ranker used to generate pseudo-path to articles based
* on categories they're in and list of wiki's top categories
*
* @param {Array} topCategories
* @constructor
*/
function CategoriesRanker(topCategories) {
this.topCategories = topCategories;
}
/**
* Get the pseudo-path for the article using provided categories
*
* @param {string} title
* @param {Array} categories
* @param {int} [limit=5] -1 for no limit
* @returns {string}
*/
CategoriesRanker.prototype.getArticlePath = function(title, categories, limit) {
var path = [];
limit = limit || 5;
this.topCategories.forEach(function(cat) {
if (categories.indexOf(cat) > -1) {
path.push(cat);
}
});
// apply a limit
if (limit > 0) {
path = path.slice(0, limit);
}
path.push(title);
return path.join('/');
};
/**
* Colors ranker used to set the color of an entry based on
* the number of current article edits and
* the average value of edits per articles for a wiki
*
* @param {object} stats
* @param {string} [from] RGB color to start from
* @param {string} [to] RGB color to finish on
* @constructor
*/
function ColorsRanker(stats, from, to) {
this.edits = stats.edits;
this.articles = stats.articles;
this.edits_per_article = Math.ceil(this.edits / this.articles) * 2;
this.from = from || '#000000';
this.to = new Color(to || '#ffffff');
}
/**
* Return color for the n-th edit
*
* @param {int} idx edit index (starting from 0)
* @returns {string} RGB color (without a hash)
*/
ColorsRanker.prototype.getColorForEdit = function(idx) {
const from = Color(this.from || '#000000');
var mix = Math.min(1, (idx + 1) / this.edits_per_article);
return from.mix(this.to, mix).hex().substring(1);
};
// public API
module.exports = {
CategoriesRanker: CategoriesRanker,
ColorsRanker: ColorsRanker
};