Created
February 19, 2015 17:45
-
-
Save arlando/de532dad5eb5c9310d57 to your computer and use it in GitHub Desktop.
split.js
This file contains hidden or 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
#!/usr/bin/env node | |
var split = require('split'); | |
var crypto = require('crypto'); | |
var md5 = require('MD5'); | |
var Transform = require('stream').Transform; | |
var util = require('util'); | |
var fs = require('fs'); | |
var wstream = fs.createWriteStream('filtered3.tsv'); | |
function Encrypt() {} | |
Encrypt.prototype = { | |
UUID: function () { | |
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { | |
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8); | |
return v.toString(16); | |
}); | |
}, | |
SHA1: function () { | |
return crypto | |
.randomBytes(20) | |
.toString('hex'); | |
}, | |
MD5: function () { | |
return md5(this.SHA1()) | |
}, | |
randomEncryption: function () { | |
var random = Math.random(); | |
if (random < .8) return this.UUID(); | |
if (random < .9) return this.SHA1(); | |
else return this.MD5(); | |
} | |
}; | |
encrypt = new Encrypt(); | |
util.inherits(EncryptStream, Transform); | |
function EncryptStream () { | |
Transform.call(this, {'objectMode': true}); | |
} | |
EncryptStream.prototype._transform = function (line, encoding, processed) { | |
var split_string_array = line.split(/\t/); | |
split_string_array[0] = encrypt.randomEncryption(); | |
var string_to_write = split_string_array.join('\t'); | |
this.push(string_to_write + '\n'); | |
processed(); | |
} | |
process.stdin.setEncoding('utf8'); | |
process.stdin | |
.pipe(split()) | |
.pipe(new EncryptStream()) | |
.pipe(wstream); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cat x | ./split.js --> filtered3.js