Merge pull request #1855 from jakebellacera/adding_replace_function

Adding replace function
This commit is contained in:
Luke Page
2014-02-17 22:32:27 +00:00
3 changed files with 13 additions and 0 deletions

View File

@@ -218,6 +218,13 @@ tree.functions = {
escape: function (str) {
return new(tree.Anonymous)(encodeURI(str.value).replace(/=/g, "%3D").replace(/:/g, "%3A").replace(/#/g, "%23").replace(/;/g, "%3B").replace(/\(/g, "%28").replace(/\)/g, "%29"));
},
replace: function (subject, pattern, replacement, flags) {
var str = subject.value;
str = str.replace(new RegExp(pattern.value, flags ? flags.value : ""), replacement.value);
return new(tree.Quoted)('"' + str + '"', str);
},
'%': function (quoted /* arg, arg, ...*/) {
var args = Array.prototype.slice.call(arguments, 1),
str = quoted.value;

View File

@@ -45,6 +45,9 @@
contrast-dark-thresh-per: #eeeeee;
contrast-high-thresh-per: #eeeeee;
contrast-low-thresh-per: #111111;
replace: "Hello, World!";
replace-captured: "This is a new string.";
replace-with-flags: "2 + 2 = 4";
format: "rgb(32, 128, 64)";
format-string: "hello world";
format-multiple: "hello earth 2";

View File

@@ -49,6 +49,9 @@
contrast-dark-thresh-per: contrast(#000, #111111, #eeeeee, 50%);
contrast-high-thresh-per: contrast(#555, #111111, #eeeeee, 60%);
contrast-low-thresh-per: contrast(#555, #111111, #eeeeee, 10%);
replace: replace("Hello, Mars.", "Mars\.", "World!");
replace-captured: replace("This is a string.", "(string)\.$", "new $1.");
replace-with-flags: replace("One + one = 4", "one", "2", "gi");
format: %("rgb(%d, %d, %d)", @r, 128, 64);
format-string: %("hello %s", "world");
format-multiple: %("hello %s %d", "earth", 2);