Why doesn’t Mootools have a String.truncate() method?

23 views
Skip to first unread message

Michael Fuery

unread,
May 14, 2009, 7:51:47 PM5/14/09
to MooTools Users, e...@fuery.com
http://fuerysolutions.com/2009/05/14/why-doesnt-mootools-have-a-stringtruncate-method/

I need a quick way to truncate strings! I’m surprised that neither
MooTools’ String.js or String.Extras.js has such a method. They have
so many super-awesome functions, methods and extensions (I especially
love the Date.js extensions!!). Yeah, I really love mootools, and I
feel compelled to share with the community. So anyway, I wrote out a
simple, straightforward String.truncate() function.

Comments, suggestions, corrections and extensions are always welcomed!

<pre lang="javascript" line="1">
String.implement({
/**
* String.truncate(max, atChar, trail)
*
* ++All parameters are optional.
* @param max = (integer) maximum length of truncated string.
Defaults to 100 chars.
* @param atChar = (string) truncate at the last index of this
string. If not found, just truncates to max length.
* If null, does not search and truncates to max
length.
* @param trail = (string) what you want appended to the end of
the returned string
*
* @author Michael Fuery, Fuery Solutions, Inc. http://www.fuerysolutions.com/
*
* +Requires MooTools Core 1.2.2
*/
truncate:function(max, atChar, trail){
var s = this.trim();
if(s.length<1) return '';
if(!$defined(max)) var max = 100;
else max = max.toInt();
if(!$defined(atChar)) var atChar=' '; // break at space
else if(atChar == null) var atChar=false;
if(!$defined(trail)) var trail = '...';

if(s.length > max) {
var i=0;
if(atChar){
if((i = s.lastIndexOf(atChar)) != -1){
s = s.substring(0, i);
}else{
s = s.substring(0, max);
}
}

s += trail;
}

return s;
}
});
</pre>

csuwldcat

unread,
May 14, 2009, 8:33:56 PM5/14/09
to MooTools Users
Good call Michael! I have my own function (I bet many do) this is a
great addition!

- Daniel

