UNPKG

@whisklabs/grpc

Version:

gRPC generator and http library for typescript

76 lines 3.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.insertOption = exports.semicolon = exports.check = exports.ch = exports.cutStr = exports.cut = void 0; var typeguards_1 = require("@whisklabs/typeguards"); var thrower_1 = require("./thrower"); var cut = function (arr, count) { return arr.splice(0, count); }; exports.cut = cut; var cutStr = function (str) { var _a; return (_a = str === null || str === void 0 ? void 0 : str.slice(1, -1)) !== null && _a !== void 0 ? _a : ''; }; exports.cutStr = cutStr; var ch = function (rule, config) { return ({ rule: rule, config: config }); }; exports.ch = ch; function check(_a) { var _b; var type = _a.type, rules = _a.rules, tokens = _a.tokens; var errors = []; var results = []; var range = tokens.slice(0, rules.length); var iToken = 0; for (var i = 0; i < rules.length; i++) { var _c = rules[i], rule = _c.rule, config = _c.config; var val = Array.isArray(rule) ? rule : [rule]; var token = (_b = tokens[(config === null || config === void 0 ? void 0 : config.strict) === true ? i : iToken]) !== null && _b !== void 0 ? _b : ''; var valid = false; for (var _i = 0, val_1 = val; _i < val_1.length; _i++) { var validator = val_1[_i]; if (!valid) { valid = typeof validator === 'function' ? validator(token) : token === validator; } } if ((config === null || config === void 0 ? void 0 : config.result) === true) { results.push(valid ? token : ''); } if (!valid && (config === null || config === void 0 ? void 0 : config.ignore) !== true) { errors.push(["Token \"".concat(token, "\" not equal \"").concat(val.join(','), "\""), i]); } if (valid || (!valid && (config === null || config === void 0 ? void 0 : config.ignore) !== true)) { iToken++; } } if (errors.length > 0 && (0, typeguards_1.isString)(type)) { errors.push([range.join(', '), 0]); throw new thrower_1.Thrower(type, errors); } return { errors: errors, results: results, range: range, len: iToken }; } exports.check = check; var semicolon = function (tokens) { if (tokens[0] === ';') { tokens.shift(); } }; exports.semicolon = semicolon; function insertOption(result, field, out) { if (!(0, typeguards_1.isText)(field)) { throw new thrower_1.Thrower('insert option', [['No any field name', 0]]); } if ((0, typeguards_1.isPresent)(result[field])) { if (Array.isArray(result[field])) { if (Array.isArray(out)) { result[field] = result[field].concat(out); } else { result[field].push(out); } } else { result[field] = [result[field]]; result[field].push(out); } } else { result[field] = out; } } exports.insertOption = insertOption; //# sourceMappingURL=utils.js.map