UNPKG

@qaflag/core

Version:

Base requirements for the QA Flag library

288 lines 9.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.test = exports.Test = void 0; const helpers_1 = require("../utils/helpers"); const values_1 = require("../value/values"); const test_base_1 = require("./test-base"); const is_value_interface_1 = require("./utils/is-value-interface"); const assertion_1 = require("./assertion"); class Test extends test_base_1.TestBase { get assertion() { return new assertion_1.Assertion(this.input, this.opts); } _clone() { return this._copy(this.input); } _copy(input, pushWord) { return new Test(input, this.mustShouldCould, this.opts, pushWord ? [...this.message, pushWord] : this.message); } get length() { const length = Array.isArray(this.input.$) ? this.input.$.length : String(this.input.$).length; return this._copy(new values_1.NumericValue(length, { name: `Length of ${this.input.name}`, context: this.input.context, }), 'length'); } get lengths() { const getLength = (item) => Array.isArray(item) ? item.length : String(item).length; const length = this.opts.evalType == 'standard' ? getLength(this.input.$) : this.input.array.$.map(item => getLength(item)); return this._copy(new values_1.ArrayValue(Array.isArray(length) ? length : [length], { name: `Lengths of ${this.input.name}`, context: this.input.context, }), 'length'); } assert(test, word) { return this.result(this.assertion[test](), word); } assertThat(test, thatValue, word = '') { const { value, name } = toThat(thatValue); return this.result(this.assertion[test](value), `${word} ${name}`); } equal(value) { return this.assertThat('equals', value, 'equals'); } equalTo(value) { return this.assertThat('equals', value, 'equal to'); } exactly(value) { return this.assertThat('isExactly', value, 'exactly'); } include(value) { return this.assertThat('arrayIncludes', value, 'include'); } inArray(value) { return this.assertThat('isInArray', value, 'in array'); } containedIn(value) { return this.assertThat('containedIn', value, 'contained in'); } contain(value) { return this.assertThat('contains', value, 'contain'); } exist() { return this.assert('exists', 'exist'); } true() { return this.assert('isTrue', 'true'); } false() { return this.assert('isFalse', 'false'); } nullOrUndefined() { return this.assert('isNullOrUndefined', 'null or undefined'); } null() { return this.assert('isNull', 'null'); } undefined() { return this.assert('isUndefined', 'undefined'); } boolean() { return this.assert('isBoolean', 'boolean'); } truthy() { return this.assert('isTruthy', 'truthy'); } empty() { return this.assert('isEmpty', 'empty'); } type(typeName) { return this.assertThat('isType', typeName); } date() { return this.assert('isDate', 'date'); } integer() { return this.assert('isInteger', 'integer'); } string() { return this.assert('isString', 'string'); } object() { return this.assert('isObject', 'object'); } array() { return this.assert('isArray', 'array'); } number() { return this.assert('isNumber', 'number'); } numeric() { return this.assert('isNumeric', 'numeric'); } like(value) { return this.assertThat('isLikeString', value, 'like'); } similarTo(thatValue, maxDistance = 3) { const { value, name } = toThat(thatValue); return this.result(this.assertion.isSimilarTo(String(value), maxDistance), `similar to ${name} (Max Distance: 3)`); } startWith(value) { return this.result(this.assertion.startsWith(value), `starts with ${Array.isArray(value) ? (0, helpers_1.humanReadableList)(value, ',', 'or') : value}`); } endWith(value) { return this.result(this.assertion.endsWith(value), `ends with ${Array.isArray(value) ? (0, helpers_1.humanReadableList)(value, ',', 'or') : value}`); } regularExpression(pattern) { return this.assertThat('matchesPattern', pattern, 'regular expression'); } email() { return this.assert('isEmailAddress', 'email address'); } creditCard() { return this.assert('isCreditCard', 'credit card number'); } numericString() { return this.assert('isNumericString', 'numeric string'); } ipAddress(version) { return this.assert('isIpAddress', 'IP Adddress'); } url() { return this.assert('isUrl', 'URL'); } jwt() { return this.assert('isJwt', 'JWT'); } md5() { return this.assert('isMd5', 'MD5 Hash'); } postalCode(countryCode) { return this.assertThat('isPostalCode', countryCode, 'postal code in'); } uuid(version) { return this.assert('isUuid', 'UUID'); } uppercase() { return this.assert('isUppercase', 'uppercase'); } lowercase() { return this.assert('isLowercase', 'lowercase'); } slug() { return this.assert('isSlug', 'slug'); } mimeType() { return this.assert('isMimeType', 'valid mime type'); } emptyString() { return this.assert('isEmptyString', 'empty string'); } falsy() { return this.assert('isFalsy', 'falsy'); } mongoId() { return this.assert('isMongoId', 'Mongo ID'); } alphanumeric() { return this.assert('isAlphanumeric', 'alphanumeric'); } onlyLetters() { return this.assert('isOnlyLetters', 'only letters'); } onlyNumbers() { return this.assert('isOnlyNumbers', 'only numbers'); } greaterThan(value) { return this.assertThat('isGreaterThan', value, 'greater than'); } greaterThanOrEquals(value) { return this.assertThat('isGreaterThanOrEquals', value, 'greater than or equal to'); } lessThan(value) { return this.assertThat('isLessThan', value, 'less than'); } lessThanOrEquals(value) { return this.assertThat('isLessThanOrEquals', value, 'less than or equal to'); } between(valueA, valueB) { return this.result(this.assertion.isInRange(valueA, valueB), `beween ${valueA} and ${valueB}`); } closeTo(value, within = 0.01) { return this.result(this.assertion.isCloseTo(value, within), `close to ${value}`); } roundTo(value) { return this.assertThat('roundsTo', value, 'rounds to'); } roundUpTo(value) { return this.assertThat('roundsUpTo', value, 'rounds up to'); } roundDownTo(value) { return this.assertThat('roundsDownTo', value, 'rounds down to'); } zero() { return this.assert('isZero', 'zero'); } nonZeroNumber() { return this.assert('isNonZeroNumber', 'non-zero number'); } nonZeroInteger() { return this.assert('isNonZeroInteger', 'non-zero integer'); } odd() { return this.assert('isOddInteger', 'odd number'); } even() { return this.assert('isEvenInteger', 'even number'); } divisibleBy(value) { return this.assertThat('isDivisibleBy', value, 'divisible by'); } positiveNumber() { return this.assert('isPositiveNumber', 'positive number'); } positiveInteger() { return this.assert('isPositiveInteger', 'positive integer'); } negativeNumber() { return this.assert('isNegativeNumber', 'negative number'); } negativeInteger() { return this.assert('isNegativeInteger', 'negative integer'); } nan() { return this.assert('isNan', 'NaN'); } property(value) { return this.assertThat('hasProperty', value, 'property'); } properties(value) { return this.assertThat('hasProperties', value, 'properties'); } emptyObject() { return this.assert('isEmptyObject', 'empty object'); } arrayOf(typeName) { return this.assertThat('isArrayOf', typeName, 'array of'); } emptyArray() { return this.assert('isEmptyArray', 'empty array'); } before(value) { return this.assertThat('isBeforeDate', value, 'before'); } after(value) { return this.assertThat('isAfterDate', value, 'after'); } inThePast() { return this.assert('inThePast', 'in the past'); } inTheFuture() { return this.assert('inTheFuture', 'in the future'); } } exports.Test = Test; const test = (input, type = 'must') => { return new Test(input, type); }; exports.test = test; const toThat = (value) => { return (0, is_value_interface_1.isValueInterface)(value) ? { value: value.$, name: value.name } : { value, name: String(value) }; }; //# sourceMappingURL=test.js.map