UNPKG

@whisklabs/grpc

Version:

gRPC generator and http library for typescript

67 lines 2.68 kB
import { isPresent, isString, isText } from '@whisklabs/typeguards'; import { Thrower } from './thrower'; export var cut = function (arr, count) { return arr.splice(0, count); }; export var cutStr = function (str) { var _a; return (_a = str === null || str === void 0 ? void 0 : str.slice(1, -1)) !== null && _a !== void 0 ? _a : ''; }; export var ch = function (rule, config) { return ({ rule: rule, config: config }); }; export 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 && isString(type)) { errors.push([range.join(', '), 0]); throw new Thrower(type, errors); } return { errors: errors, results: results, range: range, len: iToken }; } export var semicolon = function (tokens) { if (tokens[0] === ';') { tokens.shift(); } }; export function insertOption(result, field, out) { if (!isText(field)) { throw new Thrower('insert option', [['No any field name', 0]]); } if (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; } } //# sourceMappingURL=utils.js.map