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.
请注意,我不是在谈论像jQuery或Dojo等Ajax/DOM库.我知道那些库带来了我在这里谈论的一些功能,但不是全部.我想要一个独立于环境的库,以便相同的库可以与服务器端JavaScript一起使用.
到目前为止,我发现最好的库是php.js,但我不喜欢它如何污染全局命名空间.我也不太喜欢PHP函数的命名方式.
编辑
我和Underscore.js一起解决了,因为它很容易扩展.所以我用自己的字符串函数扩展它.看看这里:
Jam*_*mes 11
看看下划线.
另一方面,你想要的东西看起来很简单:
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
Run Code Online (Sandbox Code Playgroud)
您可以查看Google Closure Library.它提供以下包:
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)
Run Code Online (Sandbox Code Playgroud)
Closure Library是开源的,Google 应该在Gmail,地图,文档,网站,图书,阅读器,Blogger,日历和Picasa中使用它.
您可能需要查看Array和String包以获得快速的第一印象.