-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CP-3166] Refactored dataProviderSort to use type-safe comparison and…
… modular helper functions
- Loading branch information
Showing
3 changed files
with
80 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
libs/generic-view/utils/src/lib/data-provider-helpers/data-provider-sort.helpers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* Copyright (c) Mudita sp. z o.o. All rights reserved. | ||
* For licensing, see https://github.com/mudita/mudita-center/blob/master/LICENSE.md | ||
*/ | ||
|
||
import { cloneDeep } from "lodash" | ||
import { DataProviderSortConfig, sortDirection, sortOrderingPatterns } from "device/models" | ||
import { stringToRegex } from "./string-to-regex" | ||
|
||
export const sortByPriority = (sortConfigs: DataProviderSortConfig) => { | ||
if (!sortConfigs) return [] | ||
return cloneDeep(sortConfigs).sort((a, b) => a.priority - b.priority) | ||
} | ||
|
||
export const compareFields = ( | ||
fieldA: string, | ||
fieldB: string, | ||
direction: sortDirection | ||
) => { | ||
return direction === "asc" | ||
? fieldA.localeCompare(fieldB) | ||
: fieldB.localeCompare(fieldA) | ||
} | ||
|
||
export const compareWithOrderingPatterns = ( | ||
fieldA: string, | ||
fieldB: string, | ||
patterns: sortOrderingPatterns, | ||
direction: sortDirection | ||
) => { | ||
const directionMultiplier = direction === "asc" ? 1 : -1 | ||
|
||
for (const pattern of patterns) { | ||
const regex = stringToRegex(pattern) | ||
const matchA = regex.test(fieldA) | ||
const matchB = regex.test(fieldB) | ||
|
||
if (matchA && !matchB) return -1 * directionMultiplier | ||
if (!matchA && matchB) return 1 * directionMultiplier | ||
} | ||
return 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters