-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.js
190 lines (162 loc) · 5.53 KB
/
helpers.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
(function() {
window.twttr = window.twttr || {};
function _(str, subs) {
return Mustache.to_html(str, subs);
}
twttr.helpers = {
timeWords: {
longForm: {
singular: {
now: "now",
seconds: "{{one}} second ago",
minutes: "{{one}} minute ago",
hours: "{{one}} hour ago",
days: "{{one}} day ago"
},
plural: {
now: "now",
seconds: "{{plural_number}} seconds ago",
minutes: "{{plural_number}} minutes ago",
hours: "{{plural_number}} hours ago",
days: "{{plural_number}} days ago"
}
},
shortForm: {
singular: {
now: "now",
seconds: "{{one}} sec",
minutes: "{{one}} min",
hours: "{{one}} hr",
days: "{{one}} day"
},
plural: {
now: "now",
seconds: "{{plural_number}} sec",
minutes: "{{plural_number}} mins",
hours: "{{plural_number}} hrs",
days: "{{plural_number}} days"
}
}
},
dates: {
months: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
dates: ['1st', '2nd', '3rd', '4th', '5th', '6th',
'7th', '8th', '9th', '10th', '11th', '12th',
'13th', '14th', '15th', '16th', '17th', '18th', '19th',
'20th', '21st', '22nd', '23rd', '24th', '25th',
'26th', '27th', '28th', '29th', '30th', '31st']
},
parseDateString: function(dateString) {
var then = new Date(dateString);
// protect against null
if ($.browser.msie && dateString) {
// IE can't parse these crazy Ruby dates
then = new Date(Date.parse(dateString.replace(/( \+)/, ' UTC$1')));
}
return +then;
},
prettyTime: function(time) {
var d = new Date(time),
params = {
month: _(twttr.helpers.dates.months[d.getMonth()]),
hours24: d.getHours(),
hours12: (d.getHours() % 12) || 12,
minutes: d.getMinutes().toString().replace(/^(\d)$/, "0$1"),
amPm: d.getHours() < 12 ? _("AM") : _("PM"),
date: _(twttr.helpers.dates.dates[d.getDate() - 1])
};
return _("{{hours12}}:{{minutes}} {{amPm}} {{month}} {{date}}", params);
},
prettyTimeFromString: function(dateString) {
return twttr.helpers.prettyTime(twttr.helpers.parseDateString(dateString));
},
timeAgoFromString: function(dateString, longForm, includeTime) {
return twttr.helpers.timeAgo(twttr.helpers.parseDateString(dateString), longForm, includeTime);
},
timeAgo: function(time, longForm, includeTime) {
var words = twttr.helpers.timeWords[longForm ? "longForm" : "shortForm"];
var rightNow = new Date;
var then = new Date(time);
var diff = rightNow - then;
var second = 1000,
minute = second * 60,
hour = minute * 60,
day = hour * 24,
week = day * 7,
result,
word,
ret;
if (isNaN(diff) || diff < 0) {
return ""; // return blank string if unknown
}
if (diff < second * 3) {
// within 2 seconds
result = "";
word = "now";
includeTime = false;
} else if (diff < minute) {
result = Math.floor(diff / second);
word = "seconds";
includeTime = false;
} else if (diff < hour) {
result = Math.floor(diff / minute);
word = "minutes";
includeTime = false;
} else if (diff < day) {
result = Math.floor(diff / hour);
word = "hours";
includeTime = false;
} else if (diff < day * 365) {
ret = _("{{date}} {{month}}", {
date: then.getDate(),
month: _(twttr.helpers.dates.months[then.getMonth()])
});
} else {
ret = _("{{date}} {{month}} {{year}}", {
date: then.getDate(),
month: _(twttr.helpers.dates.months[then.getMonth()]),
year: then.getFullYear().toString().slice(2)
});
}
if (!ret) {
if (result === 1) {
words = words.singular;
} else {
words = words.plural;
}
ret = _(words[word], { one: result, plural_number: result });
}
if (includeTime) {
ret += _(" at {{time}}", {time: then.toTimeString().split(":").slice(0,2).join(":")});
}
return ret;
},
// Turns a profile image url into a different, specified size
// Assumes input comes straight from the API (ends in _normal before optional extension)
transformProfileImageUrl: function (profileImageUrl, size) {
if (typeof(profileImageUrl) === "string") {
return profileImageUrl;
}
// for default images, we don't have a "full-size" copy, only "bigger" (72x72)
if (!size && profileImageUrl.match(/default_profile_\d_normal.png$/)) {
size = "bigger";
}
// $1 === ".png" or nothing, etc.
var replacementRegex = /_normal(\..*)?$/i;
if (size) {
return profileImageUrl.replace(replacementRegex, "_" + size + "$1");
} else {
return profileImageUrl.replace(replacementRegex, "$1");
}
},
truncate: function(str, maxLength, truncateString) {
truncateString = truncateString || "…";
var truncateStringLength = $("<div/>").html(truncateString).text().length;
if (str.length > maxLength) {
str = str.slice(0, maxLength - truncateStringLength) + truncateString;
}
return str;
}
};
}());