Skip to content

Instantly share code, notes, and snippets.

@igrek8
Created December 14, 2016 16:00
Show Gist options
  • Save igrek8/4218cc196c4d1e05a3a8978c17d4a0d0 to your computer and use it in GitHub Desktop.
Save igrek8/4218cc196c4d1e05a3a8978c17d4a0d0 to your computer and use it in GitHub Desktop.
export const resolveClasses = (rules: {
[className: string]: () => boolean
}, ...persistedClasses: string[]): string => {
let classes: string[] = [];
Object.keys(rules).forEach((className) => {
const isResolved = rules[className]();
if (isResolved) {
classes.push(className);
} else {
// should remove it
classes = classes.filter((needle) => (
needle == className
));
}
});
if (persistedClasses) {
classes = classes.concat(persistedClasses);
}
return classes.join(' ');
}
// Usage
resolveClasses({
'MyClass': () => true
'IgnoreClass': () => false
}, 'AlwaysThere') // => 'MyClass AlwaysThere'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment