Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

bin for generating random phrases without duplicates.

  • Loading branch information...
commit 6031fb6c98a416e81e4fb991b809ed3068357e28 1 parent a358762
@brianloveswords brianloveswords authored
View
4 Makefile
@@ -8,10 +8,10 @@ lint:
@jshint *.js lib/*.js models/*.js routes/*.js
test:
- @node test && node_modules/.bin/tap tests/*.test.js
+ @node tests && node_modules/.bin/tap tests/*.test.js
verbose-test:
- @node test/*.test.js
+ @node tests/*.test.js
heroku:
@git push heroku master && heroku open
View
68 bin/randwiches.js
@@ -0,0 +1,68 @@
+#!/usr/bin/env node
+const argv = require('optimist').argv;
+const phrases = require('../lib/phrases');
+const iteratorStream = require('iterator-stream');
+const fs = require('fs');
+const pathutil = require('path');
+const util = require('util');
+
+function log() {
+ const args = [].slice.call(arguments);
+ const str = util.format.apply(util, args);
+ process.stderr.write(str + '\n');
+}
+
+function usage(err) {
+ if (err) log('\nError:', err, '\n');
+ log('%s -n <count> [files-to-exclude, ...]', argv.$0);
+ log(' Prints a bunch of random phrases to stdout.');
+ log(' If given files, will avoid any phrases that appear in those files');
+ process.exit(1);
+}
+
+function fileToArray(file) {
+ return (fs.readFileSync(pathutil.join(process.cwd(), file))
+ .toString()
+ .trim()
+ .split('\n'));
+}
+
+function inArrayFilter(array) {
+ return function (v) {
+ if (array.indexOf(v) > -1) log('DUPLICATE:', v);
+ return array.indexOf(v) == -1;
+ };
+}
+
+function main() {
+ const count = argv.n || argv.count
+ const excludeFiles = argv._;
+ var exclude = [];
+ if (!count)
+ return usage('requires -n');
+
+ log('generating', count, 'phrases');
+
+ var filter = function (x) { return true };
+
+ if (excludeFiles.length) {
+ log('excluding', excludeFiles);
+ exclude = (excludeFiles
+ .map(fileToArray)
+ .reduce(function (exclude, phrases) {
+ return exclude.concat(phrases);
+ }, exclude));
+ }
+
+ iteratorStream(phrases.iterator, {
+ transform: function (v) { return v.join('-') },
+ filter: inArrayFilter(exclude),
+ take: 1000,
+ separator: '\n',
+ method: 'random'
+ }).pipe(process.stdout);
+
+}
+
+if (!module.parent) main();
+
View
10 lib/phrases/index.js
@@ -12,19 +12,21 @@ const fs = require('fs');
const ADVERBS = fileToArray('adverbs.txt');
const ADJECTIVES = fileToArray('adjectives.txt');
const NOUNS = fileToArray('nouns.txt');
-const generator = sandwich(ADVERBS, ADJECTIVES, NOUNS);
+const iterator = sandwich(ADVERBS, ADJECTIVES, NOUNS);
-module.exports = function phrases(count) {
+function phrases(count) {
const matches = {}
const results = []
var length = 0;
var word;
while (length < count) {
- word = generator.random().join('-');
+ word = iterator.random().join('-');
if (!matches[word]) {
matches[word] = results.push(word);
length++;
}
}
return results;
-};
+};
+phrases.iterator = iterator;
+module.exports = phrases;
View
4 package.json
@@ -19,7 +19,9 @@
"up": "0.2.2",
"connect-flash": "0.1.0",
"jwt-simple": "0.1.0",
- "sandwich": "~0.1.1"
+ "sandwich": "~0.1.1",
+ "optimist": "~0.3.5",
+ "iterator-stream": "~0.4.0"
},
"devDependencies": {
"tap": "~0.3.1"
Please sign in to comment.
Something went wrong with that request. Please try again.