20261008
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
'use strict';
|
||||
|
||||
function normalizeSortType(type) {
|
||||
if (type === 'ascending') return 'asc';
|
||||
if (type === 'descending') return 'desc';
|
||||
type = typeof type === 'string' ? type.toLowerCase() : type;
|
||||
return ['asc', 'desc'].includes(type) ? type : '';
|
||||
}
|
||||
|
||||
function normalizeSortRule(sortRule, defaultSortArr, allowNames) {
|
||||
const allowSet = allowNames && allowNames.length ? new Set(allowNames) : null;
|
||||
const rules = Array.isArray(sortRule)
|
||||
? sortRule
|
||||
: (sortRule && typeof sortRule === 'object' ? [sortRule] : []);
|
||||
|
||||
const sortArr = rules
|
||||
.map(item => {
|
||||
const name = item && (item.name || item.prop || item.field || item.key);
|
||||
const type = normalizeSortType(item && (item.type || item.order || item.sort));
|
||||
if (!name || !type) return null;
|
||||
if (allowSet && !allowSet.has(name)) return null;
|
||||
return {name, type};
|
||||
})
|
||||
.filter(Boolean);
|
||||
|
||||
return sortArr.length ? sortArr : defaultSortArr;
|
||||
}
|
||||
|
||||
module.exports = normalizeSortRule;
|
||||
Reference in New Issue
Block a user