Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "eslint:recommended",
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2017
},
"env": {
"browser": true,
"es6": true
},
"rules": {
"semi": [2, "always"]
}
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
coverage/
node_modules/
*.log
cjs/*
malarkey.min.js
171 changes: 90 additions & 81 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,134 +1,143 @@
var DELETE_ALL_SENTINEL = -1
const DELETE_ALL_SENTINEL = -1;

function noop () {}
function noop() {}

function malarkey (callback, options) {
options = options || {}
function malarkey(callback, options) {
options = options || {};

var text = ''
let text = '';

var functionQueue = []
var functionArguments = []
var functionIndex = -1
const functionQueue = [];
const functionArguments = [];
let functionIndex = -1;

var isStopped = true
var stoppedCallback = noop
let isStopped = true;
let stoppedCallback = noop;

var methods = {
call: function (fn) {
return enqueue(_call, [fn])
const methods = {

call(fn) {
return enqueue(_call, [fn]);
},
clear: function () {
return enqueue(_clear, null)

clear() {
return enqueue(_clear, null);
},
delete: function (characterCount, deleteOptions) {

delete(characterCount, deleteOptions) {
if (typeof characterCount === 'object') {
deleteOptions = characterCount
characterCount = DELETE_ALL_SENTINEL
deleteOptions = characterCount;
characterCount = DELETE_ALL_SENTINEL;
}
return enqueue(_delete, [
characterCount || DELETE_ALL_SENTINEL,
(deleteOptions ? deleteOptions.speed : options.deleteSpeed) || 50
])
(deleteOptions ? deleteOptions.speed : options.deleteSpeed) || 50,
]);
},
isStopped: function () {
return isStopped

isStopped() {
return isStopped;
},
pause: function (pauseOptions) {

pause(pauseOptions) {
return enqueue(setTimeout, [
(pauseOptions ? pauseOptions.duration : options.pauseDuration) || 2000
])
(pauseOptions ? pauseOptions.duration : options.pauseDuration) || 2000,
]);
},
triggerResume: function () {

triggerResume() {
if (isStopped) {
isStopped = false
next()
isStopped = false;
next();
}
return methods
return methods;
},
triggerStop: function (fn) {
isStopped = true
stoppedCallback = fn || noop
return methods

triggerStop(fn) {
isStopped = true;
stoppedCallback = fn || noop;
return methods;
},
type: function (text, typeOptions) {

type(text, typeOptions) {
return enqueue(_type, [
text,
(typeOptions ? typeOptions.speed : options.typeSpeed) || 50
])
}
}
(typeOptions ? typeOptions.speed : options.typeSpeed) || 50,
]);
},

};

function next () {
function next() {
if (isStopped) {
stoppedCallback(text)
stoppedCallback = noop
return
stoppedCallback(text);
stoppedCallback = noop;
return;
}
functionIndex += 1
functionIndex += 1;
if (functionIndex === functionQueue.length) {
if (!options.repeat) {
functionIndex = functionQueue.length - 1
isStopped = true
return
functionIndex = functionQueue.length - 1;
isStopped = true;
return;
}
functionIndex = 0
functionIndex = 0;
}
functionQueue[functionIndex].apply(
null,
[next].concat(functionArguments[functionIndex])
)
);
}

function enqueue (callback, args) {
functionQueue.push(callback)
functionArguments.push(args)
function enqueue(callback, args) {
functionQueue.push(callback);
functionArguments.push(args);
if (isStopped) {
isStopped = false
setTimeout(next, 0)
isStopped = false;
setTimeout(next, 0);
}
return methods
return methods;
}

function _type (next, typeText, typeSpeed) {
var length = typeText.length
var i = 0
setTimeout(function typeCharacter () {
text += typeText[i++]
callback(text)
function _type(next, typeText, typeSpeed) {
let length = typeText.length;
let i = 0;
setTimeout(function typeCharacter() {
text += typeText[i++];
callback(text);
if (i === length) {
next()
return
next();
return;
}
setTimeout(typeCharacter, typeSpeed)
}, typeSpeed)
setTimeout(typeCharacter, typeSpeed);
}, typeSpeed);
}

function _delete (next, characterCount, deleteSpeed) {
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)
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
next();
return;
}
setTimeout(deleteCharacter, deleteSpeed)
}, deleteSpeed)
setTimeout(deleteCharacter, deleteSpeed);
}, deleteSpeed);
}

function _clear (next) {
text = ''
callback(text)
next()
function _clear(next) {
text = '';
callback(text);
next();
}

function _call (next, fn) {
fn(next, text)
function _call(next, fn) {
fn(next, text);
}

return methods
return methods;
}

module.exports = malarkey
export default malarkey;
Loading