@shencom/api
Version:
shencom api group
125 lines (111 loc) • 4.23 kB
text/typescript
export const sortType = ['ASC', 'DESC'] as const;
export type SortType = (typeof sortType)[number];
export interface ISorts<P extends string = string> {
/** 筛选字段 */
orderField: P;
/** 筛选类型 */
orderType: SortType | (string & {});
}
export type IIndexSorts<P extends string = string> = ISorts<P>[];
/**
* 排序参数生成格式方法
*
* @export
* @param {string} prop 排序字段
* @param {SortType} [type] 排序类型 (默认: DESC)
* @param {[string, SortType?][]} params 同 [prop, orderType]
*
* @example
* ```js
* ApiSortsConstruct('aaa') // { orderField: 'aaa', orderType: 'DESC' }
* ApiSortsConstruct('aaa', 'ASC') // { orderField: 'aaa', orderType: 'ASC' }
* ApiSortsConstruct([['aaa', 'ASC'], ['bbb']]) // [{ orderField: 'aaa', orderType: 'ASC' }, { orderField: 'bbb', orderType: 'DESC' }]
* ```
* @returns {IIndexSorts}
*/
export function ApiSortsConstruct(prop: string, type?: SortType): IIndexSorts;
export function ApiSortsConstruct(params: [string, SortType?][]): IIndexSorts;
export function ApiSortsConstruct(params: [string, string?][]): IIndexSorts;
export function ApiSortsConstruct(prop: any, type?: any): IIndexSorts {
if (Array.isArray(prop)) {
return prop.map((item) => ({ orderField: item[0], orderType: item[1] || 'DESC' }));
}
return [{ orderField: prop, orderType: type || 'DESC' }];
}
/**
* @deprecated 请使用 ApiSortsConstruct 代替
*/
export const ApiSortsHandler = ApiSortsConstruct;
/**
* 将排序对象转换为键值对格式
*
* @export
* @param {{ orderField: string, orderType: SortType }} sort 排序对象
* @param {Array<{ orderField: string, orderType: SortType }>} sorts 排序对象数组
*
* @example
* ```js
* ApiSortsDestructure({ orderField: 'aaa', orderType: 'DESC' }) // { "aaa": "DESC" }
* ApiSortsDestructure([{ orderField: 'aaa', orderType: 'DESC' }, { orderField: 'bbb', orderType: 'ASC' }])
* // { "aaa": "DESC", "bbb": "ASC" }
* ```
* @returns {Record<string, SortType>} 键值对格式的排序对象
*/
export function ApiSortsDestructure(sort: ISorts): Record<string, SortType>;
export function ApiSortsDestructure(sorts: ISorts[]): Record<string, SortType>;
export function ApiSortsDestructure(param: any): Record<string, SortType> {
const result: Record<string, SortType> = {};
if (Array.isArray(param)) {
param.forEach((item) => {
if (item && item.orderField) {
result[item.orderField] = item.orderType || 'DESC';
}
});
} else if (param && param.orderField) {
result[param.orderField] = param.orderType || 'DESC';
}
return result;
}
/**
* 从排序数组中删除指定字段的排序条件
*
* @export
* @param {IIndexSorts} sorts 排序条件数组
* @param {string | string[]} deleteProps 要删除的字段名(单个字符串或字符串数组)
*
* @example
* ```js
* // 删除单个字段的排序
* const sorts = [{ orderField: 'createTime', orderType: 'DESC' }, { orderField: 'updateTime', orderType: 'ASC' }];
* ApiSortsDelete(sorts, 'createTime'); // [{ orderField: 'updateTime', orderType: 'ASC' }]
*
* // 删除多个字段的排序
* const sorts = [
* { orderField: 'createTime', orderType: 'DESC' },
* { orderField: 'updateTime', orderType: 'ASC' },
* { orderField: 'name', orderType: 'ASC' }
* ];
* ApiSortsDelete(sorts, ['createTime', 'name']); // [{ orderField: 'updateTime', orderType: 'ASC' }]
*
* // 当结果为空数组时返回null
* const sorts = [{ orderField: 'createTime', orderType: 'DESC' }];
* ApiSortsDelete(sorts, 'createTime'); // null
* ```
*
* @returns {IIndexSorts | null} 删除后的排序数组,如果为空则返回null
*/
export function ApiSortsDelete(
sorts: IIndexSorts,
deleteProps: string | string[],
): IIndexSorts | null {
if (!Array.isArray(sorts) || sorts.length === 0) return sorts;
if (!deleteProps || deleteProps.length === 0) return sorts;
const targetDeleteProps = Array.isArray(deleteProps) ? deleteProps : [deleteProps];
targetDeleteProps.forEach((prop) => {
const index = sorts.findIndex((item) => item.orderField === prop);
if (index > -1) {
sorts.splice(index, 1);
}
});
return sorts.length === 0 ? null : sorts;
}