是否有一个 JavaScript 库包含一组丰富的非常高级的常用函数?

发布于 2024-08-29 15:07:40 字数 237 浏览 8 评论 0原文

我发现大多数知名的 javascript 库(例如 jquery、YUI...等)都缺少许多高级函数。以字符串操作为例,startsWith、endsWith、contains、lTrim、rTrim、trim、isNullOrEmpty...等。这些功能其实都是很常见的。

我想知道是否存在一个 javascript 库/ javascript 库插件可以填补这些空白(包括但不限于字符串操作)?

如果库不重写原型那就太好了。

I find that many high level functions are missing in most well-known javascript libraries such as jquery, YUI...etc. Taking string manipulation as an example, startsWith, endsWith, contains, lTrim, rTrim, trim, isNullOrEmpty...etc. These function are actually very common ones.

I would like to know if there exists a javascript library/ plugin of a javascript library that fills these gaps (including but not limited to string manipulation)?

It would be great if the library does not override the prototype.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦冥 2024-09-05 15:07:40

看看 underscore.js (遗憾的是,没有字符串操作,但有很多其他好东西)。

Take a look at underscore.js (sadly, no string manipulation, but lots of other good stuff).

灼疼热情 2024-09-05 15:07:40

大多数字符串函数都可以使用与字符串对象关联的其他方法来使用,例如,

var myString = 'hello world';

myString.indexOf('hello') == 0; //same as startsWith('hello');

如果您愿意,您可以将这些函数包装到其他函数中。我认为向字符串对象添加原型将是到达那里的方法,并且您找到的任何库都可能会沿着这条路线走下去。

Most of those string functions are available using other methods associated with the string object eg

var myString = 'hello world';

myString.indexOf('hello') == 0; //same as startsWith('hello');

You could wrap these functions up into other functions if you wish. I think adding prototypes to the string object would be the way to go there and any libraries you find will probably go down that route anyway.

遮云壑 2024-09-05 15:07:40

ms ajax core 库包含所有这些字符串方法以及日期方法等基本上是将 .net 引入 js 的勇敢尝试。

您不需要加载整个 MS Ajax js 堆栈,只需加载核心文件。

The ms ajax core library contains all of those string methods as well as date methods etc. basically a valiant attempt at bringing .net to js.

You don't need to load the entire MS Ajax js stack, just the core file.

没企图 2024-09-05 15:07:40

如果您不想扩展原型,所有这些都可以使用包装器轻松实现。

var StringWrapper = (function(){
    var wrapper = {
        string: null,
        trim: function(){
            return this.string.replace(/^\s+|\s+$/g, "");
        },
        lTrim: function(){

        }
    };

    return function(string){
        wrapper.string = string;
        return wrapper;
    };
})();

StringWrapper("   aaaa bbbb    ").trim(); /// "aaaa bbbb"

函数仅创建一次,因此非常高效。但是在辅助对象上使用包装器确实会产生一次额外的函数调用。

All of this is easily implemented with wrappers if you don't want to extend the prototype

var StringWrapper = (function(){
    var wrapper = {
        string: null,
        trim: function(){
            return this.string.replace(/^\s+|\s+$/g, "");
        },
        lTrim: function(){

        }
    };

    return function(string){
        wrapper.string = string;
        return wrapper;
    };
})();

StringWrapper("   aaaa bbbb    ").trim(); /// "aaaa bbbb"

The functions are only being created once, so its quite efficient. But using a wrapper over a helper object does incur one extra function call.

旧街凉风 2024-09-05 15:07:40

underscore.string 看起来可能适合您的需求。他们是这样描述的:

Underscore.string 是一个 JavaScript 库,用于舒适地操作字符串,Underscore.js 的扩展受到 Prototype.js、Right.js、Underscore 和漂亮的 Ruby 语言的启发。

Underscore.string 为您提供了几个有用的函数:capitalize、clean、includes、count、escapeHTML、unescapeHTML、insert、splice、startsWith、endsWith、titleize、trim、truncate 等。

underscore.string looks like it might suit your needs. Here's how they describe it:

Underscore.string is JavaScript library for comfortable manipulation with strings, extension for Underscore.js inspired by Prototype.js, Right.js, Underscore and beautiful Ruby language.

Underscore.string provides you several useful functions: capitalize, clean, includes, count, escapeHTML, unescapeHTML, insert, splice, startsWith, endsWith, titleize, trim, truncate and so on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文