-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
92 lines (76 loc) · 2.24 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
var _ = require('lodash'),
path = require('path');
var filters = require(path.resolve(__dirname, 'src', 'filters')),
generate = require(path.resolve(__dirname, 'src', 'generate')),
loadLists = require(path.resolve(__dirname, 'src', 'load'));
function getFunction (obj, key) {
if (_.isFunction(key)) return key;
return obj[key];
}
function referenceError (type, arg) {
return new ReferenceError('Unknown ' + type + '(s): ' + arg);
}
/**
* Build a codename generator
*
* @id codename
* @param {Object} opts - An object containing one param (`dir`) describing
* where to find word lists
* @type Function
* @return {Object}
*/
module.exports = function (opts) {
var defaults = {
dir: path.resolve(__dirname, 'resources')
};
var options = _.extend({}, defaults, opts);
var lists = loadLists(options.dir);
var getList = _.partial(getFunction, lists),
getFilter = _.partial(getFunction, filters);
var exports = {};
/**
* A list of available filters
*
* @id filters
* @group exports
* @return {Array}
*/
exports.filters = _.partial(_.keys, filters);
/**
* A codename gneerator.
*
* var codename = require('codename')();
* console.log(codename.generate(
* ['adjectives', 'crayons'],
* ['alliterative','random']
* ));
*
* @id generate
* @param {Array} filterNames - a list of filters (by name) to limit the words
* selected for the codename
* @param {Array} listNames - a list of the wordlists to use when generating
* the codename
* @group exports
* @return {String}
*/
exports.generate = function (filterNames, listNames) {
var unknownFilters = _.difference(filterNames, this.filters()),
unknownLists = _.difference(listNames, this.lists());
if (unknownFilters.length) {
return referenceError('filter', unknownFilters.join(', '));
}
else if (unknownLists.length) {
return referenceError('list', unknownLists.join(', '));
}
return generate(_.zipObject(listNames, listNames.map(getList)), filterNames.map(getFilter));
};
/**
* A list of available wordlists
*
* @id lists
* @group exports
* @return {Array}
*/
exports.lists = _.partial(_.keys, lists);
return exports;
};