On May 14, 4:51 pm, Michael Fuery <mfu...@gmail.com> wrote:
> http://fuerysolutions.com/2009/05/14/why-doesnt-mootools-have-a-strin...
>
> I need a quick way to truncate strings!  I’m surprised that neither
> MooTools’ String.js or String.Extras.js has such a method.  They have
> so many super-awesome functions, methods and extensions (I especially
> love the Date.js extensions!!).  Yeah, I really love mootools, and I
> feel compelled to share with the community.  So anyway, I wrote out a
> simple, straightforward String.truncate() function.
>
> Comments, suggestions, corrections and extensions are always welcomed!
>
> <pre lang="javascript" line="1">
> String.implement({
>     /**
>      * String.truncate(max, atChar, trail)
>      *
>      * ++All parameters are optional.
>      * @param max = (integer) maximum length of truncated string.
> Defaults to 100 chars.
>      * @param atChar = (string) truncate at the last index of this
> string. If not found, just truncates to max length.
>      *                 If null, does not search and truncates to max
> length.
>      * @param trail = (string) what you want appended to the end of
> the returned string
>      *
>      * @author Michael Fuery, Fuery Solutions, Inc.http://www.fuerysolutions.com/

csuwldcat

unread,
May 14, 2009, 8:35:33 PM5/14/09
to MooTools Users
Try to get the feature added, you can put in a ticket to Lighthouse
here for it: http://mootools.lighthouseapp.com/

;)
> I need a quick way to truncate strings!  I’m surprised that neither
> MooTools’ String.js or String.Extras.js has such a method.  They have
> so many super-awesome functions, methods and extensions (I especially
> love the Date.js extensions!!).  Yeah, I really love mootools, and I
> feel compelled to share with the community.  So anyway, I wrote out a
> simple, straightforward String.truncate() function.
>
> Comments, suggestions, corrections and extensions are always welcomed!
>
> <pre lang="javascript" line="1">
> String.implement({
>     /**
>      * String.truncate(max, atChar, trail)
>      *
>      * ++All parameters are optional.
>      * @param max = (integer) maximum length of truncated string.
> Defaults to 100 chars.
>      * @param atChar = (string) truncate at the last index of this
> string. If not found, just truncates to max length.
>      *                 If null, does not search and truncates to max
> length.
>      * @param trail = (string) what you want appended to the end of
> the returned string
>      *
>      * @author Michael Fuery, Fuery Solutions, Inc.http://www.fuerysolutions.com/

Michael Fuery

unread,
May 15, 2009, 1:01:45 PM5/15/09
to MooTools Users

nutron

unread,
May 15, 2009, 1:27:36 PM5/15/09
to mootool...@googlegroups.com
Thanks Michael. We'll consider this for inclusion in String.Extras.

On Fri, May 15, 2009 at 10:02 AM, Michael Fuery (via Nabble) <ml-user%2B223748-717264800@...> wrote:

The MooTools Tutorial: www.mootorial.com Clientcide: www.clientcide.com


View this message in context: Re: [Moo] Re: Why doesn’t Mootools have a String.truncate() method?
Sent from the MooTools Users mailing list archive at Nabble.com.

Olmo

unread,
May 16, 2009, 6:43:42 PM5/16/09
to MooTools Users
Got a few examples of the usage?

On May 15, 10:27 am, nutron <anut...@gmail.com> wrote:
> Thanks Michael. We'll consider this for inclusion in String.Extras.
>
> On Fri, May 15, 2009 at 10:02 AM, Michael Fuery (via Nabble) <
> ml-user+223748-717264...@n2.nabble.com<ml-user%2B223748-717264...@n2.nabble.com>
>
>
>
> > wrote:
>
> > Good idea!  Done!
> >https://mootools.lighthouseapp.com/projects/2706-mootools/tickets/670
>
> > On May 14, 5:35 pm, csuwldcat <daniel...@...<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=2908871&i=0>>
> > wrote:
> > > Try to get the feature added, you can put in a ticket to Lighthouse
> > > here for it:http://mootools.lighthouseapp.com/
>
> > > ;)
>
> > > On May 14, 4:51 pm, Michael Fuery <mfu...@...<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=2908871&i=1>>
> > ------------------------------
> >  View message @
> >http://n2.nabble.com/-Moo--Why-doesn%E2%80%99t-Mootools-have-a-String...
> > To start a new topic under MooTools Users, email
> > ml-node+660466-1583815...@n2.nabble.com<ml-node%2B660466-1583815...@n2.nabble.com>
> > To unsubscribe from MooTools Users, click here< (link removed) >.
>
> -----
> The MooTools Tutorial:  http://www.mootorial.comwww.mootorial.com
> Clientcide:  http://www.clientcide.comwww.clientcide.com
> --
> View this message in context:http://n2.nabble.com/-Moo--Why-doesn%E2%80%99t-Mootools-have-a-String...

Oskar Krawczyk

unread,
May 17, 2009, 3:36:00 AM5/17/09
to mootool...@googlegroups.com
Simple as:

var elText = el.get('text');
el.set('html', elText.truncate(10, ' ', '...'));

Michael Fuery

unread,
May 17, 2009, 3:47:51 PM5/17/09
to MooTools Users
Oops, I forgot a line in the code. Below "if(s.length > max)
{ var i=0; " insert this line:

s = s.slice(0,max);

Usage does not require any parameters at all if you are fine with the
default cut off of 100 chars with a trailing elipsis (...) and
truncate at space:

var str = "MooTools is an open source, lightweight, modular object-
oriented programming JavaScript web application framework released
under the MIT License. The goal of the software is to provide a means
for intermediate to advanced web developers to write cross-browser
JavaScript in an elegant, flexible, and efficient fashion. ";
alert(str.truncate());

// outputs: "MooTools is an open source, lightweight, modular object-
oriented programming JavaScript web..."
Reply all
Reply to author
Forward
0 new messages