-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
52 lines (45 loc) · 1.8 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
var replace = require('gulp-replace');
var escapeStringForRegex = require('escape-string-regexp');
var homographs = require('./homographs.json');
// haystack - {Array}
// Returns one item
var getRandomItem = function(haystack){
return haystack[Math.round(Math.random() * (haystack.length - 1))];
};
// args - {Object}
// :mode - (Optional) {String}. Defaults to `greek`.
// :charactersToReplace - (Optional) {String}
module.exports = function(args) {
args = args || {};
args.mode = args.mode || 'greek';
var charactersToReplace;
if(args.charactersToReplace){
charactersToReplace = args.charactersToReplace;
if(typeof charactersToReplace === 'string'){
charactersToReplace = charactersToReplace.split('');
}
// discard unsupported characters
charactersToReplace = charactersToReplace.filter(function(character){
return !!homographs[character];
});
}
else {
if (args.mode === 'greek') {
return replace(';', homographs[';'][0]);
}
var replaceableCharacters = Object.keys(homographs);
if (args.mode === 'one') {
charactersToReplace = [getRandomItem(replaceableCharacters)];
}
else if (args.mode === 'all') {
charactersToReplace = replaceableCharacters;
}
}
// build regular expression from the characters (escaped) which would match any
var search = new RegExp(charactersToReplace.map(escapeStringForRegex).join('|'), 'g');
// hand off the real work to gulp-replace (and replaceable-stream)
return replace(search, function(characterToReplace){
// if the character has multple homoglyphs, take one at random (per occurrence)
return getRandomItem(homographs[characterToReplace].split(''), true);
});
};