This repository has been archived by the owner on Dec 6, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweet-baker.js
142 lines (122 loc) · 4.15 KB
/
tweet-baker.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
/* global define */
(function (root) {
'use strict';
// Default entity templates
var defaultTemplates = {
user_mentions: function (ctx) {
return [
'<a href="https://twitter.com/' + ctx.screen_name + '" title="' + ctx.name + '">',
'@' + ctx.screen_name,
'</a>'
].join('');
},
media: function (ctx) {
return [
'<a href="' + ctx.url + '">',
ctx.display_url,
'</a>'
].join('');
},
hashtags: function (ctx) {
return [
'<a href="https://twitter.com/search?q=%23' + ctx.text + '">',
'#' + ctx.text,
'</a>'
].join('');
},
urls: function (ctx) {
return [
'<a href="' + ctx.url + '">',
ctx.display_url,
'</a>'
].join('');
}
};
// Create default baker
var bake = make(defaultTemplates);
bake.make = make;
bake.defaultTemplates = defaultTemplates;
// Make a baker
function make (templates) {
assertIsObject(templates, 'Invalid templates argument, object expected');
return function (tweet) {
assertIsObject(tweet, 'Invalid tweet, object expected');
assertIsObject(tweet.entities, 'Invalid tweet.entities, object expected');
assertIsString(tweet.text, 'Invalid tweet.text, string expected');
return bakeEntities(tweet, templates);
};
}
// Bake tweet entities using a set of templates
function bakeEntities (tweet, templates) {
var entityReplacements = renderEntities(tweet.entities, templates);
return replaceBetweenMultipleIndices(tweet.text, entityReplacements);
}
// Render entities based on a set of templates
function renderEntities (entities, templates) {
var rendered = [], type, len, i;
for (type in entities) {
if (entities.hasOwnProperty(type) && templates[type]) {
len = entities[type].length;
for (i = 0; i < len; i += 1) {
rendered.push([
templates[type](entities[type][i]),
entities[type][i].indices[0],
entities[type][i].indices[1]
]);
}
}
}
return rendered;
}
// Replace characters in a string between multiple pairs of indices
function replaceBetweenMultipleIndices (str, replacements) {
replacements = cloneArray(replacements).sort(function (a, b) {
return (b[1] - a[1]);
});
for (var i = 0, len = str.length, replacement; i < len; i ++) {
replacement = replacements[i];
if (replacement) {
str = replaceBetweenIndices(str, replacement[0], replacement[1], replacement[2]);
}
}
return str;
}
// Replace characters in a string between two indices
function replaceBetweenIndices (str, replace, start, end) {
return str.substr(0, start) + replace + str.substr(end);
}
// Assert that a value is an object
function assertIsObject (val, errMsg) {
if (val === null || isArray(val) || typeof val !== 'object') {
throw new Error(errMsg);
}
}
// Assert that a value is a string
function assertIsString (val, errMsg) {
if (typeof val !== 'string') {
throw new Error(errMsg);
}
}
// Cross-browser isArray
function isArray (val) {
return (Object.prototype.toString.call(val) === '[object Array]');
}
// Clone an array
function cloneArray (arr) {
return Array.prototype.slice.call(arr);
}
// Export: AMD
if (typeof define !== 'undefined' && define.amd) {
define([], function () {
return bake;
});
}
// Export: CommonJS
else if (typeof module !== 'undefined' && module.exports) {
module.exports = bake;
}
// Export: Script tag
else {
root.bake = bake;
}
} (this));