This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
54 lines (42 loc) · 1.48 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
'use strict';
var randomInt = require('random-int');
var _assignIn = require('lodash/assignIn');
// parse words out of a string and mess them up
function Dsxyliea(str, options) {
var messedUpText = '';
// iterate through each word and scramble it
var re = /\w+/g;
var word;
while ((word = re.exec(str)) != null) {
// include any special characters before the word
messedUpText += str.slice(messedUpText.length, word.index);
// scramble the word
messedUpText += scrambleWord(word[0], options);
}
// include any special characters after the word
messedUpText += str.slice(messedUpText.length, str.length);
return messedUpText;
}
// scramble the word, being sure to always keep the first and last letters
// in-tact. this is important so the text can still be read.
function scrambleWord(word, options) {
options = _assignIn({}, options, {
minWordLength: 3,
scrambleChance: 100
});
if (options.scrambleChance > 100) {
options.scrambleChance = 100;
}
// if it's a small word or ~randomness~, don't scramble it
if (word.length < options.minWordLength || randomInt(100) > options.scrambleChance) {
return word;
}
var a = randomInt(1, word.length - 1);
var b = randomInt(a, word.length - 1);
var scrambledWord = word.slice(0, a) +
word.slice(a, b).split('').reverse().join('') +
word.slice(b, word.length);
return scrambledWord;
}
Dsxyliea.scrambleWord = scrambleWord;
module.exports = Dsxyliea;