views:

249

answers:

9

As I'm writing JavaScript I'm always missing some fairly basic language features that JavaScript just don't have. So is there any library that would bring such features as trim, sprintf, str.endwith and etc. functions to JavaScript ?

I just have written those functions too many times and I'm also tired of copy/pasting them from my old code. It would be nice to have some library which has those implemented and tested in one place.

Note that I'm not talking about Ajax/DOM-libraries like jQuery or Dojo and such. I know that those libraries bring some of the features that I'm talking here, but not all. And I would like to have also an environment independent library so that same library could be used with server side JavaScript .

Best library so far that I've found is php.js, but I don't like how it is polluting the global namespace. I'm also not too fond of how PHP-functions are named.

EDIT

I'm settling with Underscore.js as found out that it is very easy to extend. So I extended it with my own set of string functions. Check it out here:

http://esa-matti.suuronen.org/projects/underscore.strings/

+2  A: 

I'm not aware of any libraries that provide such functions other than the popular AJAX/DOM libraries. Why not hand pick the functions you need from PHP.js and add them to your own namespace? You could even rename them to whatever you like.

Andy E
A: 

Javascript substring will help you... if you dont like that way too, i'll recommend regular expressions...

Most of the time, when i need that basics functions in a very simple page, i do regex my bestfriend...

Garis Suero
+9  A: 

Have a look at Underscore.


On the other hand, what you want seems simple enough:

function endsWith(str, end) {
    return String(str).lastIndexOf(end) === str.length - end.length;
}

function trim(str) {
    return String(str).replace(/^\s\s*|\s\s*$/g, '');
} // btw, should really be checking for String.prototype.trim

// ... and google "JavaScript sprintf" for a sprintf implementation
J-P
+1 for this, I'd never heard of it until now but it looks pretty cool.
Andy E
Underscore would be the library I'm looking for if it would include some string manipulation helpers. Or maybe I could use it with some string specific library...
Epeli
The newer versions of Opera, Firefox and Chrome all have a string prototype trim method.
kennebec
Well, this is kinda cool library. I created string extensions for it http://bitbucket.org/epeli/underscore.strings/
Epeli
Melmacian: Nice work on the string library. If you want to write up a brief documentation page for it, I'd be glad to link to that from the main Underscore.js page...
jashkenas
+4  A: 

You may want to check out the Google Closure Library. It provides the following packages:

array (1)
asserts (1)
async (3)
base.js
color (3)
crypt (5)
cssom (2)
datasource (8)
date (4)
debug (16)
demos (6)
deps.js
disposable (1)
dom (28)
editor (15)
events (18)
format (3)
functions (1)
fx (12)
gears (14)
graphics (25)
history (1)
i18n (15)
iter (1)
json (1)
locale (16)
math (15)
memoize (1)
module (10)
net (29)
object (1)
positioning (9)
proto (2)
proto2 (10)
pubsub (1)
reflect (1)
spell (1)
string (3)
structs (18)
style (2)
testing (37)
timer (1)
ui (133)
uri (2)
useragent (9)

The Closure Library is open source, and Google should be using it in Gmail, Maps, Docs, Sites, Books, Reader, Blogger, Calendar and Picasa.

You may want to check out the Array and String packages to get a quick first impression.

Daniel Vassallo
A: 

There's a list in Server-side_JavaScript

+3  A: 

You might want to check out MooTools. It is a very modular library with a focus on enhancing JavaScript code, not just the browser-specific JavaScript environment (DOM, AJAX, etc.).

Lucas
Indeed, on the download screen you can select just the String and Array modules, for instance.
seanmonstar
A: 

I agree with Garis. Here is a page that shows the JS String Object functions.

http://www.w3schools.com/jsref/jsref_obj_string.asp

Using the above info and such you can write your own functions using the ones above: for example. If you want to trim a string you would do something like this to allow a certain amount of characters (this one adds elipsis, you can remove that of course):

String.prototype.trim= function(num){
   var str = this;
   if (!num) {
      num = 20;
   }
   if (str.length > num) {
      str = str.slice(0, num);
      str += '...';
   } else {
      str = this + '';
   }
   return str;

}
alert('this string needs to be shorter'.trim(10));

Output would be: this strin...

Dale
Sorry, the above is my truncate method. There really is no need for trim in js, (correct me if I am wrong) but js doesn't allow for whitespace at the beginning or end of string.
Dale
However, \b matches a character at the beginning or end of a string and \s matches whitespace.
Dale
+1  A: 

You could check out Mootools Server.

It is a customized MooTools build without any component relative to browsers. Includes Class, Core and Native Extensions. It's specifically made for server-side environments such as v8cgi, Rhino or SpiderMonkey.

Don't know if it suits your purpose, but it is one way to go.

mqchen
A: 

HTML5 javascript libraries

Comptrol