From e21aa13c330a30262e7389357e0ec7acce412367 Mon Sep 17 00:00:00 2001 From: yoorek Date: Fri, 27 Nov 2015 16:43:37 +0100 Subject: [PATCH] feat: strings solution --- app/strings.js | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 app/strings.js diff --git a/app/strings.js b/app/strings.js new file mode 100644 index 0000000..ace08d7 --- /dev/null +++ b/app/strings.js @@ -0,0 +1,41 @@ +exports = (typeof window === 'undefined') ? global : window; + +exports.stringsAnswers = { + + reduceString: function (str, amount) { + var matches; + + while (matches = /(\w)\1{2,}/g.exec(str)) { + str = str.replace(matches[0], matches[0].substr(0, amount)); + } + + return str; + }, + + wordWrap: function (str, cols) { + var words = str.split(' '), rows = []; + + words.reduce(function (row, current, index) { + if (row.length + current.length > cols) { + + if (row) { + rows.push(row); + } + + if (index === words.length - 1) { + rows.push(current); + } + + return current; + } else { + return row ? row + ' ' + current : current; + } + }); + + return rows.join('\n'); + }, + + reverseString: function (str) { + return str.split('').reverse().join(''); + } +};