evant
8 Aug 2007, 3:52 PM
Hi all, I made this for a project, thought someone might find it useful. It's not thoroughly tested, but it should come in handy:
For those not familiar, a string builder is an object used to concatenate strings.
Advantages of using this object:
1) Your code may appear cleaner (subject to debate ;))
var sb = new Ext.ux.StringBuilder();
sb.append('string1');
sb.append('string2');
Ext.get('foo').update(sb.toString()); //prints 'string1string2'
2) Supports strings, arrays and objects
var sb = new Ext.ux.StringBuilder('foo');
var other = new Ext.ux.StringBuilder();
other.append(sb);
other.append(['1', '2', '3']);
other.append('bar');
Ext.get('foo').update(other.toString()); //prints 'foo123bar'
3) Is optimized for particular browsers (Thanks Jack)
4) Supports chaining
var sb = new Ext.ux.StringBuilder();
sb.append('a').append('b').append('c').append(['1','2','3']);
Ext.get('foo').update(sb.toString()); //prints 'abc123'
Also, thanks to Animal for pointing out how to do the private functions. Cheers
Ext.namespace('Ext.ux');
Ext.ux.StringBuilder = function(start)
{
this.builder = '';
this.items = [];
this.append(start);
};
Ext.ux.StringBuilder.prototype = (function()
{
function appendString(s)
{
if (Ext.isIE)
this.items.push(s);
else
this.builder += s;
}
function appendArray(a)
{
Ext.each(a, function(el)
{
this.append(el);
}, this);
}
function appendObject(o)
{
if (o.toString)
appendString.call(this, o.toString());
}
return {
append: function(o)
{
if (o)
{
if (typeof o == 'string')
appendString.call(this, o);
else if (o instanceof Array)
appendArray.call(this, o);
else
appendObject.call(this, o);
}
return this;
},
toString: function()
{
return Ext.isIE ? this.items.join('') : this.builder;
},
clear: function()
{
this.items = [];
builder = '';
}
}
})();
Enjoy.
For those not familiar, a string builder is an object used to concatenate strings.
Advantages of using this object:
1) Your code may appear cleaner (subject to debate ;))
var sb = new Ext.ux.StringBuilder();
sb.append('string1');
sb.append('string2');
Ext.get('foo').update(sb.toString()); //prints 'string1string2'
2) Supports strings, arrays and objects
var sb = new Ext.ux.StringBuilder('foo');
var other = new Ext.ux.StringBuilder();
other.append(sb);
other.append(['1', '2', '3']);
other.append('bar');
Ext.get('foo').update(other.toString()); //prints 'foo123bar'
3) Is optimized for particular browsers (Thanks Jack)
4) Supports chaining
var sb = new Ext.ux.StringBuilder();
sb.append('a').append('b').append('c').append(['1','2','3']);
Ext.get('foo').update(sb.toString()); //prints 'abc123'
Also, thanks to Animal for pointing out how to do the private functions. Cheers
Ext.namespace('Ext.ux');
Ext.ux.StringBuilder = function(start)
{
this.builder = '';
this.items = [];
this.append(start);
};
Ext.ux.StringBuilder.prototype = (function()
{
function appendString(s)
{
if (Ext.isIE)
this.items.push(s);
else
this.builder += s;
}
function appendArray(a)
{
Ext.each(a, function(el)
{
this.append(el);
}, this);
}
function appendObject(o)
{
if (o.toString)
appendString.call(this, o.toString());
}
return {
append: function(o)
{
if (o)
{
if (typeof o == 'string')
appendString.call(this, o);
else if (o instanceof Array)
appendArray.call(this, o);
else
appendObject.call(this, o);
}
return this;
},
toString: function()
{
return Ext.isIE ? this.items.join('') : this.builder;
},
clear: function()
{
this.items = [];
builder = '';
}
}
})();
Enjoy.