-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
47 lines (39 loc) · 1.37 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
module.exports = function({text = '', viewWidth = text.length, fill = ' ', interval = 100, fullTextFrames = 1} = {}) {
if (text === '') {
throw new Error('Must provide text to the marquee')
}
if (fullTextFrames > 1 && viewWidth < text.length) {
throw new Error('Unable to show full text when viewWidth is smaller than text.length');
}
if (viewWidth > text.length) {
const leftHalf = Math.floor((viewWidth - text.length) / 2);
const rightHalf = Math.ceil((viewWidth - text.length) / 2);
text = fill.repeat(leftHalf) + text + fill.repeat(rightHalf);
}
let frames = new Array(fullTextFrames);
frames.fill(text);
for (let i = 1; i <= text.length; i++) {
frames.unshift(fill.repeat(i) + text.substring(0, text.length - i));
frames.push(text.substring(i) + fill.repeat(i));
}
//truncate each frame if the viewWidth is shorter than text.length
if (viewWidth < text.length) {
frames = frames.map(frame => frame.substring(0, viewWidth));
}
removeRepeatEmptyFrames(frames);
return {
interval,
frames
};
};
/*
* Useful for removing leading and trailing frames that are just fill created from a viewWidth that is larger than the text.length
*/
function removeRepeatEmptyFrames(frames) {
while(frames[0] === frames[1]) {
frames.shift();
}
while(frames[frames.length-1] === frames[frames.length-2]) {
frames.pop();
}
}