From 9033ceeac6d9603909bb3f39c2c578f5287d2ff6 Mon Sep 17 00:00:00 2001 From: NuSkooler Date: Sat, 18 Oct 2014 12:45:24 -0600 Subject: [PATCH] * WIP on views --- core/string_util.js | 95 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 core/string_util.js diff --git a/core/string_util.js b/core/string_util.js new file mode 100644 index 00000000..5696ce65 --- /dev/null +++ b/core/string_util.js @@ -0,0 +1,95 @@ +/* jslint node: true */ +'use strict'; + +exports.stylizeString = stylizeString; + +// :TODO: create Unicode verison of this +var VOWELS = [ 'a', 'e', 'i', 'o', 'u' ]; + +VOWELS.forEach(function onVowel(v) { + VOWELS.push(v.toUpperCase()); +}); + +var SIMPLE_ELITE_MAP = { + 'a' : '4', + 'e' : '3', + 'i' : '1', + 'o' : '0', + 's' : '5', + 't' : '7' +}; + +function stylizeString(s, style) { + var len = s.length; + var c; + var i; + var stylized = ''; + + switch(style) { + // UPPERCASE + case 'U' : return s.toUpperCase(); + + // lowercase + case 'l' : return s.toLowerCase(); + + // Proper Case + case 'P' : + return s.replace(/\w\S*/g, function onProperCaseChar(t) { + return t.charAt(0).toUpperCase() + t.substr(1).toLowerCase(); + }); + + // fIRST lOWER + case 'f' : + return s.replace(/\w\S*/g, function onFirstLowerChar(t) { + return t.charAt(0).toLowerCase() + t.substr(1).toUpperCase(); + }); + + // SMaLL VoWeLS + case 'v' : + for(i = 0; i < len; ++i) { + c = s[i]; + if(-1 !== VOWELS.indexOf(c)) { + stylized += c.toLowerCase(); + } else { + stylized += c.toUpperCase(); + } + } + return stylized; + + // bIg vOwELS + case 'V' : + for(i = 0; i < len; ++i) { + c = s[i]; + if(-1 !== VOWELS.indexOf(c)) { + stylized += c.toUpperCase(); + } else { + stylized += c.toLowerCase(); + } + } + return stylized; + + // Small i's: DEMENTiA + case 'i' : return s.toUpperCase().replace('I', 'i'); + + // mIxeD CaSE (random upper/lower) + case 'M' : + for(i = 0; i < len; i++) { + if(Math.random() < 0.5) { + stylized += s[i].toUpperCase(); + } else { + stylized += s[i].toLowerCase(); + } + } + return stylized; + + // l337 5p34k + case '3' : + for(i = 0; i < len; ++i) { + c = SIMPLE_ELITE_MAP[s[i].toLowerCase()]; + stylized += c || s[i]; + } + return stylized; + } + + return s; +} \ No newline at end of file