Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function changeCase(text) {
- var currentSymbol,
- openTag = '<',
- closeTag = '>',
- isTag = false,
- currentTag = '',
- result = '',
- tags = [];
- for (var i = 0, l = text.length; i < l; i += 1) {
- currentSymbol = text[i];
- if (currentSymbol === openTag) {
- isTag = true;
- continue;
- }
- if (currentSymbol === closeTag) {
- isTag = false;
- continue;
- }
- if (isTag) {
- switch (currentSymbol) {
- case 'u':
- currentTag = '<upcase>';
- tags.push(currentTag);
- i += currentTag.length - 2;
- break;
- case 'm':
- currentTag = '<mixcase>';
- tags.push(currentTag);
- i += currentTag.length - 2;
- break;
- case 'l':
- currentTag = '<lowcase>';
- tags.push(currentTag);
- i += currentTag.length - 2;
- break;
- case '/':
- tags.pop();
- i += currentTag.length - 2;
- if (tags.length > 0) {
- currentTag = tags[tags.length - 1];
- } else {
- currentTag = '';
- }
- break;
- }
- isTag = false;
- continue;
- }
- switch (currentTag) {
- case '<upcase>':
- result += currentSymbol.toUpperCase();
- break;
- case '<mixcase>':
- result += Math.random() < 0.5 ? currentSymbol.toUpperCase() : currentSymbol.toLowerCase();
- break;
- case '<lowcase>':
- result += currentSymbol.toLowerCase();
- break;
- default:
- result += currentSymbol;
- break;
- }
- }
- return result;
- }
- var text = 'We are <upcase><mixcase><lowcase></lowcase>living</mixcase> in </upcase> a <upcase>yellow submarine</upcase>. We <mixcase>don\'t</mixcase> have <lowcase>anything</lowcase> else.';
- console.log(changeCase(text));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement