Skip to content

Commit

Permalink
processing-js#19, rewrote trim(), added tests in examples/seneca/trim
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Hodgin committed Feb 6, 2010
1 parent 051a398 commit e7f258e
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 1 deletion.
33 changes: 33 additions & 0 deletions examples/seneca/trim/trim.pjs
@@ -0,0 +1,33 @@
// Trim example
size(500,220, P3D);
background(20);
stroke(128, 128, 128);
fill(64);
rect(1, 1, width-3, height-3);

String s1 = "normal string";
String s2 = " extra leading spaces";
String s3 = "extra trailing spaces ";
String s4 = " both leading and trailing ";
String[] a = { " inconsistent ", " spacing", "ok", " tab at the end "};
String[] a2 = trim(a);

PFont font;
font = loadFont("Arial");
textFont(font, 14);
fill(32, 255, 0);
text("Strings", 5, 20);
text("String Arrays", 5, 120);
fill(255, 255, 255);
text("s1: |" + s1 + "|", 15, 40);
text("after: |" + trim(s1) + "|", 240, 40);
text("s2: |" + s2 + "|", 15, 60);
text("after: |" + trim(s2) + "|", 240, 60);
text("s3: |" + s3 + "|", 15, 80);
text("after: |" + trim(s3) + "|", 240, 80);
text("s4: |" + s4 + "|", 15, 100);
text("after: |" + trim(s4) + "|", 240, 100);
for(int i=0; i<a2.length; i++){
text("a[" + i + "]: |" + a[i] + "|", 15, 140+(i*20));
text("after: |" + a2[i] + "|", 240, 140+(i*20));
}
Binary file added examples/seneca/trim/trim.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
122 changes: 122 additions & 0 deletions examples/seneca/trim/trimtest.htm
@@ -0,0 +1,122 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Processing.js test of trim()</title>
<script type="text/javascript" src="../../../processing.js"></script>
<script type="text/javascript" src="../../../scripts/shCore.js"></script>
<script type="text/javascript" src="../../../scripts/shBrushProcessing.js"></script>
<link type="text/css" rel="stylesheet" href="../../../styles/shCore.css" />
<link type="text/css" rel="stylesheet" href="../../../styles/shThemeDefault.css" />
<script type="text/javascript">
SyntaxHighlighter.config.clipboardSwf = '../../../scripts/clipboard.swf';
SyntaxHighlighter.all();
</script>

<style type="text/css">
html, body
{
background: #EEE;
font-family: Arial;
font-size: 13px;
margin: 0;
padding: 0;
}
h1
{
background: #385C85;
color: #FFF;
padding: 10px;
padding-left: 20px;
margin-top: 0px;
margin-bottom: 0px;
}
h1 a
{
color: #FFF;
}
h2
{
padding-left: 20px;
}
p
{
padding-left: 20px;
line-height: 1.3em;
}
p a
{
color: #385C85;
}
canvas
{
margin: 10px 0;
}
pre
{
margin-left: 20px;
margin-right: 20px;
padding: 5px;
background: #FFF;
border-top: 1px solid #333;
border-bottom: 1px solid #333;
}
div.ref-col
{
float: left;
width: 32%;
}
</style>
</head>
<body>
<h1>
<!-- name of your function and test description -->
trim() test</h1>
<h2>
This is a test of trim(string)</h2>
<p>
trim() removes whitespace characters from the beginning and end of a String. In addition to standard whitespace characters such as space, carriage return, and tab, this function also removes the Unicode "nbsp" character.</p>
<!-- include your processing file below -->
<canvas datasrc="trim.pjs" width="200" height="200" style="margin-left: 10px;"></canvas>
<img src="trim.png" alt=""/>
<br />
<b>Test written by <a href="http://dhodgin.wordpress.com/">Daniel Hodgin</a></b><br />
<h1>
Source Code:</h1>
<pre class="brush: processing;">
// Trim example
size(500,220, P3D);
background(20);
stroke(128, 128, 128);
fill(64);
rect(1, 1, width-3, height-3);

String s1 = "normal string";
String s2 = " extra leading spaces";
String s3 = "extra trailing spaces ";
String s4 = " both leading and trailing ";
String[] a = { " inconsistent ", " spacing", "ok" };
String[] a2 = trim(a);

PFont font;
font = loadFont("Arial");
textFont(font, 14);
fill(32, 255, 0);
text("Strings", 5, 20);
text("String Arrays", 5, 120);
fill(255, 255, 255);
text("s1: " + s1, 15, 40);
text("after: " + trim(s1), 240, 40);
text("s2: " + s2, 15, 60);
text("after: " + trim(s2), 240, 60);
text("s3: " + s3, 15, 80);
text("after: " + trim(s3), 240, 80);
text("s4: " + s4, 15, 100);
text("after: " + trim(s4), 240, 100);
for(int i=0; i&lt;a2.length; i++){
text("a[" + i + "]: " + a[i], 15, 140+(i*20));
text("after: " + a2[i], 240, 140+(i*20));
}
</pre>
<br />
</body>
</html>
16 changes: 15 additions & 1 deletion processing.js
Expand Up @@ -1628,7 +1628,21 @@
return key;
};


p.trim = function( str ) {
var newstr;
if (typeof str === "object") {
// if str is an array recursivly loop through each element
// and drill down into multiple arrays trimming whitespace
newstr = new Array(0);
for (var i = 0; i < str.length; i++) {
newstr[i] = p.trim(str[i]);
}
} else {
// if str is not an array then remove all whitespace, tabs, and returns
newstr = str.replace(/^\s*/,'').replace(/\s*$/,'').replace(/\r*$/,'');
}
return newstr;
};

////////////////////////////////////////////////////////////////////////////
// Math functions
Expand Down

0 comments on commit e7f258e

Please sign in to comment.