Skip to content

Commit

Permalink
refactor: use const/let, improve function formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
StephaneBour committed Jan 15, 2024
1 parent e60dc69 commit 258b510
Showing 1 changed file with 18 additions and 21 deletions.
39 changes: 18 additions & 21 deletions src/kb.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
(function () {

var global = window;
var GLOBAL_AUTOCOMPLETE_RULES = {}, ES_SCHEME_BY_ENDPOINT = {};
const global = window;
let GLOBAL_AUTOCOMPLETE_RULES = {}, ES_SCHEME_BY_ENDPOINT = {};

function escapeRegex(text) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}

function addGlobalAutocompleteRules(parentNode, rules) {
if (typeof parentNode == 'object') {
parentNode.forEach(function(element) {
parentNode.forEach(function (element) {
console.log(element);
});
} else {
Expand All @@ -27,13 +26,13 @@
description.endpoint_autocomplete = [endpoint];

if (!description.match) {
var l = $.map(description.endpoint_autocomplete, escapeRegex);
const l = $.map(description.endpoint_autocomplete, escapeRegex);
description.match = "(?:" + l.join(")|(?:") + ")";
}

if (typeof description.match == "string") description.match = new RegExp(description.match);

var copiedDescription = {};
const copiedDescription = {};
$.extend(copiedDescription, description);
copiedDescription._id = endpoint;

Expand All @@ -45,20 +44,20 @@
}

function getEndpointsForIndicesTypesAndId(indices, types, id) {
var ret = [];
var index_mode = "none";
const ret = [];
let index_mode = "none";
if (indices && indices.length > 0) {
indices = sense.mappings.expandAliases(indices);
index_mode = typeof indices == "string" ? "single" : "multi";
}

var type_mode = "none";
let type_mode = "none";
if (types && types.length > 0) type_mode = types.length > 1 ? "multi" : "single";
var id_mode = "none";
let id_mode = "none";
if (id && id.length > 0) id_mode = "single";

for (var endpoint in ES_SCHEME_BY_ENDPOINT) {
var scheme = ES_SCHEME_BY_ENDPOINT[endpoint];
for (const endpoint in ES_SCHEME_BY_ENDPOINT) {
const scheme = ES_SCHEME_BY_ENDPOINT[endpoint];
switch (scheme.indices_mode) {
case "none":
if (index_mode !== "none") continue;
Expand Down Expand Up @@ -98,19 +97,19 @@
}

function getEndpointDescriptionByPath(path, indices, types, id) {
var endpoints = getEndpointsForIndicesTypesAndId(indices, types, id);
for (var i = 0; i < endpoints.length; i++) {
var scheme = ES_SCHEME_BY_ENDPOINT[endpoints[i]];
const endpoints = getEndpointsForIndicesTypesAndId(indices, types, id);
for (let i = 0; i < endpoints.length; i++) {
const scheme = ES_SCHEME_BY_ENDPOINT[endpoints[i]];
if (scheme.match.test(path || "")) return scheme;
}
return null;
}

function getEndpointAutocomplete(indices, types, id) {
var ret = [];
var endpoints = getEndpointsForIndicesTypesAndId(indices, types, id);
for (var i = 0; i < endpoints.length; i++) {
var scheme = ES_SCHEME_BY_ENDPOINT[endpoints[i]];
const ret = [];
const endpoints = getEndpointsForIndicesTypesAndId(indices, types, id);
for (let i = 0; i < endpoints.length; i++) {
const scheme = ES_SCHEME_BY_ENDPOINT[endpoints[i]];
ret.push.apply(ret, scheme.endpoint_autocomplete);
}
return ret;
Expand All @@ -130,6 +129,4 @@
global.sense.kb.getEndpointDescriptionByPath = getEndpointDescriptionByPath;
global.sense.kb.getEndpointDescriptionByEndpoint = getEndpointDescriptionByEndpoint;
global.sense.kb.clear = clear;


})();

0 comments on commit 258b510

Please sign in to comment.