-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
95 lines (79 loc) · 2.51 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
/*!
* isogram | MIT (c) Shinnosuke Watanabe
* https://github.com/shinnn/isogram
*/
var arrayToSentence = require('array-to-sentence');
var assertUnique = require('assert-unique');
var gaLoaderSnippets = require('ga-loader-snippets');
var gaTrackerSnippet = require('ga-tracker-snippet');
var isVarName = require('is-var-name');
module.exports = function isogram(characters, options) {
'use strict';
if (arguments.length === 0) {
characters = 'GoOgle';
} else if (typeof characters !== 'string') {
if (arguments.length !== 1 || typeof characters !== 'object') {
throw new TypeError('First argument must be a string or an object.');
}
options = characters;
characters = 'GoOgle';
}
if (options) {
if (typeof options !== 'object') {
throw new TypeError(
options +
' is not an object. Second argument must be an object.'
);
}
} else {
options = {};
}
var len = characters.length;
var invalidChars = [];
for (var i = 0; i < len; i++) {
var char = characters.charAt(i);
if (!isVarName(char) && i === characters.indexOf(char)) {
invalidChars.push('"' + char + '"');
}
}
if (invalidChars.length > 0) {
var isPlural = invalidChars.length !== 1;
throw new Error(
arrayToSentence(invalidChars) + ' cannot be used as ' +
(isPlural ? '' : 'a ') + 'JavaScript parameter name' + (isPlural ? 's' : '') + '.'
);
}
if (len < 3 || 7 < len) {
throw new RangeError('Number of characters must be no fewer than 3 and no greater than 7.');
}
assertUnique.apply(null, characters.split(''));
var gaTracker = gaTrackerSnippet(options);
var gaLoader = gaLoaderSnippets['with' + len + 'params']
.replace(
new RegExp('([A-' + String.fromCharCode(65 + len - 1) + '])(?=[^a-z])', 'g'),
function(str) {
var char = characters.charAt(str.charCodeAt(0) - 65);
if (options.color) {
// Colorize with ANSI green color
char = '\u001b[32m' + char + '\u001b[39m';
}
return char;
}
);
if (options.globalName) {
gaLoader = gaLoader.replace(/"ga"/g, '"' + options.globalName + '"');
}
if (options.singleQuotes === undefined || options.singleQuotes) {
gaLoader = gaLoader.replace(/"/g, '\'');
}
if (options.track === undefined || options.track) {
gaLoader = gaLoader + '\n\n' + gaTracker;
}
if (options.scriptTag) {
gaLoader = '<script>\n' + gaLoader + '\n</script>';
}
if (options.minify) {
gaLoader = gaLoader.replace(/\n/g, '');
}
return gaLoader;
};