UNPKG

@fimbul/wotan

Version:

Pluggable TypeScript and JavaScript linter

81 lines 3.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.OptionParser = void 0; const debug = require("debug"); const ymir_1 = require("@fimbul/ymir"); const log = debug('wotan:optparse'); var OptionParser; (function (OptionParser) { function parse(options, specs, config) { const result = {}; let name; for (name of Object.keys(specs)) result[name] = specs[name](options && options[name], reportMismatch); if (config.exhaustive && options) for (const key of Object.keys(options)) if (specs[key] === undefined) report(`Unexpected option '${key}'.`); return result; function reportMismatch(type) { report(`Expected a value of type '${type}' for option '${name}'.`); } function report(message) { message = config.context + ': ' + message; if (config.validate) throw new ymir_1.ConfigurationError(message); log(message); } } OptionParser.parse = parse; let Transform; (function (Transform) { function withDefault(parseFn, defaultValue) { return (value, report) => { const result = parseFn(value, report); return result === undefined ? defaultValue : result; }; } Transform.withDefault = withDefault; function noDefault(parseFn) { return (value, report) => { return value === undefined ? undefined : parseFn(value, report); }; } Transform.noDefault = noDefault; function map(parseFn, cb) { return (value, report) => { var _a; return (_a = parseFn(value, report)) === null || _a === void 0 ? void 0 : _a.map(cb); }; } Transform.map = map; function transform(parseFn, cb) { return (value, report) => cb(parseFn(value, report)); } Transform.transform = transform; })(Transform = OptionParser.Transform || (OptionParser.Transform = {})); let Factory; (function (Factory) { function parsePrimitive(...types) { return (value, report) => { for (const type of types) if (typeof value === type) return value; if (value !== undefined) report(types.join(' | ')); return; }; } Factory.parsePrimitive = parsePrimitive; function parsePrimitiveOrArray(type) { return (value, report) => { if (Array.isArray(value) && value.every((v) => typeof v === type)) return value; if (typeof value === 'string') return [value]; if (value !== undefined) report('string | string[]'); return; }; } Factory.parsePrimitiveOrArray = parsePrimitiveOrArray; })(Factory = OptionParser.Factory || (OptionParser.Factory = {})); })(OptionParser = exports.OptionParser || (exports.OptionParser = {})); //# sourceMappingURL=optparse.js.map