-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_generator.js
69 lines (60 loc) · 2.55 KB
/
data_generator.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
/*
* NOTE: This file generates fake tweet data, and is not intended to be part of your implementation.
* You can safely leave this file untouched, and confine your changes to index.html.
*/
// set up data structures
window.streams = {};
streams.home = [];
streams.users = {};
streams.users.shawndrost = [];
streams.users.sharksforcheap = [];
streams.users.mracus = [];
streams.users.douglascalhoun = [];
window.users = Object.keys(streams.users);
// utility function for adding tweets to our data structures
var addTweet = function(newTweet){
var username = newTweet.user;
streams.users[username].push(newTweet);
streams.home.push(newTweet);
};
// utility function
var randomElement = function(array){
var randomIndex = Math.floor(Math.random() * array.length);
return array[randomIndex];
};
// random tweet generator
var opening = ['just', '', '', '', '', 'ask me how i', 'completely', 'nearly', 'productively', 'efficiently', 'last night i', 'the president', 'that wizard', 'a ninja', 'a seedy old man'];
var verbs = ['drank', 'drunk', 'deployed', 'got', 'developed', 'built', 'invented', 'experienced', 'fought off', 'hardened', 'enjoyed', 'developed', 'consumed', 'debunked', 'drugged', 'doped', 'made', 'wrote', 'saw'];
var objects = ['my', 'your', 'the', 'a', 'my', 'an entire', 'this', 'that', 'the', 'the big', 'a new form of'];
var nouns = ['cat', 'koolaid', 'system', 'city', 'worm', 'cloud', 'potato', 'money', 'way of life', 'belief system', 'security system', 'bad decision', 'future', 'life', 'pony', 'mind'];
var tags = ['#techlife', '#burningman', '#sf', 'but only i know how', 'for real', '#sxsw', '#ballin', '#omg', '#yolo', '#magic', '', '', '', ''];
var randomMessage = function(){
return [randomElement(opening), randomElement(verbs), randomElement(objects), randomElement(nouns), randomElement(tags)].join(' ');
};
// generate random tweets on a random schedule
var generateRandomTweet = function(){
var tweet = {};
tweet.user = randomElement(users);
tweet.message = randomMessage();
tweet.created_at = new Date();
addTweet(tweet);
};
for(var i = 0; i < 10; i++){
generateRandomTweet();
}
var scheduleNextTweet = function(){
generateRandomTweet();
setTimeout(scheduleNextTweet, Math.random() * 1500);
};
scheduleNextTweet();
// utility function for letting students add "write a tweet" functionality
// (note: not used by the rest of this file.)
var writeTweet = function(message){
if(!visitor){
throw new Error('set the global visitor property!');
}
var tweet = {};
tweet.user = visitor;
tweet.message = message;
addTweet(tweet);
};