Created
June 17, 2013 02:08
-
-
Save breckenedge/5794259 to your computer and use it in GitHub Desktop.
Completely boring text-concat in Javascript
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>SpreadsheetUtil.rollup()</title> | |
<style> | |
body, textarea { | |
font-family: 'Lucida Console', 'Consolas', courier; | |
color: #7f7; | |
} | |
body { | |
margin: 0; | |
padding: 10px 1%; | |
background: #333; | |
} | |
textarea { | |
width: 96%; | |
border: none; | |
padding: 10px 2%; | |
margin: 0; | |
background-color: rgba(0,0,0,0.5); | |
border-radius: 10px; | |
line-height: 12px; | |
font-size: 8px; | |
} | |
textarea::selection { background: #7f7; color: #222; } | |
textarea::-moz-selection { background: #7f7; color: #222; } | |
textarea::-webkit-selection { background: #7f7; color: #222; } | |
</style> | |
</head> | |
<body> | |
<div style="float:left;width:49.5%"> | |
<textarea id="input" onkeyup="SpreadsheetUtil.rollup();" rows="40" placeholder="Paste text here"></textarea> | |
</div> | |
<div style="float:left;width:49.5%;margin-left:1%"> | |
<textarea id="output" rows="40" placeholder="Output appears here"></textarea> | |
</div> | |
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js"></script> | |
<script type="text/javascript"> | |
(function() { | |
var SpreadsheetUtil = {}; | |
SpreadsheetUtil.rollup = function () { | |
var combinatoryTrailingCharacters = ["-", "/"]; | |
var outputElement = document.getElementById("output"); | |
outputElement.value = ""; | |
var inputLines = document.getElementById("input").value.split(/\r\n|\r|\n/g); | |
var outputRows = []; | |
var outputRowIndex = 0; | |
for(var inputLineIndex=0; inputLineIndex < inputLines.length; inputLineIndex++) { | |
var inputColumns = inputLines[inputLineIndex].split(/\t/g); | |
if(inputColumns[0] != "") { | |
outputRowIndex += 1; | |
}; | |
if(outputRows[outputRowIndex] === undefined) { | |
outputRows[outputRowIndex] = []; | |
}; | |
for(var i = 0; i < inputColumns.length; i++) { | |
if(outputRows[outputRowIndex][i] === undefined) { | |
outputRows[outputRowIndex][i] = inputColumns[i].trim(); | |
} else { | |
var trailingCharacter = outputRows[outputRowIndex][i].charAt(outputRows[outputRowIndex][i].length - 1); | |
if(_.contains(combinatoryTrailingCharacters, trailingCharacter)) { | |
outputRows[outputRowIndex][i] = (outputRows[outputRowIndex][i] + inputColumns[i]).trim(); | |
} else { | |
outputRows[outputRowIndex][i] = (outputRows[outputRowIndex][i] + " " + inputColumns[i]).trim(); | |
} | |
} | |
} | |
} | |
_.each(outputRows, function(row) { | |
if(outputElement.value === "") { | |
outputElement.value = row.join("\t"); | |
} else { | |
outputElement.value = outputElement.value + "\n" + row.join("\t"); | |
} | |
}) | |
}; | |
window.SpreadsheetUtil = SpreadsheetUtil; | |
})(); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment