-
Notifications
You must be signed in to change notification settings - Fork 1
/
autocomplete.js
76 lines (68 loc) · 2.56 KB
/
autocomplete.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const parsers = require("./parsers");
// const { helper } = require("./helpers");
const awsDyanmoDbService = require('./awsDyanmoDbService');
const MAX_RESULTS = 25;
// auto complete helper methods
function mapAutoParams(autoParams){
const params = {};
autoParams.forEach(param => {
params[param.name] = parsers.autocomplete(param.value);
});
return params;
}
/***
* @returns {[{id, value}]} filtered result items
***/
function handleResult(result, query, keyField, valField){
if (!result || result.length == 0) return [];
if (!keyField) {
keyField = "id";
valField = "name";
}
const items = result.map(item => getAutoResult(item[keyField], item[valField]));
return filterItems(items, query);
}
/***
* @returns {{id, value}} formatted autocomplete item
***/
function getAutoResult(id, value) {
return {
id: id || value,
value: value || id
};
}
function filterItems(items, query){
if (query){
const qWords = query.split(/[. ]/g).map(word => word.toLowerCase()); // split by '.' or ' ' and make lower case
items = items.filter(item => qWords.every(word => item.value.toLowerCase().includes(word)));
items = items.sort((word1, word2) => word1.value.toLowerCase().indexOf(qWords[0]) - word2.value.toLowerCase().indexOf(qWords[0]));
}
return items
// const itemList = items.filter(item=> item.toLowerCase().includes(query.toLowerCase()))
// return itemList
}
function listAuto(listFuncName, fields) {
return async (query, pluginSettings, triggerParameters) => {
try {
const settings = mapAutoParams(pluginSettings);
const params = mapAutoParams(triggerParameters);
const client = awsDyanmoDbService.from({
region: parsers.autocomplete(params.region) || parsers.string(settings.region) /* Change to correct parser */,
accessKeyId: parsers.string(params.accessKeyId) || parsers.string(settings.accessKeyId)/* Change to correct parser */,
secretAccessKey: parsers.string(params.secretAccessKey) || parsers.string(settings.secretAccessKey)/* Change to correct parser */,
});
const result = await client[listFuncName]({query: parsers.string(query), ...params});
const items = handleResult(result, query, ...fields);
return items;
}
catch (err) {
if (typeof err === 'string') throw err;
throw err.message || JSON.stringify(err);
}
}
}
module.exports = {
getRegions: listAuto("getRegions", ["RegionName", "RegionName"]),
getTable: listAuto("getTables", ["TableName", "TableName"]),
getBackups: listAuto("getBackups", ["BackupName", "BackupName"])
}