flagpole
Version:
Simple and fast DOM integration, headless or headful browser, and REST API testing framework.
125 lines • 3.82 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const _1 = require(".");
class ProtoValue {
get $() {
return this._input;
}
get name() {
return this._name || 'it';
}
constructor(input, context, name) {
this._input = input;
this._context = context;
this._name = name || null;
}
toArray() {
return this.isArray() ? this._input : [this._input];
}
valueOf() {
return this._input;
}
toString() {
const type = _1.Flagpole.toType(this._input);
if (this._input && this._input.value) {
return String(this._input.value);
}
else if (type == 'object') {
return String(Object.keys(this._input));
}
return String(this._input);
}
toFloat() {
return parseFloat(this.toString());
}
toInteger() {
return parseInt(this.toString());
}
toType() {
return String(_1.Flagpole.toType(this._input));
}
isNullOrUndefined() {
return _1.Flagpole.isNullOrUndefined(this._input);
}
isUndefined() {
return this.toType() == 'undefined';
}
isNull() {
return this._input === null;
}
isPromise() {
return this.toType() == 'promise';
}
isArray() {
return this.toType() == 'array';
}
isString() {
return this.toType() == 'string';
}
isObject() {
return this.toType() == 'object';
}
isNumber() {
return this.toType() == 'number' && this._input !== NaN;
}
isNumeric() {
return !isNaN(this._input);
}
isNaN() {
return this._input === NaN;
}
isCookie() {
return this._input && this._input.cookieString;
}
isRegularExpression() {
return this.toType() == 'regexp';
}
isCheerioElement() {
return this.toType() == 'cheerio';
}
isPuppeteerElement() {
return this.toType() == 'elementhandle';
}
hasProperty(key) {
return __awaiter(this, void 0, void 0, function* () {
return this._wrapAsValue((this._input &&
this._input.hasOwnProperty &&
this._input.hasOwnProperty(key)), `${this.name} has property ${key}`);
});
}
_wrapAsValue(data, name) {
return new Value(data, this._context, name);
}
}
exports.ProtoValue = ProtoValue;
class Value extends ProtoValue {
getProperty(key) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.isNullOrUndefined() && this.hasProperty(key)) {
const thisValue = this.$;
return this._input[key];
}
return undefined;
});
}
hasProperty(key) {
return __awaiter(this, void 0, void 0, function* () {
const thisValue = this.$;
return (thisValue && thisValue.hasOwnProperty(key));
});
}
get length() {
const thisValue = this.$;
return new Value((thisValue && thisValue.length) ? thisValue.length : 0, this._context, `Length of ${this._name}`);
}
}
exports.Value = Value;
//# sourceMappingURL=value.js.map