JavaScript代码合同库? [英] JavaScript Code Contract Libraries?

查看:97
本文介绍了JavaScript代码合同库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚启动了一个新的Web应用程序,我想在我的JavaScript中实现某种形式的contract'esque样式验证。我做了一些快速的谷歌搜索,并遇到了 JsContact 但语法并不是我想到的。是否有人知道其他图书馆?

I am just starting up a new web application and I want to implement some form of contract'esque style validation in my JavaScript. I did some quick googling, and came across JsContact but the syntax isn't quite what I had in mind. Is anyone aware of other libraries?

我在想我希望语法类似于

I am thinking I want the syntax to be something like

String.prototype.padLeft = function(c, width) {
  Verify.value(c).isRequired().isNotNull().isChar();
  Verify.value(width).isRequired().isNotNull().isNumber().greaterThan(0);

  ...

  Verify.value(result).isNotNull();

  return result;
};

尽管将我自己的库放在一起需要很长时间才能拥有我想要的语法/方法,如果其他人已经完成了这项工作并且足够接近,那么我将节省一些时间。提前致谢。

Although it won't take long to put together my own library that has the syntax/methods I want, if someone else has already done the work and it is close enough, it will save me some time. Thanks in advance.

更新

我没有时间工作直到今天下午,所以我会再花几个小时看看是否有人有任何建议。如果没有,我会发布我在某处创建的任何内容作为下面的答案供其他人参考,如果他们愿意的话。

I won't have time to work on this until this afternoon, so I will give it a few more hours to see if anyone has any recommendations. If not, I will post whatever I create up somewhere as an answer below for other people to reference if they desire.

我还考虑了一些API这是有道理的,我现在正在考虑像(人为的例子):

I have also given a little more thought to the API that would make sense, and I am currently thinking something like (contrived examples):

 function searchUser(firstName, middleInit, lastName) {
   Verify.value(firstName).isString().matching(/\w+/);       // Must have value
   Verify.value(middleInit).whenNotNull().isChar();          // May be null, but not undefined
   Verify.value(lastName).isString().withMinimumLengthOf(2); // Must have value

   ...
 }

 function syncTime(serverTime, now) {
   Verify.value(serverTime).isDate();         // Must have value.
   Verify.value(now).whenDefined().isDate();  // May be undefined, but not null.

 }

我目前的想法是容忍NULL或UNDEFINED值是非典型的(至少对我来说?),这样,而不是明确指定值.isNotNull(),而不是如上所示,实际上会禁用.whenDefined()或.whenNotNull()的规则。我可能会在UNDEFINED上使.whenNotNull()没有错误,但我认为NULL与UNDEFINED是一个重要的区别;我们会看到......所有其他方法都很典型......想法?评论?

My current thought is that tolerating NULL or UNDEFINED values is atypical (at least for me?), as such, rather than explicitly specifying that a value .isNotNull() you would actually disable the rule for .whenDefined() or .whenNotNull() as shown above. I may make .whenNotNull() not error on UNDEFINED, but I see NULL vs. UNDEFINED as an important distinction; we'll see... all other methods will be pretty typical... thoughts? comments?

推荐答案

鉴于没有人推荐任何现有的图书馆,或者我认为这是个好主意我疯了继续把一个基本的图书馆拼凑起来。代码不是很花哨,但它可以实现我想要的,并且运行起来相当快(IE中每毫秒大约40个链式检查)。

Given that no one has recommended any existing libraries, or that I am crazy for thinking this is a good idea I went ahead and threw together a basic library. The code isn't fancy, but it does what I want, and it is reasonably fast to run (approx 40 chained checks per ms in IE).

我选择了最终语法,如:

I settled on a final syntax like:

function syncTime(serverTime, now) {
  Verify.value(serverTime).always().isDate();   // Cannot be undefined or null.
  Verify.value(now).whenDefined().isDate();     // Cannot be null, but must be date when defined.

  //Code
}

function searchForUser(firstName, middleInit, lastName) {
  Verify.value(firstName).always().isString().withMinimumLengthOf(2);  // Cannot be undefined or null.
  Verify.value(lastName).always().isString().withMinimumLengthOf(2);   // Cannot be undefined or null.
  Verify.value(middleInit).whenNotNull().isChar().between('A', 'Z');   // Cannot be undefined, but must be single char string when not null.

  //Code
}

我选择了明确的通过.always()检查必须有价值,我个人觉得它更好阅读;但我可以看到一些另一种方式。

I opted for an explicit 'Must Have Value' via the .always() check, personally I found it nicer to read; but I could see some going another way.

鉴于源代码超出了我想在此答案中发布的内容,请转到 CodePlex Wiki Page 如果您对源代码感兴趣。我想它转向更流畅的断言库;但它可以满足我的需求。

Given that the source is more than I want to post in this answer, please head to this CodePlex Wiki Page if you are interested in the source. I guess it turned in to more of a fluent assertion library; but it does what I need.

更新

我更新了源代码上面链接的CodePlex页面。具体来说,我重构了Verify类以使用'值上下文',而不是总是构建新的Verifier对象;大大提高了IE的性能(从来不是FireFox或Chrome的问题)...现在在IE中每ms处理大约100个链式检查。

I updated the source on the linked CodePlex page above. Specifically, I restructed the Verify class to make use of a 'value context' rather than always building new Verifier objects; improved IE's performance greatly (never was an issue with FireFox or Chrome)... now handles about 100 chained checks per ms in IE.

这篇关于JavaScript代码合同库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