UNPKG

wdio-docker-service

Version:

WebdriverIO service to start and stop docker container (for Selenium and more)

76 lines (59 loc) 1.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; exports.sanitizeValue = sanitizeValue; exports.serializeOption = serializeOption; exports.serializeOptions = serializeOptions; var _camelToDash = _interopRequireDefault(require("./camelToDash")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const RX_SPACES = /(\s)/g; const RX_IS_ESCAPED = /^(["'])([^"']+)(["'])$/; /** * @param {Object} opt Options to serialize * @return {Array} */ function serializeOptions(opt) { return Object.keys(opt).reduce((acc, key) => { const fixedKey = (0, _camelToDash.default)(key); const value = sanitizeValue(opt[key]); const option = serializeOption(fixedKey, value); if (option) { if (Array.isArray(option)) { return acc.concat(option); } acc.push(option); } return acc; }, []); } /** * @param {String} key * @param {*} value * @return {Array} */ function serializeOption(key, value) { const prefix = key.length > 1 ? '--' : '-'; if (typeof value === 'boolean' && value) { return [`${prefix}${key}`]; } if (typeof value === 'string') { return [`${prefix}${key}`, `${value}`]; } if (Array.isArray(value)) { return value.reduce((acc, item) => acc.concat([`${prefix}${key}`, `${item}`]), []); } } /** * @param {*} value * @return {void | string | *} */ function sanitizeValue(value) { if (typeof value !== 'string' || RX_IS_ESCAPED.test(value)) { return value; } return process.platform === 'win32' ? value : value.replace(RX_SPACES, '\\ '); } var _default = serializeOptions; exports.default = _default;