UNPKG

flagpole

Version:

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

94 lines 3.24 kB
"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 cheerio = require('cheerio'); function isNullOrUndefined(obj) { return (typeof obj === "undefined" || obj === null); } exports.isNullOrUndefined = isNullOrUndefined; function toType(obj) { if (typeof obj === 'undefined') { return 'undefined'; } else if (obj === null) { return 'null'; } else if (obj === NaN) { return 'nan'; } else if (obj instanceof 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 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 fs = require('fs'); const tmp = require('tmp'); const tmpObj = tmp.fileSync({ postfix: '.html' }); const filePath = tmpObj.name; console.log(filePath); fs.writeFileSync(filePath, content); yield open(filePath); return filePath; }); } exports.openInBrowser = openInBrowser; function runAsync(callback, delay = 1) { setTimeout(callback, delay); } exports.runAsync = runAsync; function asyncForEach(array, callback) { return new Promise(resolve => { const promises = []; for (let i = 0; i < array.length; i++) { promises.push(callback(array[i], i, array)); } Promise.all(promises) .then(() => { resolve(); }); }); } exports.asyncForEach = asyncForEach; function normalizePath(path) { if (path) { path = (path.match(/\/$/) ? path : path + '/'); } return path; } exports.normalizePath = normalizePath; function exitProcess(passed) { process.exit(passed ? 0 : 1); } exports.exitProcess = exitProcess; //# sourceMappingURL=util.js.map