enigma-bbs/core/string_util.js

148 lines
3.0 KiB
JavaScript
Raw Normal View History

2014-10-18 18:45:24 +00:00
/* jslint node: true */
'use strict';
2014-10-20 03:06:39 +00:00
var miscUtil = require('./misc_util.js');
2014-10-18 18:45:24 +00:00
exports.stylizeString = stylizeString;
2014-10-20 03:06:39 +00:00
exports.pad = pad;
2014-10-18 18:45:24 +00:00
// :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 = '';
2014-10-20 05:30:44 +00:00
switch(style) {
// None/normal
case 'normal' :
case 'N' : return s;
2014-10-18 18:45:24 +00:00
// UPPERCASE
2014-10-20 03:06:39 +00:00
case 'upper' :
2014-10-18 18:45:24 +00:00
case 'U' : return s.toUpperCase();
// lowercase
2014-10-20 03:06:39 +00:00
case 'lower' :
2014-10-18 18:45:24 +00:00
case 'l' : return s.toLowerCase();
2014-10-20 05:30:44 +00:00
// Title Case
case 'title' :
case 'T' :
2014-10-18 18:45:24 +00:00
return s.replace(/\w\S*/g, function onProperCaseChar(t) {
return t.charAt(0).toUpperCase() + t.substr(1).toLowerCase();
});
// fIRST lOWER
2014-10-20 03:06:39 +00:00
case 'first lower' :
2014-10-18 18:45:24 +00:00
case 'f' :
return s.replace(/\w\S*/g, function onFirstLowerChar(t) {
return t.charAt(0).toLowerCase() + t.substr(1).toUpperCase();
});
// SMaLL VoWeLS
2014-10-20 03:06:39 +00:00
case 'small vowels' :
2014-10-18 18:45:24 +00:00
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
2014-10-20 03:06:39 +00:00
case 'big vowels' :
2014-10-18 18:45:24 +00:00
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
2014-10-20 03:06:39 +00:00
case 'small i' :
2015-04-28 23:21:24 +00:00
case 'i' : return s.toUpperCase().replace(/I/g, 'i');
2014-10-18 18:45:24 +00:00
// mIxeD CaSE (random upper/lower)
2014-10-20 03:06:39 +00:00
case 'mixed' :
2014-10-18 18:45:24 +00:00
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
2014-10-20 03:06:39 +00:00
case 'l33t' :
2014-10-18 18:45:24 +00:00
case '3' :
for(i = 0; i < len; ++i) {
c = SIMPLE_ELITE_MAP[s[i].toLowerCase()];
stylized += c || s[i];
}
return stylized;
}
2014-10-20 03:06:39 +00:00
return s;
}
// Based on http://www.webtoolkit.info/
function pad(s, len, padChar, dir, stringSGR, padSGR) {
len = miscUtil.valueWithDefault(len, 0);
padChar = miscUtil.valueWithDefault(padChar, ' ');
dir = miscUtil.valueWithDefault(dir, 'right');
stringSGR = miscUtil.valueWithDefault(stringSGR, '');
padSGR = miscUtil.valueWithDefault(padSGR, '');
2014-10-20 03:06:39 +00:00
var padlen = len >= s.length ? len - s.length : 0;
2014-10-20 03:06:39 +00:00
switch(dir) {
case 'L' :
2014-10-20 03:06:39 +00:00
case 'left' :
s = padSGR + new Array(padlen).join(padChar) + stringSGR + s;
2014-10-20 03:06:39 +00:00
break;
case 'C' :
case 'center' :
2014-10-20 03:06:39 +00:00
case 'both' :
var right = Math.ceil(padlen / 2);
2014-10-20 03:06:39 +00:00
var left = padlen - right;
s = padSGR + new Array(left + 1).join(padChar) + stringSGR + s + padSGR + new Array(right + 1).join(padChar);
2014-10-20 03:06:39 +00:00
break;
case 'R' :
2014-10-20 03:06:39 +00:00
case 'right' :
s = stringSGR + s + padSGR + new Array(padlen).join(padChar);
2014-10-20 03:06:39 +00:00
break;
default : break;
}
return stringSGR + s;
}