• 习近平会见蒙古国总统巴特图勒嘎 2019-06-26
  • 别让“屈原留下三天端午假”的段子再流传 2019-06-19
  • 爱国神曲《厉害了我的国》爆红网络 2019-06-19
  • 美国依据“301”调查发布对中国产品征收关税清单 2019-06-07
  • 沈阳举办首届锡伯族泥巴节 2019-06-04
  • 宪法修正案专题记者会 2019-06-04
  • “00后”登场、加分政策收紧 今年高考有这些新特征! 2019-05-30
  • 《脉动的中医》推广健康新理念 让中医回归自然 2019-05-27
  • 述评:美国应当记取历史教训 2019-05-27
  • 中国现在看谁脸色行使啦?哈~ 2019-05-23
  • 中国保险行业协会发布汽车后市场配件合车标准 2019-05-13
  • 世界献血者日 近距离接触一袋合格血的成长历程 2019-05-13
  • 加强网约车事中事后联合监管 2019-04-24
  • 广东警方摧毁全国首例“呼死你”专案犯罪团伙 2019-04-24
  • 旅游市场再创佳绩端午假期河北接待游客2026.77万 揽金147.79亿 2019-04-20
  • 综合技术

    湖南快乐十分是否合法:Light weight runtime type checking and document generation for JavaScript

    湖南快乐十分每天期数 www.91zhb.com 微信扫一扫,分享到朋友圈

    Light weight runtime type checking and document generation for JavaScript
    0

    doctyr v0.0.6b

    DOCument, TYpe, Runtime – Runtime type checking and document generation for JavaScript.

    doctyr
    supports runtime type checking and documentation generation for JavaScript functions and return values.
    Types can be directly specified or infered from default arguments. Type checking can go beyond basic types and
    includes support for instanceof checks and custom validation functions.

    The API has been designed with simplicity in mind.

    1. Primitive and complex object type checking can be accomplished without learning a schema system.

    2. Re-writing existing code is not required.

    3. Types can be infered from default arguments.

    4. Any boolean functions from existing libraries or custom written ones can be used to check types.

    5. Using the live documentation facility is completely optional and incurs no compute or substative memory overhead if no
      documentation for functions is provided.

    6. The type checking can be optionally included or excluded from production builds and dynamically turned on and off.

    Installation

    npm install doctyr

    There are browser based files in the browser
    sub-directory.

    Basic Usage

    The call signature for doctyr
    is (function,thisType,[argType[,...]],returnType)
    . That is
    a function followed by an array, the first element of which is the type of the this
    argument, an array of
    types of the arguments, and a final arguments that is the type of the return value.

    The type specifications can be undefined
    , strings, objects, or instances of doctyr.spec
    . If the
    value undefined
    is used, then the type is unconstrained. See API section for more detail.

    You can use multiple coding styles to take advantage of doctyr
    including:

    Direct function defintion, e.g.

    const myFunction = doctyr(function(arg1) { ...; return arg1; },undefined,["string"],"string");

    Re-defining functions by assigning a return value, e.g.

    function myFunction(arg1) { ...; return arg1; }
    
    myFunction = doctyr(func,undefined,["string"],"string"); // this could be done conditionally for non-production

    Re-writing functions to include guard calls and return conform checks, e.g.

    function myFunction(arg1) {
    	const returns = doctyr.guard(this,myFunction,[undefined,"string","string"]);
    	... // your code that assigns something to returnValue
    	return doctyr.conforms(returnValue,returns);
    }

    Type Inference

    You can rely on default arguments to determine the types. Both the below allow any type for this
    and return value,
    but constrain the argument to a string.

    function myFunction(arg1="myArg") { ...; return arg1; }
    
    myFunction = doctyr(func);
    function myFunction(arg1="myArg") {
    	doctyr.guard(this,myFunction,arguments);
    	... // your code
    	return doctyr.conforms(arg1,"string");
    }

    Test Functions and Instances

    You can provide functions to do type checks, e.g. ensure a value is even

    function myFunction(arg1) { ...; return arg1; }
    
    myFunction = doctyr(func,undefined,[value => value % 2 ===0]);

    If the function is a constructor, then the check will be satisfied if the argument is an object that satisfies instanceof
    ,
    e.g. ensure an argument is an instanceof a Date:

    function myFunction(arg1) { ...; return arg1; }
    
    myFunction = doctyr(func,undefined,[Date]);

    Polymorphic Values

    Polymorphic arguments and return values are supported through the use of an array to contain the types. The example below will
    allow a string or number as the function argument:

    function myFunction(arg1) { ...; return arg1; }
    
    myFunction = doctyr(func,undefined,[["string","number"]],"string");

    Complex Objects

    Complex object type checking just requires the provision of an object having the expected surface with property values being types.
    The example below ensures that the argument has a name property with a value of type string, an age property with a value of type number,
    and an address that is either undefined
    or an instanceof Object, i.e. it is optional. The below ensure the argument has
    a string name property, and age number sproperty, and an optional address that if provided is an instance of an Object
    .

    function myFunction(arg1) { ...; return arg1; }
    
    myFunction = doctyr(func,undefined,[{name:"string",age:"number",address:[undefined,Object]}]);

    Code As Documentation

    By using doctyr.spec(config)
    instead on primitive values, you can add documentation to your code for runtime generation, e.g.

    function myFunction(arg1) { ...; return arg1; }
    
    myFunction = doctyr(myFunction,doctyr.spec({type:undefined,doc:{description:"No 'this' context is expected."}}),
    						["string"],
    						doctyr.spec({type:"string",doc:{description:"The original string argument is returned."}}));
    							
    const html = doctyr.toHTML(myFunction);

    will return HTML that renders as:

    myFunction(arg1)

    • thisType

      • undefined No ‘this’ context is expected.
    • Arguments

      • arg1:string
    • Returns

      • string The original string argument is returned.

    Turning doctyr off

    Just set doctyr.on = false
    .

    Developed and Tested With

    Eclipse
    , Mocha
    , Chai
    , and Browserstack
    .

    Release History (reverse chronological order)

    2018-11-14 v0.0.6b Synchronized version numbers.

    2018-11-14 v0.0.5b Reintroduced HTML generation.

    2018-11-13 v0.0.4b Removed JavaScript parser and changed API to dramatically simplify. HTML generation temporarily removed.

    2018-08-12 v0.0.3b Documentation updates.

    2018-08-12 v0.0.2b Documentation updates.

    2018-08-11 v0.0.1b First public release.

    License

    MIT License

    Copyright (c) 2018 Simon Y. Blackwell, AnyWhichWay, LLC

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all
    copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    SOFTWARE.

    阅读原文...

    Github

    asp.netcore 深入了解配置文件加载过程

    上一篇

    扎克伯格:Facebook拟成立独立机构 处理内容移除申诉问题

    下一篇

    您也可能喜欢

    评论已经被关闭。

    插入图片
    Light weight runtime type checking and document generation for JavaScript

    长按储存图像,分享给朋友

  • 习近平会见蒙古国总统巴特图勒嘎 2019-06-26
  • 别让“屈原留下三天端午假”的段子再流传 2019-06-19
  • 爱国神曲《厉害了我的国》爆红网络 2019-06-19
  • 美国依据“301”调查发布对中国产品征收关税清单 2019-06-07
  • 沈阳举办首届锡伯族泥巴节 2019-06-04
  • 宪法修正案专题记者会 2019-06-04
  • “00后”登场、加分政策收紧 今年高考有这些新特征! 2019-05-30
  • 《脉动的中医》推广健康新理念 让中医回归自然 2019-05-27
  • 述评:美国应当记取历史教训 2019-05-27
  • 中国现在看谁脸色行使啦?哈~ 2019-05-23
  • 中国保险行业协会发布汽车后市场配件合车标准 2019-05-13
  • 世界献血者日 近距离接触一袋合格血的成长历程 2019-05-13
  • 加强网约车事中事后联合监管 2019-04-24
  • 广东警方摧毁全国首例“呼死你”专案犯罪团伙 2019-04-24
  • 旅游市场再创佳绩端午假期河北接待游客2026.77万 揽金147.79亿 2019-04-20
  • 冰球用球 湖北快3第20190325开奖结果 中彩网开奖数据双色球开奖结果 乌鲁木齐麻将机遥控器 极速11选5计划 澳洲幸运5技巧杀号 两码中特期期准永两码中特期期准 北京十一选五体彩 香港真道人一波中特 香港六合彩开奖结果记录 天津快乐十分0927065期 南粤36选7好彩1季节表 福建快3跨度和值表 排球教学视频上手传球 11月17日篮彩分析