-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: transform filter output value + enhanced typing for filter(s)
- Loading branch information
Showing
6 changed files
with
83 additions
and
33 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
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
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
File renamed without changes.
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,46 @@ | ||
/* | ||
* Copyright (c) 2022. | ||
* Author Peter Placzek (tada5hi) | ||
* For the full copyright and license information, | ||
* view the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
import { FilterValueSimple } from '../type'; | ||
|
||
export function transformFilterValue<T>(input: FilterValueSimple) : FilterValueSimple { | ||
if (Array.isArray(input)) { | ||
for (let i = 0; i < input.length; i++) { | ||
input[i] = transformFilterValue(input[i]) as string | number | boolean; | ||
} | ||
|
||
return (input as unknown[]) | ||
.filter((n) => n === 0 || !!n) as FilterValueSimple; | ||
} | ||
|
||
if (typeof input === 'undefined' || input === null) { | ||
return null; | ||
} | ||
|
||
if (typeof input === 'string') { | ||
const lower = input.trim().toLowerCase(); | ||
|
||
if (lower === 'true') { | ||
return true; | ||
} | ||
|
||
if (lower === 'false') { | ||
return false; | ||
} | ||
|
||
if (lower === 'null') { | ||
return null; | ||
} | ||
|
||
const num = Number(input); | ||
if (!Number.isNaN(num)) { | ||
return num; | ||
} | ||
} | ||
|
||
return input; | ||
} |
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