-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
144 lines (119 loc) · 3.97 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const crypto = require('crypto');
const _ = require('underscore');
const bigInt = require('big-integer');
const allParticles = {};
function parseParticles(particles) {
function findLengths(items) {
const lengths = [3, 4, 5, 6, 7, 8, 9];
const response = {};
items.forEach(function(item) {
lengths.forEach(function(length) {
if (item.length <= length) {
response[length] = response[length] ? response[length] + 1 : 1;
}
});
});
return response;
}
if (_.isObject(particles) && !_.isArray(particles)) {
_(particles).forEach(function(value, key) {
const newValue = Array.from(value);
allParticles[key] = {
items: newValue.sort((a, b) => (a.length === b.length ? a.localeCompare(b) : a.length - b.length)),
metadata: {
lengths: findLengths(newValue)
}
};
});
}
return allParticles;
}
parseParticles(require('./particles'), allParticles);
function parseOptions(options) {
if (options._isParsed) {
return options;
}
const response = {};
if (_.isString(options) || _.isNumber(options)) {
options = {
seed: options
};
}
response.maxItemChars = options && _.isNumber(options.maxItemChars) && options.maxItemChars > 0 ? Math.max(3, options.maxItemChars) : 0;
if (response.maxItemChars > 9 || !response.maxItemChars) {
delete response.maxItemChars;
}
if (options && _.isArray(options.particles)) {
response.particles = Array.from(options.particles);
} else {
// classic mode
response.particles = options && _.isNumber(options.adjectiveCount) ? new Array(options.adjectiveCount).fill('adjective') : ['adjective'];
response.particles.push('noun');
}
response.seed = (options && options.seed) || '';
if (_.isNumber(response.seed)) {
response.seed = response.seed.toString();
} else if (_.isObject(response.seed)) {
response.seed = JSON.stringify(response.seed);
}
response.hashAlgorithm = options && _.isString(options.hashAlgorithm) ? options.hashAlgorithm : 'md5';
response.separator = options && _.isString(options.separator) ? options.separator : '-';
if (options && options.capitalize === true) {
response.capitalize = options.capitalize;
}
response._isParsed = true;
return response;
}
function getParticles(options) {
const useOptions = parseOptions(options);
const response = [];
useOptions.particles.reverse();
useOptions.particles.forEach(function(particle) {
if (!allParticles[particle]) {
response.push(['unknown']);
} else {
response.push(useOptions.maxItemChars ? allParticles[particle].items.slice(0, allParticles[particle].metadata.lengths[useOptions.maxItemChars]) : allParticles[particle].items);
}
});
return response;
}
function getTotalWords(particles) {
let totalWords = bigInt(1);
particles.forEach(function(particle) {
totalWords = totalWords.multiply(bigInt(particle.length));
});
return totalWords;
}
function getHash(options) {
const useOptions = parseOptions(options);
const hash = crypto.createHash(useOptions.hashAlgorithm);
hash.update(useOptions.seed);
const hashDigest = hash.digest('hex');
return bigInt(hashDigest, 16).multiply('36413321723440003717');
}
function codenameParticles(options) {
const useOptions = parseOptions(options);
const particles = getParticles(useOptions);
const totalWords = getTotalWords(particles);
const objHash = getHash(useOptions);
let index = objHash.mod(totalWords);
const codenameParticles = [];
particles.forEach(function(particle) {
codenameParticles.push(particle[index.mod(particle.length).toJSNumber()]);
index = index.divide(particle.length);
});
codenameParticles.reverse();
return codenameParticles;
}
function codenamize(options) {
const useOptions = parseOptions(options);
let particles = codenameParticles(useOptions);
if (useOptions.capitalize) {
particles = particles.map(particle => particle.charAt(0).toUpperCase() + particle.substring(1));
}
return particles.join(useOptions.separator);
}
codenamize.use = function(particles) {
return parseParticles(particles, allParticles);
};
module.exports = codenamize;