salix/loopback/util/orderBySearch.js

29 lines
928 B
JavaScript

exports.orderBySearch = function(results, property, searchValue) {
return results.sort((a, b) => {
const aStartsWithSearch = a[property].startsWith(searchValue);
const bStartsWithSearch = b[property].startsWith(searchValue);
if (aStartsWithSearch && !bStartsWithSearch) return -1;
if (!aStartsWithSearch && bStartsWithSearch) return 1;
return a[property].localeCompare(b[property]);
});
};
exports.findLikeKeys = function(where) {
const likeKeys = [];
function searchLike(obj, parentKey = '') {
if (typeof obj !== 'object' || obj === null) return;
for (const key in obj) {
if (key === 'like')
likeKeys.push({property: parentKey, value: obj[key].replace(/%/g, '')});
else if (typeof obj[key] === 'object')
searchLike(obj[key], key);
}
}
searchLike(where);
return likeKeys;
};