-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
136 additions
and
48 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
packages/components/src/filters/filterByCategoricalFilter.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,26 @@ | ||
/** | ||
* @lineup-lite/components | ||
* https://github.com/sgratzl/lineup-lite | ||
* | ||
* Copyright (c) 2021 Samuel Gratzl <[email protected]> | ||
*/ | ||
|
||
import { ALWAYS_TRUE } from './utils'; | ||
|
||
/** | ||
* filter function by a set of filter values | ||
*/ | ||
export default function filterByCategoricalFilter( | ||
filterValue?: readonly string[] | null | ||
): (value?: string | null) => boolean { | ||
if (filterValue == null) { | ||
return ALWAYS_TRUE; | ||
} | ||
const lookup = new Set(filterValue); | ||
return (v) => { | ||
if (v == null) { | ||
return false; | ||
} | ||
return !lookup.has(v); | ||
}; | ||
} |
49 changes: 49 additions & 0 deletions
49
packages/components/src/filters/filterByCategoricalSetFilter.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,49 @@ | ||
/** | ||
* @lineup-lite/components | ||
* https://github.com/sgratzl/lineup-lite | ||
* | ||
* Copyright (c) 2021 Samuel Gratzl <[email protected]> | ||
*/ | ||
|
||
import type { FilterSetValue } from '../components'; | ||
import { ALWAYS_TRUE } from './utils'; | ||
|
||
/** | ||
* filter function by a set of filter values | ||
*/ | ||
export default function filterByCategoricalSetFilter( | ||
filterValue?: readonly FilterSetValue<string>[] | null | ||
): (value?: readonly string[] | ReadonlySet<string> | null) => boolean { | ||
if (filterValue == null) { | ||
return ALWAYS_TRUE; | ||
} | ||
const must: string[] = []; | ||
const mustNot: string[] = []; | ||
for (const v of filterValue) { | ||
if (v.value) { | ||
must.push(v.set); | ||
} else { | ||
mustNot.push(v.set); | ||
} | ||
} | ||
return (v) => { | ||
const hasV = v instanceof Set ? v : new Set(v); | ||
if (mustNot.length > 0) { | ||
// if any must not value is in the values -> false | ||
for (const mi of mustNot) { | ||
if (hasV.has(mi)) { | ||
return false; | ||
} | ||
} | ||
} | ||
if (must.length > 0) { | ||
// if any must value is not in the values -> false | ||
for (const mi of must) { | ||
if (!hasV.has(mi)) { | ||
return false; | ||
} | ||
} | ||
} | ||
return true; | ||
}; | ||
} |
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,28 @@ | ||
/** | ||
* @lineup-lite/components | ||
* https://github.com/sgratzl/lineup-lite | ||
* | ||
* Copyright (c) 2021 Samuel Gratzl <[email protected]> | ||
*/ | ||
|
||
import { ALWAYS_TRUE } from './utils'; | ||
|
||
/** | ||
* a range numeric filter | ||
*/ | ||
export default function filterByRangeFilter<T>(filterValue?: readonly [T, T] | null): (v?: T | null) => boolean { | ||
if (filterValue == null) { | ||
return ALWAYS_TRUE; | ||
} | ||
const matches = (v: T) => | ||
(filterValue[0] == null || v >= filterValue[0]) && (filterValue[1] == null || v <= filterValue[1]); | ||
return (v) => { | ||
if (v == null) { | ||
return false; | ||
} | ||
if (Array.isArray(v)) { | ||
return v.every((vi) => v != null && matches(vi)); | ||
} | ||
return matches(v); | ||
}; | ||
} |
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,11 @@ | ||
/** | ||
* @lineup-lite/components | ||
* https://github.com/sgratzl/lineup-lite | ||
* | ||
* Copyright (c) 2021 Samuel Gratzl <[email protected]> | ||
*/ | ||
|
||
export * from './utils'; | ||
export { default as filterByCategoricalSetFilter } from './filterByCategoricalSetFilter'; | ||
export { default as filterByRangeFilter } from './filterByRangeFilter'; | ||
export { default as filterByCategoricalFilter } from './filterByCategoricalFilter'; |
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,8 @@ | ||
/** | ||
* @lineup-lite/components | ||
* https://github.com/sgratzl/lineup-lite | ||
* | ||
* Copyright (c) 2021 Samuel Gratzl <[email protected]> | ||
*/ | ||
|
||
export const ALWAYS_TRUE = () => true; |
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 |
---|---|---|
|
@@ -7,3 +7,5 @@ | |
|
||
export * from './components'; | ||
export * from './math'; | ||
export * from './filters'; | ||
|
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