Skip to content

Commit

Permalink
feat(color-palettes): add filteredThemes()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jun 28, 2024
1 parent 8ab6083 commit 58a9712
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions packages/color-palettes/src/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,42 @@ const __isLCH = (x: ThemeColor): x is LCH =>
/** @internal */
const __isRGB = (x: ThemeColor): x is SRGB =>
!isPrimitive(x) && x.mode === "srgb";

/**
* Similar to {@link rgbThemes}, {@link themeIDs} etc., but for filtering custom
* themes (i.e. arrays of colors), using provided filter predicate(s) or theme
* indices. This way the composable filter predicates of this package can also
* be used for user-defined themes (i.e. not limited to those presets included
* in this package)
*
* @remarks
* If `idOnly` is enabled/true, only the IDs/indices of matching themes are
* returned (rather than the corresponding themes themselves).
*
* @param preds
* @param themes
*/
export function filteredThemes<T extends Theme>(
preds: ThemePredicate[] | number[],
themes: T[]
): IterableIterator<T>;
export function filteredThemes<T extends Theme>(
preds: ThemePredicate[] | number[],
themes: T[],
idOnly: true
): IterableIterator<number>;
export function* filteredThemes<T extends Theme>(
preds: ThemePredicate[] | number[],
themes: T[],
idOnly = false
) {
if (preds.length && typeof preds[0] === "function") {
const pred = compFilter(...(<ThemePredicate[]>preds));
for (let i = 0; i < themes.length; i++) {
const theme = themes[i];
if (pred(theme)) yield idOnly ? i : theme;
}
} else {
for (let id of <number[]>preds) yield idOnly ? id : themes[id];
}
}

0 comments on commit 58a9712

Please sign in to comment.