README.md

    Assertions & checkers for variables value

    HomePage

    Include

    package.json:

    "dependencies": {
      "@jzucen/asserts-js": "git+https://gitflic.ru/project/jzucen/asserts-js.git"
    }
    

    Import

    import { asserts, checks } from '@jzucen/asserts-js';
    
    const someVar = 'test value';
    asserts.inspect('inspected variable name', someVar).isString().isEmpty(); // throws AssertError in isEmpty()
    console.log(checks.inspect('someVar', 'variable value').not().isInteger()); // output: "true"
    

    Properties & methods of asserts

    Property/method
    String name
    Mixed value
    Boolean expected
    Boolean inverted
    Mixed actual
    this self
    ErrorClass ErrorClass
    this setErrorClass(ErrorClass errorClass)
    self constructor (String name, Mixed value, Boolean inverted = false)
    self inspect (String name, Mixed value, Boolean inverted = false)
    ✱✱ self expect (Boolean expect)
    ✱✱ ✱✱✱ self not ()
    this register (String name, Function assert, ?String message)
    self isEqualTo (Mixed expectedValue, ?String message)
    self isEmpty (?String message)
    self hasType (String expectedType, ?String message)
    self hasTypeFromList (String[] typesList, ?String message)
    self isUndefined (?String message)
    self isFunction (?String message)
    self isBoolean (?String message)
    self isString (?String message)
    self isNumber (?String message)
    self isObject (?String message)
    self isArray (?String message)
    self isNull (?String message)
    self isNaN (?String message)
    self isTrue (?String message)
    self isFalse (?String message)
    self hasLength (Number length, ?String message)
    self containsSubString (String subString, ?String message)
    self matchesRegExp (RegExp regexp, ?String message)
    self isZero (?String message)
    self isInteger (?String message)
    self isFloat (?String message)
    self isLessThan (Number value, ?String message)
    self isLessOrEqual (Number value, ?String message)
    self isGreaterOrEqual (Number value, ?String message)
    self isGreaterThan (Number value, ?String message)
    self isPositive (?String message)
    self isNegative (?String message)
    self isNatural (?String message)
    self isInRange (Number minimal, Number maximal, ?Number|Boolean bounds, ?String message)
    self isInstanceOf (Class expectedClass, ?String message)
    self isDate (?String message)
    self isRegExp (?String message)
    self isMap (?String message)
    self isSet (?String message)
    self isWeakMap (?String message)
    self isWeakSet (?String message)
    self hasProperty (String property, ?String message)
    self containsArrayItem (Mixed arrayItem, ?String message)
    self contains (Mixed smth, ?String message)
    self isExistsPath (?String message)
    self hasPathStats (?String message)
    self isExistsFile (?String message)
    self isExistsDirectory (?String message)
    • – always return a new instance;
    • ✱✱ – return either a new instance or the original if property inverted remains unchanged;
    • ✱✱✱ – only applies to the next statement, then returns to the original instance;
    • methods that perform assertion checking either throw an exception or return the (original) instance.

    Properties & methods of checks

    • Is a proxy for the assertion object, meaning it has the same properties and methods.
    • Assertion checking methods return true or false instead of the original object.
    • Has an additional property lastError containing error information if the last check returned false.

    Register custom asserts

    import { asserts, checks } from '@jzucen/asserts-js';
    
    asserts.register(
      'newAssertName', // assert name (valid function name)
      (asserts, message, ...params) => { // positive (!!!) assert
        // asserts – link to this (asserts/cheks)
        // message – registered error message
        // params – additional parameters to check
        const [top] = params;
        asserts.isInRange(0, top).not().isEqualTo(top);
        // may not return anything - this is not necessary
      },
      'Assertion of {#name} was fail'
    );
    
    console.log(
      checks.inspect('myVar', 100).newAssertName(100), // output: "false"
      checks.lastError // output: AssertError: Value of "myVar" must not be equal to "100" but "100" is given
    );
    
    Конвейеры
    0 успешных
    0 с ошибкой