-
Notifications
You must be signed in to change notification settings - Fork 2
/
surprise_lipsum.js
74 lines (60 loc) · 2.39 KB
/
surprise_lipsum.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
var surprise_lipsum = window.surprise_lipsum || {};
surprise_lipsum.generator = function () {
var latin = "lorem ipsum dolor sit amet consectetuer adipiscing elit nam cursus morbi ut mi nullam enim leo egestas id condimentum at laoreet mattis massa sed eleifend nonummy diam praesent mauris ante elementum et bibendum at posuere sit amet nibh duis tincidunt lectus quis dui viverra vestibulum suspendisse vulputate aliquam dui nulla elementum dui ut augue aliquam vehicula mi at mauris maecenas placerat nisl at consequat rhoncus sem nunc gravida justo quis eleifend arcu velit quis lacus morbi magna magna tincidunt a mattis non imperdiet vitae tellus sed odio est auctor ac sollicitudin in consequat vitae orci fusce id felis vivamus sollicitudin metus eget eros".split(" ");
var punctuation = ['.','!','?',',',';','-',':'];
function random_bool_w_bias() {
var x = Math.random(),
bias = 0.75;
if (x < bias) {
return false;
} else {
return true;
}
}
function random_punctuation() {
var x = Math.floor(Math.random() * punctuation.length);
return punctuation[x];
}
function random_int(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function surprise_lipsum() {
$("*").filter(function(){
var match = $(this).text().match(/^\[lipsum (\d+)w (\d+)%\]/);
if(match) {
$(this).data("surprise-ideal", match[1]);
$(this).data("surprise-variance", match[2]);
}
return match;
}).each(function() {
var lipsum = generate_lipsum($(this).data("surprise-ideal"), $(this).data("surprise-variance"));
$(this).text(lipsum);
});
}
function generate_lipsum(ideal, variance) {
ideal = parseInt(ideal);
variance = ((ideal/100) * parseInt(variance));
var min = ideal - variance;
var max = ideal + variance;
var words = random_int(min, max);
var lipsum = "";
for(var x = 0; x < words; x++) {
index = random_int(0, latin.length - 1);
lipsum += latin[index];
var bool = random_bool_w_bias();
if (bool) {
lipsum += random_punctuation();
}
lipsum += " ";
}
return lipsum;
}
return {
surprise: function () {
surprise_lipsum();
}
};
} ();
$(function() {
surprise_lipsum.generator.surprise();
});