UNPKG

@equantic/linq

Version:
29 lines (28 loc) 933 B
import { Sorting } from './Sorting.js'; import { SortingCollection } from './SortingCollection.js'; export class SortingParser { static parse(querySorter) { const result = new SortingCollection(); if (!querySorter) { return result; } if (!Array.isArray(querySorter)) { querySorter = [querySorter]; } for (const s of querySorter) { if (!s) { continue; } const arr = s.split(':'); const dir = arr.length === 1 ? 'asc' : arr[1].toLowerCase(); result.push(new Sorting(arr[0], dir)); } return result; } static toQueryString(sorting) { if (!sorting || sorting.length === 0) return undefined; const newSorting = new SortingCollection(sorting.map((s) => new Sorting(s.column, s.direction))); return newSorting.join(','); } }