Created
February 18, 2019 18:40
-
-
Save wheresjames/e2a9b32c376fcbdc7f33e9566a759c3b to your computer and use it in GitHub Desktop.
Splits words and creates javascript / JSON friendly array
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 nodejs | |
//------------------------------------------------------------------------------------------------- | |
// Developer: Robert Umbehant | |
//------------------------------------------------------------------------------------------------- | |
// Splits the input file into words, filters the words, and outputs as a javascript / JSON friendly | |
// array of strings. | |
//------------------------------------------------------------------------------------------------- | |
var fs = require('fs'); | |
var path = require('path'); | |
// https://stackoverflow.com/questions/11386492/accessing-line-number-in-v8-javascript-chrome-node-js | |
Object.defineProperty(global, '__stack', | |
{ | |
get: function() | |
{ | |
var orig = Error.prepareStackTrace; | |
Error.prepareStackTrace = function(_, stack) { | |
return stack; | |
}; | |
var err = new Error; | |
Error.captureStackTrace(err, arguments.callee); | |
var stack = err.stack; | |
Error.prepareStackTrace = orig; | |
return stack; | |
} | |
}); | |
function Log() | |
{ | |
var st = __stack[1]; | |
console.log(st.getFunctionName() + "(" + st.getLineNumber() + "): " + Array.prototype.slice.call(arguments).toString()); | |
} | |
function main() | |
{ | |
// Ensure parameters | |
if (process.argv.length <= 2) | |
{ | |
Log("Usage: " + path.basename(__filename) + " <input-file> [max-line-length=100]"); | |
process.exit(-1); | |
} | |
// Verify root path | |
var fi = process.argv[2]; | |
if (!fs.existsSync(fi)) | |
{ | |
Log("Input file doesn't exist : " + fi); | |
process.exit(-1); | |
} | |
// Get max line length for output | |
var max = parseInt(process.argv[3]); | |
if (!Number.isInteger(max) || 0 >= max) | |
max = 100; | |
// Read file data | |
var data = String(fs.readFileSync(fi)); | |
if (!data.length) | |
{ | |
Log("File appears to be empty : " + fi); | |
process.exit(-1); | |
} | |
// Split words | |
var words = data.split(/[^a-zA-Z]+/); | |
// Throw out invalid words | |
words = words.filter( v => 3 <= v.length ); | |
// Convert to lower case | |
words = words.map(v => v.toLowerCase()); | |
// Eliminate duplicates and sort words | |
words = [...new Set(words)].sort(); | |
// Convert to code | |
var out = ""; | |
var cur = max; | |
for (w in words) | |
{ | |
if (0 < out.length) | |
out += ", " | |
if (0 > cur) | |
{ | |
out += "\r\n"; | |
cur = max; | |
} | |
out += "\"" + words[w] + "\"" | |
cur -= words[w].length + 4; | |
} | |
console.log(out); | |
} | |
return main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment