get function: added filter, test script modification
This commit is contained in:
+35
-3
@@ -324,7 +324,8 @@ function get(valueObject, callback) {
|
||||
.filter(func => ({}.hasOwnProperty.call(exports, func)))
|
||||
.map(func => {
|
||||
const params = valueObject[func].substring(valueObject[func].lastIndexOf('(') + 1, valueObject[func].lastIndexOf(')'));
|
||||
const funcWithoutParams = func.split('(')[0];
|
||||
let funcWithoutParams = func.indexOf(')') >= 0 ? func.split(')')[1].trim() : func;
|
||||
funcWithoutParams = func.indexOf('|') >= 0 ? func.split('|')[0].trim() : funcWithoutParams;
|
||||
if (params) {
|
||||
return exports[funcWithoutParams](params)
|
||||
} else {
|
||||
@@ -340,7 +341,22 @@ function get(valueObject, callback) {
|
||||
if (valueObject[key] === '*' || valueObject[key] === 'all') {
|
||||
result[key] = data[i];
|
||||
} else {
|
||||
const keys = valueObject[key].replace(/,/g, ' ').replace(/ +/g, ' ').split(' ');
|
||||
let keys = valueObject[key];
|
||||
// let params = '';
|
||||
let filter = '';
|
||||
let filterParts = [];
|
||||
// remove params
|
||||
if (keys.indexOf(')') >= 0) {
|
||||
keys = keys.split(')')[1].trim();
|
||||
}
|
||||
// extract filter and remove it from keys
|
||||
if (keys.indexOf('|') >= 0) {
|
||||
filter = keys.split('|')[1].trim();
|
||||
filterParts = filter.split(':');
|
||||
|
||||
keys = keys.split('|')[0].trim();
|
||||
}
|
||||
keys = keys.replace(/,/g, ' ').replace(/ +/g, ' ').split(' ');
|
||||
if (data[i]) {
|
||||
if (Array.isArray(data[i])) {
|
||||
// result is in an array, go through all elements of array and pick only the right ones
|
||||
@@ -352,7 +368,23 @@ function get(valueObject, callback) {
|
||||
partialRes[k] = element[k];
|
||||
}
|
||||
});
|
||||
partialArray.push(partialRes);
|
||||
if (filter && filterParts.length === 2) {
|
||||
if ({}.hasOwnProperty.call(partialRes, filterParts[0].trim())) {
|
||||
const val = partialRes[filterParts[0].trim()];
|
||||
if (typeof val == 'number') {
|
||||
if (val === parseFloat(filterParts[1].trim())) {
|
||||
partialArray.push(partialRes);
|
||||
}
|
||||
} else if (typeof val == 'string') {
|
||||
if (val.toLowerCase() === filterParts[1].trim().toLowerCase()) {
|
||||
partialArray.push(partialRes);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
partialArray.push(partialRes);
|
||||
}
|
||||
|
||||
});
|
||||
result[key] = partialArray;
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user