Skip to content

Instantly share code, notes, and snippets.

Created July 7, 2014 21:05
Show Gist options
  • Save anonymous/99cbf1cbd94ee939b5d5 to your computer and use it in GitHub Desktop.
Save anonymous/99cbf1cbd94ee939b5d5 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<link href="http://code.jquery.com/qunit/qunit-git.css" rel="stylesheet" type="text/css" />
<script src="http://code.jquery.com/qunit/qunit-git.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="http://code.jquery.com/qunit/qunit-git.js"></script>
</body>
</html>
function isNested(content, regex) {
var match = regex.exec(content);
if (match) {
return /%C(.+?):(.+?)%CR/g.exec(match[2]) ? true : false;
}
return false;
}
function splitter(string) {
var regex = /%C(.+?):([\s\S]+?)%CR(?!.*%CR)/g;
var tailRegex = /%CR(.+?)$/;
function matcher(regex, content, parts) {
content.replace(regex, function() {
var content = arguments[2];
var color = arguments[1];
var obj = {
color: arguments[1],
content: content
};
var tail = null;
if(content.match(tailRegex)) {
content.replace(tailRegex, function() {
var content = arguments[1];
match = tailRegex.exec(content);
if (null !== match) {
content = match[1];
}
tail = {
color: obj.color,
content: content
};
});
}
parts.push(obj);
match = /^(.+?)%C[a-z]/.exec(content);
if (null !== match) {
obj.content = match[1];
} else {
obj.content = content;
}
if (content.match(regex)) {
parts = matcher(regex, content, parts);
}
if(tail !== null) {
parts.push(tail);
}
});
return parts;
}
if (isNested(string, regex)) {
parts = matcher(regex, string, []);
} else {
parts = [];
string.replace(/%C(.+?):(.+?)%CR/g, function() {
parts.push({
color: arguments[1],
content: arguments[2]
});
});
}
return parts;
}
///////////////////////////////////////////
/** ------------- TESTS --------------- **/
///////////////////////////////////////////
QUnit.test("Single Color", function(assert) {
var string = '%Cgreen:Shane is ace %CR';
var expected = [
{
color: "green",
content: "Shane is ace "
}
];
var actual = splitter(string);
assert.deepEqual(actual, expected);
});
QUnit.test("Nested Colors", function(assert) {
var nestedString = '%Cgreen:Shane %Cred:Osbourne%CR is ace %CR';
var expected = [
{
color: "green",
content: "Shane "
},
{
color: "red",
content: "Osbourne"
},
{
color: "green",
content: " is ace "
}
];
var actual = splitter(nestedString);
assert.deepEqual(actual, expected);
});
QUnit.test("Triple Nested Colors WTF?!", function(assert) {
var nestedString = '%Cgreen:Shane %Cred:Osbourne%Corange: Aydin is the king!%CR%CR is ace %CR';
var expected = [
{
color: "green",
content: "Shane "
},
{
color: "red",
content: "Osbourne"
},
{
color: "orange",
content: " Aydin is the king!"
},
{
color: "green",
content: " is ace "
}
];
var actual = splitter(nestedString);
assert.deepEqual(actual, expected);
});
QUnit.test("Simple colors", function(assert) {
var nestedString = '%Cgreen:Shane %CR%Cred:Osbourne%CR';
var expected = [
{
color: "green",
content: "Shane "
},
{
color: "red",
content: "Osbourne"
}
];
var actual = splitter(nestedString);
assert.deepEqual(actual, expected);
});
QUnit.test("isNested with non nested string", function(assert) {
var nonNestedString = '%Cgreen:Shane %CR%Cred:Osbourne%CR';
var regex = /%C(.+?):([\s\S]+?)%CR(?!.*%CR)/g;
var actual = isNested(nonNestedString, regex);
assert.strictEqual(actual, false);
});
QUnit.test("isNested with nested string", function(assert) {
var nestedString = '%Cgreen:Shane %Cred:Osbourne%Corange: Aydin is the king!%CR%CR is ace %CR';
var regex = /%C(.+?):([\s\S]+?)%CR(?!.*%CR)/g;
var actual = isNested(nestedString, regex);
assert.strictEqual(actual, true);
});
QUnit.test("isNested with string with no colours", function(assert) {
var nestedString = 'No gay colours here';
var regex = /%C(.+?):([\s\S]+?)%CR(?!.*%CR)/g;
var actual = isNested(nestedString, regex);
assert.strictEqual(actual, false);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment