-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
122 lines (112 loc) · 4.74 KB
/
index.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const validateType = require('./src/utils/shared/functions/validateTypes.js');
const defaultFunctions = require('./src/utils/shared/functions/defaultFunctions.js');
const { IsNotObjectError } = require('./src/utils/shared/errors/IsNotObject.error.js');
const { doSearch, findLatestSearchedIds } = require('./src/main/search/search.js');
const { IsNotStringError } = require('./src/utils/shared/errors/IsNotString.error.js');
const similar = require('./src/main/search/findSimilarMatches.js');
const dataset = require('./src/main/dataSet/generateDataset.js');
/**
* This function generates a data set based on an array of objects. **ID field required on each object**.
* @param {Object} main Main object with options.
* @param {[object]} main.array Array of objects.
* @param {string} main.nameId Name of the identification field in your objects. (**\'id\' by default**)
* @param {number} main.wordSize Size of the words you want to filter to generate the tags. (**\'2\' by default**)
* @param {[string]} main.attributes Name of the attributes on the objects you need to generate the tags.
* @param {[string]} main.ignoreInTags The tags ignore any words in the object that look like any words in this array.
* @return Array of objects with an array of tags on each object in the array.
*/
function dataSetGenerate(main) {
try {
if (!validateType.isObject(main)) {
throw new IsNotObjectError('main')
}
return dataset.generate(main);
} catch (e) {
if (e.type && e.description)
console.error(`\n[${e.type}] - ${e.description}`);
else
console.error(e.message)
}
}
/**
* This function uses user input to search for objects generated by the data set.
* - by default, it takes the first attribute that is considered a string in the object.
* - Ignores identifier (ID) attributes and take the next attribute.
* @param {Object} obj Main object with options.
* @param {[object]} obj.data Array with the current objects.
* @param {string} obj.input User input in a search bar.
* @param {boolean} obj.returnAll When nothing is found: --- (**false by default**)
* - [**true**] changes the return to an array with all objects from dataset.
* - [**false**] changes the return to an empty array.
* @param {string} obj.filterByValue Value that the search will always give priority. (**\'empty string\' by default**)
* @return Returns an array with the found objects.
*/
function search(obj) {
try {
if (!validateType.isObject(obj)) {
throw new IsNotObjectError('obj')
}
if (!obj.data) {
throw new Error('dataset not found.');
}
if (obj.returnAll) {
if (typeof obj.returnAll != 'boolean') {
throw new Error('\'returnAll\' cannot be different from boolean type.');
}
} else {
obj.returnAll = false;
}
if (!validateType.isString(obj.input)) {
throw new IsNotStringError('input');
}
const field = (obj.input) ? defaultFunctions.removeSpecialCharStringArray(obj.input) : '';
obj.filterByValue = (obj.filterByValue) ? obj.filterByValue : '';
return doSearch(obj.data, obj.returnAll, obj.input, obj.filterByValue);
} catch (e) {
if (e.type && e.description) {
console.error(`\n[${e.type}] - ${e.description}`);
} else {
console.error(e.message)
}
}
}
/**
* This function returns an object with all the attributes of the dataset.
*/
function getDataset() {
return dataset.findDataset();
}
/**
* This function returns an object with the current values of 'min' and 'max'.
*/
function getSearchDistance() {
return similar.getMaxMinDistance();
}
/**
* This function allows changing the maximum and minimum tolerance that the search needs to bring results.
* The search result will be the max value or the distance value between 'max' and 'min'.
* @param min Minimum tolerance value. (**0.30 by default**).
* @param max Max tolerance value. (**0.85 by default**).
*/
function setSearchDistance(min, max) {
similar.setMaxMinDistance(min, max);
}
/**
* This function resets the distance values ('min' and 'max') to the default values.
*/
function resetDistance() {
similar.resetMaxMin();
}
/**
* This function returns an object with an array of ids found by the search.
*/
function getLatestSearchedIds() {
return findLatestSearchedIds();
}
module.exports.resetDistance = resetDistance;
module.exports.setSearchDistance = setSearchDistance;
module.exports.getSearchDistance = getSearchDistance;
module.exports.getDataset = getDataset;
module.exports.search = search;
module.exports.dataSetGenerate = dataSetGenerate;
module.exports.getLatestSearchedIds = getLatestSearchedIds;