UNPKG

@ply-ct/ply

Version:

REST API Automated Testing

206 lines 7.48 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.plyVersion = exports.base64ToUintArray = exports.uintArrayToBase64 = exports.isBinary = exports.header = exports.writeablePath = exports.writeableFileName = exports.writeableName = exports.isJson = exports.fixEol = exports.fwdSlashes = exports.lineComment = exports.lines = exports.newlines = exports.timeparse = exports.genId = exports.nanoTime = exports.timestamp = void 0; const process = __importStar(require("process")); const fs = __importStar(require("fs")); /** * Turn a date into a timestamp based on the OS locale * Note: Node/V8 bug causes hour = 24 (https://github.com/nodejs/node/issues/33089) * eg: 7/3/2021, 24:52:33:347 */ function timestamp(date, withTimeZone = false, withMillis = true) { let stamp = `${date.toLocaleString(undefined, { hour12: false })}`; if (withMillis) stamp += ':' + ('' + date.getMilliseconds()).padStart(3, '0'); if (withTimeZone) stamp += date.toTimeString().substring(date.toTimeString().indexOf(' ')); return stamp; } exports.timestamp = timestamp; /** * time in nanoseconds */ function nanoTime() { const hrTime = process.hrtime(); return hrTime[0] * 1000000000 + hrTime[1]; } exports.nanoTime = nanoTime; function genId() { return nanoTime().toString(16); } exports.genId = genId; /** * Expected format is per timestamp() -- * TODO: Only works for these two locale formats: * MM/DD/YYYY, HH:mm:ss * DD/MM/YYYY, HH:mm:ss (not tested actually) */ function timeparse(time) { const parser = /(\d+?)\/(\d+?)\/(\d+?), (\d+?):(\d+?):(\d+?):(\d+?)$/; const match = time.match(parser); if (match) { const formatObj = new Intl.DateTimeFormat().formatToParts(new Date()); const first = formatObj[Object.keys(formatObj)[0]].type; const month = first === 'month' ? 1 : 2; const day = month === 1 ? 2 : 1; return new Date(parseInt(match[3]), // year parseInt(match[month]) - 1, // monthIndex parseInt(match[day]), // day parseInt(match[4]), // hours parseInt(match[5]), // minutes parseInt(match[6]), // seconds parseInt(match[7]) // millis ); } } exports.timeparse = timeparse; /** * Remove windows newline characters (\r) */ function newlines(input) { return input.replace(/\r/g, ''); } exports.newlines = newlines; /** * split a string into an array of lines, ignoring escaped */ function lines(input) { return input.split(/\r?\n/); } exports.lines = lines; /** * Return the trailing comment portion of a line. Only works with no * embedded comment tokens (in string content, etc). */ function lineComment(line, token = '#') { const hash = line.indexOf(token); if (hash >= 0 && hash < line.length - 1) { return line.substring(hash + 1).trim(); } } exports.lineComment = lineComment; /** * substitute a forward slash for all backslashes */ function fwdSlashes(path) { return path.replace(/\\/g, '/'); } exports.fwdSlashes = fwdSlashes; function fixEol(path) { return path.replace(/\r/g, ''); } exports.fixEol = fixEol; function isJson(str) { return (str.startsWith('{') && str.endsWith('}')) || (str.startsWith('[') && str.endsWith(']')); } exports.isJson = isJson; function writeableName(name) { return name .replace(/</g, '-') .replace(/>/g, '-') .replace(/:/g, '-') .replace(/"/g, '-') .replace(/\|/g, '-') .replace(/\?/g, '-') .replace(/\*/g, '-'); } exports.writeableName = writeableName; function writeableFileName(file) { return writeableName(file).replace(/\//g, '-').replace(/\\/g, '-'); } exports.writeableFileName = writeableFileName; function writeablePath(path) { return writeableName(path).replace(/ \/ /g, '/'); } exports.writeablePath = writeablePath; function header(headers, name) { const key = name.toLowerCase(); const match = Object.keys(headers).find((h) => h.toLowerCase() === key); if (match) return [match, headers[match]]; } exports.header = header; function isBinary(headers, options) { if (!headers || !(options === null || options === void 0 ? void 0 : options.binaryMediaTypes)) return false; const contentTypeKey = Object.keys(headers).find((k) => k.toLowerCase() === 'content-type'); if (!contentTypeKey) return false; const contentType = headers[contentTypeKey]; return !!options.binaryMediaTypes.find((mt) => mt === contentType || mt.startsWith(`${contentType};`)); } exports.isBinary = isBinary; function uintArrayToBase64(uintArray) { return Buffer.from(uintArray).toString('base64'); } exports.uintArrayToBase64 = uintArrayToBase64; function base64ToUintArray(str) { return new Uint8Array(Buffer.from(str, 'base64')); } exports.base64ToUintArray = base64ToUintArray; let cachedPlyVersion = ''; function plyVersion() { return new Promise((resolve, reject) => { if (cachedPlyVersion) { resolve(cachedPlyVersion); } else { try { const plyDir = `${process.cwd()}/node_modules/@ply-ct/ply`; if (fs.existsSync(`${plyDir}/package.json`)) { fs.promises .readFile(`${plyDir}/package.json`, { encoding: 'utf-8' }) .then((contents) => { cachedPlyVersion = JSON.parse(contents).version; resolve(cachedPlyVersion); }); } else if (fs.existsSync(`${process.cwd()}/package.json`)) { fs.promises .readFile(`${process.cwd()}/package.json`, { encoding: 'utf-8' }) .then((contents) => { const pkgJson = JSON.parse(contents); if (pkgJson.name === '@ply-ct/ply') { cachedPlyVersion = pkgJson.version; resolve(cachedPlyVersion); } else { resolve(''); } }); } else { resolve(''); } } catch (err) { reject(err); } } }); } exports.plyVersion = plyVersion; //# sourceMappingURL=util.js.map