From 133f073acdbb93fd6db0e04323b247896696ab17 Mon Sep 17 00:00:00 2001 From: s2 Date: Fri, 17 Apr 2015 22:28:45 +0200 Subject: [PATCH] initial checkin --- .gitignore | 2 ++ quote.js | 45 +++++++++++++++++++++++++++++++++++++++ tests.js | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 .gitignore create mode 100644 quote.js create mode 100644 tests.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..650751b --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/.settings +/.project diff --git a/quote.js b/quote.js new file mode 100644 index 0000000..f5d57da --- /dev/null +++ b/quote.js @@ -0,0 +1,45 @@ +(function() { + + var validquotechars = ['>']; + + + var quoteline = function(line, maxlength, qc) { + if (line.length > maxlength) { + var lastspace = line.lastIndexOf(' ', maxlength); + if (lastspace != -1) { + line = line.substr(0, lastspace) + ' \n' + quoteline(line.substr(lastspace + 1), maxlength, qc); + } + } + + //this must be after the code before, because this inserts a space in the string + return qc + ' ' + line; + }; + + + var quote = function(text, length, quotechar) { + if (typeof(quotechar) === 'undefined') { + quotechar = '>'; + } + if (typeof(length) === 'undefined') { + length = 75; + } + + var quoted = text.split('\n').map(function(v, i) { + //if this is already quoted text, return it as is + if (validquotechars.indexOf(v.charAt(0)) > -1) { + return v; + } + + return quoteline(v, length, quotechar); + }).join('\n'); + + return quoted; + }; + + if (typeof(window) === 'undefined') { + module.exports = quote; + } else { + window.quote = quote; + } + +})(); diff --git a/tests.js b/tests.js new file mode 100644 index 0000000..79b03ad --- /dev/null +++ b/tests.js @@ -0,0 +1,62 @@ +var quote = require('./quote.js'); + +var test = function(name, what, ref, c) { + process.stdout.write(name); + + if (typeof(c) !== 'boolean') { + c = true; + } + + if (typeof(what) === 'function') { + what = what(); + } + if (typeof(ref) === 'function') { + ref = ref(); + } + + if ((what === ref) === c) { + process.stdout.write(' ✓'); + } else { + process.stdout.write(' ⛝\n'); + process.stdout.write(' ----> expected | ' + ref + '\n'); + process.stdout.write(' ----> got | ' + what + '\n'); + } + process.stdout.write('\n'); +}; + + +test('simple short text', + quote('simple short text'), + '> simple short text' +); + +test('an already quoted line', + quote('> simple short text'), + '> simple short text' +); + +test('a line longer than 75 chars with no spaces', + quote('12345678901234567890123456789012345678901234567890123456789012345678901234567890'), + '> 12345678901234567890123456789012345678901234567890123456789012345678901234567890' +); + +test('a line longer than 75 chars with spaces', + quote('1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890', 75), + '> 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 \n> 1234567890 1234567890' +); + +test('three lines with a hard break', + quote('1234567890\n1234567890\n1234567890'), + '> 1234567890\n> 1234567890\n> 1234567890' +); + +test('three lines, one long line, two with a hard break in between', + quote('1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890\n' + + '1234567890 1234567890 1234567890 1234567890\n' + + '1234567890 1234567890 1234567890 1234567890\n'), + '> 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 \n' + + '> 1234567890 1234567890\n' + + '> 1234567890 1234567890 1234567890 1234567890\n' + + '> 1234567890 1234567890 1234567890 1234567890\n' + + '> ' +);