admin管理员组

文章数量:1185719

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:

.string

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:

https://github.com/epeli/underscore.string

Share Improve this question edited Oct 5, 2011 at 17:56 esamatti asked Jun 8, 2010 at 17:15 esamattiesamatti 18.9k11 gold badges81 silver badges83 bronze badges 0
Add a comment  | 

8 Answers 8

Reset to default 11

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

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.

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.).

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.

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.

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...

There's a list in Server-side_JavaScript

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...

本文标签: JavaScript helper libraries No DOM or AJAX stuffStack Overflow