UNPKG

@configurator/ravendb

Version:
92 lines 4.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.IndexDefinitionHelper = void 0; const Exceptions_1 = require("../../Exceptions"); const StringUtil_1 = require("../../Utility/StringUtil"); const XRegExpAll = require("xregexp"); const XRegExp = XRegExpAll.default || XRegExpAll; const COMMENT_REGEX = XRegExp("(?:/\\*(?:[^*]|(?:\\*+[^*/]))*\\*+/)|(?://.*)", "gm"); class IndexDefinitionHelper { static detectStaticIndexType(map, reduce) { if (!map) { (0, Exceptions_1.throwError)("InvalidArgumentException", "Index definitions contains no Maps"); } map = IndexDefinitionHelper._stripComments(map); map = IndexDefinitionHelper._unifyWhiteSpace(map); const mapLower = map.toLocaleLowerCase(); if (mapLower.startsWith("from") || mapLower.startsWith("docs") || (mapLower.startsWith("timeseries") && !mapLower.startsWith("timeseries.map")) || (mapLower.startsWith("counters") && !mapLower.startsWith("counters.map"))) { if (StringUtil_1.StringUtil.isNullOrWhitespace(reduce)) { return "Map"; } return "MapReduce"; } if (StringUtil_1.StringUtil.isNullOrWhitespace(reduce)) { return "JavaScriptMap"; } return "JavaScriptMapReduce"; } static extractEnumNotation(functionBody) { functionBody = functionBody.trim(); if (functionBody.endsWith(";")) { return IndexDefinitionHelper.extractEnumNotation(functionBody.substring(0, functionBody.length - 1)); } if (functionBody.startsWith("return")) { return IndexDefinitionHelper.extractEnumNotation(functionBody.substring("return".length)); } const openBracketIdx = functionBody.indexOf("{"); const closeBracketIdx = functionBody.lastIndexOf("}"); if (openBracketIdx < closeBracketIdx && openBracketIdx !== -1 && closeBracketIdx !== -1) { const body = functionBody.substring(openBracketIdx + 1, closeBracketIdx); return IndexDefinitionHelper.extractEnumNotation(body); } const hasArrow = functionBody.indexOf("=>"); if (hasArrow !== -1) { return IndexDefinitionHelper.extractEnumNotation(functionBody.substring(hasArrow + 2)); } const openParentesis = functionBody.indexOf("("); const closeParentesis = functionBody.lastIndexOf(")"); if (openParentesis < closeParentesis && openParentesis !== -1 && closeParentesis !== -1) { const body = functionBody.substring(openParentesis + 1, closeParentesis); return IndexDefinitionHelper.extractEnumNotation(body); } return functionBody; } static detectStaticIndexSourceType(map) { if (StringUtil_1.StringUtil.isNullOrWhitespace(map)) { (0, Exceptions_1.throwError)("InvalidArgumentException", "Value cannot be null or whitespace."); } map = IndexDefinitionHelper._stripComments(map); map = IndexDefinitionHelper._unifyWhiteSpace(map); const mapLower = map.toLocaleLowerCase(); if (mapLower.startsWith("timeseries")) { return "TimeSeries"; } if (mapLower.startsWith("counters")) { return "Counters"; } if (mapLower.startsWith("from")) { const tokens = mapLower.split(" ", 4) .filter(x => !StringUtil_1.StringUtil.isNullOrEmpty(x)); if (tokens.length >= 4 && "in" === tokens[2].toLocaleLowerCase()) { if (tokens[3].startsWith("timeseries")) { return "TimeSeries"; } if (tokens[3].startsWith("counters")) { return "Counters"; } } } return "Documents"; } static _stripComments(input) { return input.replace(COMMENT_REGEX, "").trim(); } static _unifyWhiteSpace(input) { return input.replace(/(\s+)/g, " "); } } exports.IndexDefinitionHelper = IndexDefinitionHelper; //# sourceMappingURL=IndexDefinitionHelper.js.map