This commit is contained in:
2026-10-08 18:18:21 +08:00
parent 9cc1ff546c
commit 1e3095c05e
1911 changed files with 349551 additions and 0 deletions
@@ -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;