JavaScript Code Contract Libraries?

后端 未结 4 797
花落未央
花落未央 2021-02-01 08:11

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 Js

4条回答
  •  佛祖请我去吃肉
    2021-02-01 08:54

    I may suggest you the next code contracts library: dbc-code-contracts.


    NPM: https://www.npmjs.com/package/dbc-code-contracts

    GitLab repo (home): https://gitlab.com/o.oleg/orbios.dbc#README

    CI-builds with the unit-tests: https://gitlab.com/o.oleg/orbios.dbc/-/jobs/


    Sample code:

    Dbc.Contract.throwException = true;
    
    const domId = "my-div";
    const div   = document.createElement("div");
    div.id .    = domId;
    document.body.appendChild(div);
    
    const state = Dbc.Dom.removeById(domId);
    Dbc.Contract.isTrue(state);
    Dbc.Contract.isNull(document.getElementById(domId));
    

    The following contracts are supported (2nd November, 2017):

    1. isFunction
    2. isObject
    3. isSymbol
    4. isBoolean
    5. isTrue
    6. isFalse
    7. isString
    8. isEmptyString
    9. isNotEmptyString
    10. areStringsEqual
    11. isNumber
    12. isNumberLess
    13. isNumberBigger
    14. areNumbersEqual
    15. isValueNaN
    16. isDefined
    17. isUndefined
    18. isNull
    19. isArray
    20. isEmptyArray
    21. isNotEmptyArray
    22. isObjectImmutable
    23. isPromise
    24. isPrototypeOf

    Also, there are internal contract checks, inside the DOM methods from this library.

提交回复
热议问题