This repository has been archived by the owner on Jan 14, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bin for generating random phrases without duplicates.
- Loading branch information
1 parent
a358762
commit 6031fb6
Showing
4 changed files
with
79 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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(); | |||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters