-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
134 lines (120 loc) · 2.95 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
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
var DELETE_ALL_SENTINEL = -1
function noop () {}
function malarkey (callback, options) {
options = options || {}
var text = ''
var functionQueue = []
var functionArguments = []
var functionIndex = -1
var isStopped = true
var stoppedCallback = noop
var methods = {
call: function (fn) {
return enqueue(_call, [fn])
},
clear: function () {
return enqueue(_clear, null)
},
delete: function (characterCount, deleteOptions) {
if (typeof characterCount === 'object') {
deleteOptions = characterCount
characterCount = DELETE_ALL_SENTINEL
}
return enqueue(_delete, [
characterCount || DELETE_ALL_SENTINEL,
(deleteOptions ? deleteOptions.speed : options.deleteSpeed) || 50
])
},
isStopped: function () {
return isStopped
},
pause: function (pauseOptions) {
return enqueue(setTimeout, [
(pauseOptions ? pauseOptions.duration : options.pauseDuration) || 2000
])
},
triggerResume: function () {
if (isStopped) {
isStopped = false
next()
}
return methods
},
triggerStop: function (fn) {
isStopped = true
stoppedCallback = fn || noop
return methods
},
type: function (text, typeOptions) {
return enqueue(_type, [
text,
(typeOptions ? typeOptions.speed : options.typeSpeed) || 50
])
}
}
function next () {
if (isStopped) {
stoppedCallback(text)
stoppedCallback = noop
return
}
functionIndex += 1
if (functionIndex === functionQueue.length) {
if (!options.repeat) {
functionIndex = functionQueue.length - 1
isStopped = true
return
}
functionIndex = 0
}
functionQueue[functionIndex].apply(
null,
[next].concat(functionArguments[functionIndex])
)
}
function enqueue (callback, args) {
functionQueue.push(callback)
functionArguments.push(args)
if (isStopped) {
isStopped = false
setTimeout(next, 0)
}
return methods
}
function _type (next, typeText, typeSpeed) {
var length = typeText.length
var i = 0
setTimeout(function typeCharacter () {
text += typeText[i++]
callback(text)
if (i === length) {
next()
return
}
setTimeout(typeCharacter, typeSpeed)
}, typeSpeed)
}
function _delete (next, characterCount, deleteSpeed) {
var finalLength =
characterCount === DELETE_ALL_SENTINEL ? 0 : text.length - characterCount
setTimeout(function deleteCharacter () {
text = text.substring(0, text.length - 1)
callback(text)
if (text.length === finalLength) {
next()
return
}
setTimeout(deleteCharacter, deleteSpeed)
}, deleteSpeed)
}
function _clear (next) {
text = ''
callback(text)
next()
}
function _call (next, fn) {
fn(next, text)
}
return methods
}
module.exports = malarkey