UNPKG

@qaflag/core

Version:

Base requirements for the QA Flag library

367 lines 13.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DateValue = exports.StringValue = exports.NumericValue = exports.ArrayValue = exports.BooleanValue = exports.GenericValue = exports.PrimitiveValueAbstract = exports.ValueAbstract = void 0; const to_type_1 = require("../utils/to-type"); const test_1 = require("../assertions/test"); const is_1 = require("@sindresorhus/is"); const helpers_1 = require("../utils/helpers"); class ValueAbstract { constructor(input, opts) { this.input = input; this.opts = opts; this._nameOverride = undefined; this.context = opts.context; } get scenario() { return this.context.scenario; } get suite() { return this.context.scenario.suite; } get logger() { return this.context.logger; } get $() { return this.input; } get name() { return this._nameOverride || this.opts.name; } get length() { return new NumericValue(this.input['length'] ? this.input['length'] : 0, { name: `Length of ${this.name}`, context: this.context, }); } get type() { return new StringValue((0, to_type_1.toType)(this.input), Object.assign(Object.assign({}, this.opts), { name: `Type of ${this.name}` })); } as(newName) { this._nameOverride = newName; return this; } alias(name) { return this.context.set(name, this); } get number() { return new NumericValue(this.toNumber(), this.opts); } get string() { return new StringValue(this.toString(), this.opts); } get boolean() { return new BooleanValue(this.isTruthy(), this.opts); } get array() { return new ArrayValue(this.toArray(), this.opts); } get date() { return new DateValue(this.toDate(), this.opts); } get generic() { return new GenericValue(this, this.opts); } isArray() { return Array.isArray(this.input); } isTruthy() { return !!this.input; } isUndefined() { return this.input === undefined; } isNull() { return this.input === null; } isString() { return typeof this.input === 'string'; } isNumber() { return typeof this.input === 'number'; } isBoolean() { return typeof this.input === 'boolean'; } isPrimitive() { return ['string', 'number', 'boolean'].includes(typeof this.input); } toString() { return typeof this.input == 'string' ? this.input : JSON.stringify(this.input); } toNumber() { return Number(this.input); } toArray() { return Array.isArray(this.input) ? this.input : [this.input]; } toDate() { return new Date(this.toString()); } createGeneric(data, opts) { return new GenericValue(data, Object.assign({ context: this.context, name: this.name }, opts)); } createString(str, opts) { return new StringValue(str, Object.assign({ context: this.context, name: this.name }, opts)); } createNumber(num, opts) { return new NumericValue(num, Object.assign({ context: this.context, name: this.name }, opts)); } createBoolean(bool, opts) { return new BooleanValue(bool, Object.assign({ context: this.context, name: this.name }, opts)); } createArray(data, opts) { return new ArrayValue(data, Object.assign({ context: this.context, name: this.name }, opts)); } valueOf() { return this.isPrimitive() ? this.input : this.toString(); } } exports.ValueAbstract = ValueAbstract; class PrimitiveValueAbstract extends ValueAbstract { get number() { return new NumericValue(this.toNumber(), this.opts); } get date() { return new DateValue(this.toDate(), this.opts); } get keys() { return new ArrayValue(Object.keys(this.toArrayOrObject()), this.opts); } get values() { return new ArrayValue(Object.values(this.toArrayOrObject()), this.opts); } get entries() { return new ArrayValue(Object.entries(this.toArrayOrObject()), this.opts); } toObject() { return Object(this.$); } toArray() { return Array.isArray(this.$) ? this.$ : [this.$]; } toNumber() { return Number(this.input); } toDate() { try { return is_1.default.date(this.input) ? this.input : new Date(this.toString()); } catch (_a) { throw `Could not convert ${this.name} (${this.input}) to date.`; } } valueOf() { return this.input; } split(separator) { return new ArrayValue(this.toString().split(separator), this.opts); } join(separator) { return new StringValue(this.toArray().join(separator), this.opts); } toArrayOrObject() { return Array.isArray(this.$) ? this.$ : Object(this.$); } } exports.PrimitiveValueAbstract = PrimitiveValueAbstract; class GenericValue extends PrimitiveValueAbstract { get must() { return (0, test_1.test)(this, 'must'); } get should() { return (0, test_1.test)(this, 'should'); } get could() { return (0, test_1.test)(this, 'could'); } } exports.GenericValue = GenericValue; class BooleanValue extends PrimitiveValueAbstract { get must() { return (0, test_1.test)(this, 'must'); } get should() { return (0, test_1.test)(this, 'should'); } get could() { return (0, test_1.test)(this, 'could'); } } exports.BooleanValue = BooleanValue; class ArrayValue extends PrimitiveValueAbstract { get must() { return (0, test_1.test)(this, 'must'); } get should() { return (0, test_1.test)(this, 'should'); } get could() { return (0, test_1.test)(this, 'could'); } get first() { return new GenericValue(this.$[0], Object.assign(Object.assign({}, this.opts), { name: `First in ${this.name}` })); } get length() { return new NumericValue(this.input.length, { name: `Length of ${this.name}`, context: this.context, }); } get last() { return new GenericValue(this.$.slice(-1), Object.assign(Object.assign({}, this.opts), { name: `Last in ${this.name}` })); } every(name, callback) { return new BooleanValue(this.$.every(callback), Object.assign(Object.assign({}, this.opts), { name })); } some(name, callback) { return new BooleanValue(this.$.some(callback), Object.assign(Object.assign({}, this.opts), { name })); } map(callback) { return new ArrayValue(this.$.map(callback), Object.assign(Object.assign({}, this.opts), { name: `Mapped ${this.name}` })); } filter(callback) { return new ArrayValue(this.$.filter(callback), Object.assign(Object.assign({}, this.opts), { name: `Filtered ${this.name}` })); } forEach(callback) { this.$.forEach((input, i) => { const opts = { name: `${(0, helpers_1.ordinal)(i + 1)} item in ${this.name}`, context: this.context, }; callback(new GenericValue(input, opts), i); }); return this; } pluck(propertyName) { return this.$.map((item, i) => { const opts = { name: `${(0, helpers_1.ordinal)(i + 1)} item in ${this.name}`, context: this.context, }; return new GenericValue(item[propertyName], opts); }); } } exports.ArrayValue = ArrayValue; class NumericValue extends PrimitiveValueAbstract { get must() { return (0, test_1.test)(this, 'must'); } get should() { return (0, test_1.test)(this, 'should'); } get could() { return (0, test_1.test)(this, 'could'); } } exports.NumericValue = NumericValue; class StringValue extends PrimitiveValueAbstract { get must() { return (0, test_1.test)(this, 'must'); } get should() { return (0, test_1.test)(this, 'should'); } get could() { return (0, test_1.test)(this, 'could'); } nthLine(n) { const lines = this.$.split('\n'); return new StringValue(lines[n - 1], Object.assign(Object.assign({}, this.opts), { name: `${(0, helpers_1.ordinal)(n)} line of ${this.name}` })); } get firstLine() { const lines = this.$.split('\n'); return new StringValue(lines[0], Object.assign(Object.assign({}, this.opts), { name: `first line of ${this.name}` })); } get lastLine() { const lines = this.$.split('\n'); return new StringValue(lines[lines.length - 1], Object.assign(Object.assign({}, this.opts), { name: `last line of ${this.name}` })); } get lines() { const lines = this.$.split('\n'); return lines.map((line, i) => new StringValue(line, Object.assign(Object.assign({}, this.opts), { name: `${(0, helpers_1.ordinal)(i + 1)} line of ${this.name}` }))); } get lineCount() { const lines = this.$.split('\n'); return new NumericValue(lines.length, Object.assign(Object.assign({}, this.opts), { name: `Number of lines in ${this.name}` })); } get trim() { return new StringValue(this.$.trim(), Object.assign(Object.assign({}, this.opts), { name: `Trimmed ${this.name}` })); } get length() { return new NumericValue(this.$.length, Object.assign(Object.assign({}, this.opts), { name: `Length of ${this.name}` })); } } exports.StringValue = StringValue; class DateValue extends PrimitiveValueAbstract { get must() { return (0, test_1.test)(this, 'must'); } get should() { return (0, test_1.test)(this, 'should'); } get could() { return (0, test_1.test)(this, 'could'); } get unixTime() { return new NumericValue(Math.floor(this.$.getTime() / 1000), Object.assign(Object.assign({}, this.opts), { name: `${this.name} from UNIX Time` })); } get hours() { return new NumericValue(this.$.getHours(), Object.assign(Object.assign({}, this.opts), { name: `Hours from ${this.name}` })); } get minutes() { return new NumericValue(this.$.getMinutes(), Object.assign(Object.assign({}, this.opts), { name: `Minutes from ${this.name}` })); } get seconds() { return new NumericValue(this.$.getSeconds(), Object.assign(Object.assign({}, this.opts), { name: `Seconds from ${this.name}` })); } get month() { return new NumericValue(this.$.getMonth() + 1, Object.assign(Object.assign({}, this.opts), { name: `Month from ${this.name}` })); } get dayOfMonth() { return new NumericValue(this.$.getDate(), Object.assign(Object.assign({}, this.opts), { name: `Day of Month from ${this.name}` })); } get year() { return new NumericValue(this.$.getFullYear(), Object.assign(Object.assign({}, this.opts), { name: `Year from ${this.name}` })); } get dayOfWeek() { return new NumericValue(this.$.getDay(), Object.assign(Object.assign({}, this.opts), { name: `Day of Week from ${this.name}` })); } get utcHours() { return new NumericValue(this.$.getUTCHours(), Object.assign(Object.assign({}, this.opts), { name: `UTC Hours from ${this.name}` })); } get utcMinutes() { return new NumericValue(this.$.getUTCMinutes(), Object.assign(Object.assign({}, this.opts), { name: `UTC Minutes from ${this.name}` })); } get utcSeconds() { return new NumericValue(this.$.getUTCSeconds(), Object.assign(Object.assign({}, this.opts), { name: `UTC Seconds from ${this.name}` })); } get utcMonth() { return new NumericValue(this.$.getUTCMonth() + 1, Object.assign(Object.assign({}, this.opts), { name: `UTC Month from ${this.name}` })); } get utcDayOfMonth() { return new NumericValue(this.$.getUTCDate(), Object.assign(Object.assign({}, this.opts), { name: `UTC Day of Month from ${this.name}` })); } get utcYear() { return new NumericValue(this.$.getUTCFullYear(), Object.assign(Object.assign({}, this.opts), { name: `UTC Year from ${this.name}` })); } get utcDayOfWeek() { return new NumericValue(this.$.getUTCDay(), Object.assign(Object.assign({}, this.opts), { name: `UTC Day of Week from ${this.name}` })); } get dateString() { return new StringValue(this.$.toDateString(), Object.assign(Object.assign({}, this.opts), { name: `Date String from ${this.name}` })); } get isoDateString() { return new StringValue(this.$.toISOString(), Object.assign(Object.assign({}, this.opts), { name: `ISO Date String from ${this.name}` })); } get timeString() { return new StringValue(this.$.toTimeString(), Object.assign(Object.assign({}, this.opts), { name: `Time String from ${this.name}` })); } get utcString() { return new StringValue(this.$.toUTCString(), Object.assign(Object.assign({}, this.opts), { name: `UTC String from ${this.name}` })); } } exports.DateValue = DateValue; //# sourceMappingURL=values.js.map