Skip to content

Commit

Permalink
feat(color-palettes): add themeIDs() iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed May 23, 2024
1 parent c4f19ad commit b86bb32
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions packages/color-palettes/src/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,21 +129,42 @@ export const lchThemes = (...preds: ThemePredicate[] | number[]) =>
export const rgbThemes = (...preds: ThemePredicate[] | number[]) =>
__themes(asRGB, preds);

/**
* Yields iterator of theme IDs (indices). Yields all themes unless specific
* theme IDs or filter predicates are provided.
*
* @param preds
*/
export const themeIDs = (...preds: ThemePredicate[] | number[]) =>
__themes(asRGB, preds, true);

/** @internal */
function* __themes<T extends Theme>(
function __themes<T extends Theme>(
fn: (id: number) => T,
preds: ThemePredicate[] | number[]
): IterableIterator<T>;
/** @internal */
function __themes<T extends Theme>(
fn: (id: number) => T,
preds: ThemePredicate[] | number[],
idOnly: true
): IterableIterator<number>;
/** @internal */
function* __themes<T extends Theme>(
fn: (id: number) => T,
preds: ThemePredicate[] | number[],
idOnly = false
) {
if (preds.length && typeof preds[0] === "function") {
const pred = compFilter(...(<ThemePredicate[]>preds));
for (let i = 0; i < NUM_THEMES; i++) {
const theme = fn(i);
if (pred(theme)) yield theme;
if (pred(theme)) yield idOnly ? i : theme;
}
} else if (preds.length) {
for (let id of <number[]>preds) yield fn(id);
for (let id of <number[]>preds) yield idOnly ? id : fn(id);
} else {
for (let i = 0; i < NUM_THEMES; i++) yield fn(i);
for (let i = 0; i < NUM_THEMES; i++) yield idOnly ? i : fn(i);
}
}

Expand Down

0 comments on commit b86bb32

Please sign in to comment.