UNPKG

flagpole

Version:

Simple and fast DOM integration, headless or headful browser, and REST API testing framework.

393 lines 12.8 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } 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) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.cast = exports.randomIn = exports.randomInt = exports.random = exports.lastIn = exports.middleIn = exports.firstIn = exports.objectContainsKeys = exports.objectContains = exports.deepStrictEqual = exports.deepEqual = exports.getMessageAndCallbackFromOverloading = exports.exitProcess = exports.emptyFolder = exports.ensureFolderExists = exports.normalizePath = exports.arrayExactly = exports.arrayEquals = exports.flatten = exports.asyncSome = exports.asyncNone = exports.asyncMapToObject = exports.asyncFlatMap = exports.asyncMap = exports.asyncFilter = exports.asyncEvery = exports.asyncWhichFails = exports.asyncWhich = exports.asyncForEach = exports.asyncForEachUntilFirst = exports.runAsync = exports.openInBrowser = exports.uniqueId = exports.arrayUnique = exports.toType = exports.isArray = exports.isAsyncCallback = exports.isNullOrUndefined = exports.jsonParse = exports.arrayify = void 0; const fs = require("fs"); const path = require("path"); const nodeAssert = require("assert"); const arrayify = (value) => { return toType(value) == "array" ? value : [value]; }; exports.arrayify = arrayify; const jsonParse = (json) => { try { return JSON.parse(json); } catch (ex) { } return {}; }; exports.jsonParse = jsonParse; function isNullOrUndefined(obj) { return typeof obj === "undefined" || obj === null; } exports.isNullOrUndefined = isNullOrUndefined; function isAsyncCallback(func) { return func.toString().indexOf("=> __awaiter(") > 0; } exports.isAsyncCallback = isAsyncCallback; function isArray(obj) { return toType(obj) == "array"; } exports.isArray = isArray; function toType(obj) { if (typeof obj === "undefined") { return "undefined"; } else if (obj === null) { return "null"; } else if (obj === NaN) { return "nan"; } else if (!!obj && obj.cheerio) { return "cheerio"; } else if (!!obj && (typeof obj === "object" || typeof obj === "function") && typeof obj.then === "function" && typeof obj.catch === "function") { return "promise"; } else if (obj && obj.constructor && obj.constructor.name) { return String(obj.constructor.name).toLocaleLowerCase(); } else if (obj && obj.constructor && obj.constructor.toString) { let arr = obj.constructor.toString().match(/function\s*(\w+)/); if (arr && arr.length == 2) { return String(arr[1]).toLocaleLowerCase(); } } const match = {}.toString .call(obj) .match(/\s([a-zA-Z]+)/); return match !== null ? String(match[1]).toLocaleLowerCase() : ""; } exports.toType = toType; function arrayUnique(arr) { return [...new Set(arr)]; } exports.arrayUnique = arrayUnique; function uniqueId() { return "_" + Math.random().toString(36).substr(2, 9); } exports.uniqueId = uniqueId; function openInBrowser(content) { return __awaiter(this, void 0, void 0, function* () { const open = require("open"); const tmp = require("tmp"); const tmpObj = tmp.fileSync({ postfix: ".html" }); const filePath = tmpObj.name; fs.writeFileSync(filePath, content); yield open(filePath); return filePath; }); } exports.openInBrowser = openInBrowser; function runAsync(callback, delay = 1) { setTimeout(callback, delay); } exports.runAsync = runAsync; function asyncForEachUntilFirst(array, callback) { let output = null; array.some((value, i, arr) => { output = callback(value, i, arr); return !!output; }); return output; } exports.asyncForEachUntilFirst = asyncForEachUntilFirst; function asyncForEach(array, callback) { return new Promise((resolve) => { Promise.all(array.map(callback)).then(() => { resolve(); }); }); } exports.asyncForEach = asyncForEach; function asyncWhich(array, callback) { return __awaiter(this, void 0, void 0, function* () { let first = undefined; return new Promise((resolve, reject) => { array.some((item, i) => { if (callback(item, i, array)) { first = item; return true; } return false; }); resolve(first); }); }); } exports.asyncWhich = asyncWhich; function asyncWhichFails(array, callback) { return __awaiter(this, void 0, void 0, function* () { let first = undefined; return new Promise((resolve, reject) => { array.some((item, i) => { if (!callback(item, i, array)) { first = item; return true; } return false; }); resolve(first); }); }); } exports.asyncWhichFails = asyncWhichFails; function asyncEvery(array, callback) { return __awaiter(this, void 0, void 0, function* () { return Promise.all(array.map(callback)).then((values) => values.every((v) => v)); }); } exports.asyncEvery = asyncEvery; function asyncFilter(array, callback) { return __awaiter(this, void 0, void 0, function* () { const results = yield Promise.all(array.map(callback)); return array.filter((_v, index) => results[index]); }); } exports.asyncFilter = asyncFilter; const asyncMap = (array, callback) => __awaiter(void 0, void 0, void 0, function* () { return Promise.all(array.map(callback)); }); exports.asyncMap = asyncMap; const asyncFlatMap = (array, callback) => __awaiter(void 0, void 0, void 0, function* () { const values = yield exports.asyncMap(array, callback); return [].concat(...values); }); exports.asyncFlatMap = asyncFlatMap; const asyncMapToObject = (array, callback) => __awaiter(void 0, void 0, void 0, function* () { const results = yield exports.asyncMap(array, callback); return array.reduce((map, key, i) => { map[key] = results[i]; return map; }, {}); }); exports.asyncMapToObject = asyncMapToObject; function asyncNone(array, callback) { return __awaiter(this, void 0, void 0, function* () { return Promise.all(array.map(callback)).then((values) => !values.some((v) => v)); }); } exports.asyncNone = asyncNone; function asyncSome(array, callback) { return __awaiter(this, void 0, void 0, function* () { return Promise.all(array.map(callback)).then((values) => values.some((v) => v)); }); } exports.asyncSome = asyncSome; const flatten = (items) => { return [].concat(...Object.values(items)); }; exports.flatten = flatten; const arrayEquals = (a, b) => { return (Array.isArray(a) && Array.isArray(b) && a.length === b.length && a.every((val, index) => val == b[index])); }; exports.arrayEquals = arrayEquals; const arrayExactly = (a, b) => { return (Array.isArray(a) && Array.isArray(b) && a.length === b.length && a.every((val, index) => val === b[index])); }; exports.arrayExactly = arrayExactly; function normalizePath(uri) { if (uri) { uri = uri.endsWith(path.sep) ? uri : uri + path.sep; } return uri; } exports.normalizePath = normalizePath; function ensureFolderExists(path) { if (!fs.existsSync(path)) { fs.mkdirSync(path, { recursive: true }); } } exports.ensureFolderExists = ensureFolderExists; function emptyFolder(folderPath) { return new Promise((resolve, reject) => { folderPath = path.resolve(folderPath); ensureFolderExists(folderPath); fs.readdir(folderPath, (err, files) => { if (err) reject(err); const promises = []; for (const file of files) { promises.push(fs.promises.unlink(path.join(folderPath, file))); } Promise.all(promises) .then(() => { resolve(); }) .catch((err) => { reject(err); }); }); }); } exports.emptyFolder = emptyFolder; function exitProcess(passed) { process.exit(passed ? 0 : 1); } exports.exitProcess = exitProcess; function getMessageAndCallbackFromOverloading(a, b, defaultMessage = "Untitled") { const message = typeof a == "string" ? a : defaultMessage; const callback = (() => { if (typeof b == "function") { return b; } else if (typeof a == "function") { return a; } else { return () => { }; } })(); const scenario = (() => { if (toType(a) == "scenario") { return a; } else if (toType(b) == "scenario") { return b; } return undefined; })(); return { isSubScenario: !!(a || b), message: message, callback: callback, scenario: scenario, }; } exports.getMessageAndCallbackFromOverloading = getMessageAndCallbackFromOverloading; const deepEqual = (thisValue, thatValue) => { try { nodeAssert.deepEqual(thisValue, thatValue); return true; } catch (ex) { return false; } }; exports.deepEqual = deepEqual; const deepStrictEqual = (thisValue, thatValue) => { try { nodeAssert.deepStrictEqual(thisValue, thatValue); return true; } catch (ex) { return false; } }; exports.deepStrictEqual = deepStrictEqual; const objectContains = (thisValue, thatValue) => { return Object.keys(thatValue).every((key) => thatValue[key] == thisValue[key]); }; exports.objectContains = objectContains; const objectContainsKeys = (thisValue, keys) => { return exports.arrayify(keys) .map((val) => String(val)) .every((val) => typeof thisValue[val] !== "undefined"); }; exports.objectContainsKeys = objectContainsKeys; const firstIn = (obj) => { const type = toType(obj); try { if (type == "array") { return obj[0]; } else if (type == "object") { return obj[Object.keys(obj)[0]]; } return String(obj).substr(0, 1); } catch (ex) { return null; } }; exports.firstIn = firstIn; const middleIn = (obj) => { const type = toType(obj); try { if (type == "array") { const i = Math.floor(obj.length / 2); return obj[i]; } else if (type == "object") { const keys = Object.keys(obj); const i = Math.floor(keys.length / 2); return obj[keys[i]]; } const i = Math.floor(String(obj).length / 2); return String(obj).substr(i, 1); } catch (ex) { return null; } }; exports.middleIn = middleIn; const lastIn = (obj) => { const type = toType(obj); try { if (type == "array") { return obj[obj.length - 1]; } else if (type == "object") { const keys = Object.keys(obj); return obj[keys[keys.length - 1]]; } return String(obj).substr(-1, 1); } catch (ex) { return null; } }; exports.lastIn = lastIn; const random = (min, max) => { return Math.random() * (max - min) + min; }; exports.random = random; const randomInt = (min, max) => { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min + 1) + min); }; exports.randomInt = randomInt; const randomIn = (obj) => { const type = toType(obj); try { if (type == "array") { const i = exports.randomInt(0, obj.length - 1); return obj[i]; } else if (type == "object") { const keys = Object.keys(obj); const i = exports.randomInt(0, keys.length - 1); return obj[keys[i]]; } const i = exports.randomInt(0, String(obj).length - 1); return String(obj).substr(i, 1); } catch (ex) { return null; } }; exports.randomIn = randomIn; const cast = (val) => { return val; }; exports.cast = cast; //# sourceMappingURL=util.js.map