forked from Recidvst/scrambling-letters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrambleActions.js
70 lines (63 loc) · 2.36 KB
/
scrambleActions.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
/* eslint-disable import/prefer-default-export, no-console */
// export actions for the Scrambler fn
// import randomChar
import { randomChar, isFunction } from './scrambleUtil.js';
// sets args after checking
export const setArgs = function (args, t) {
let passedAsObject = t || false;
// default args
let obj = {
target: '[data-scrambler]',
random: [1000, 3000],
speed: 100,
text: false,
beforeEach: false,
afterEach: false,
beforeAll: false,
afterAll: false,
errorHandler: false
};
// update if arg object present
if ( args && passedAsObject ) {
obj.target = (typeof args.target !== 'undefined') ? args.target : '[data-scrambler]';
obj.random = (typeof args.random !== 'undefined') ? args.random : [1000, 3000];
obj.speed = (typeof args.speed !== 'undefined') ? args.speed : 100;
obj.text = (typeof args.text !== 'undefined') ? args.text : false;
obj.beforeEach = (typeof args.beforeEach !== 'undefined' && isFunction(args.beforeEach)) ? args.beforeEach : false;
obj.afterEach = (typeof args.afterEach !== 'undefined' && isFunction(args.afterEach)) ? args.afterEach : false;
obj.beforeAll = (typeof args.beforeAll !== 'undefined' && isFunction(args.beforeAll)) ? args.beforeAll : false;
obj.afterAll = (typeof args.afterAll !== 'undefined' && isFunction(args.afterAll)) ? args.afterAll : false;
obj.errorHandler = (typeof args.errorHandler !== 'undefined' && isFunction(args.errorHandler)) ? args.errorHandler : false;
}
return obj;
}
// check if animation has completed
export const killCheck = function(newLetters, truth) {
let result = (newLetters.length === truth.length) && newLetters.every((e, i) => e === truth[i]);
if ( result ) {
return true;
}
return false;
}
// set end state of text if user specifies
export const defineEndText = function(end) {
if ( !end || end === undefined || !(typeof end === 'string' || end instanceof String) ) return false; // break if no string passed
const endText = end;
const truth = endText.split('');
const newLetters = endText.split('');
const startTextTemp = [];
let startText;
truth.forEach((item, index) => {
if (' \t\n\r\v'.indexOf(truth[index]) > -1) {
startTextTemp.push(' ');
} else {
startTextTemp.push(randomChar());
}
});
startText = startTextTemp;
return {
truth,
newLetters,
startText
}
};