Skip to content

Instantly share code, notes, and snippets.

@alex35mil
Created March 13, 2017 08:41
Show Gist options
  • Save alex35mil/a956e6014d5d776c5ed7b984c2a12601 to your computer and use it in GitHub Desktop.
Save alex35mil/a956e6014d5d776c5ed7b984c2a12601 to your computer and use it in GitHub Desktop.
/* @flow */
import { Record } from 'immutable';
interface $EnumInterface<T: Object> extends Record<T> {
items: Function;
fromValue: Function;
}
const createEnum = <T: Object>(items: T): $EnumInterface<T> => {
class Enum extends Record(items) {
// `this` here is an instance of Record so all instance methods are available!
items = () => this.toArray();
fromValue = value => this.find(item => item.value === value);
}
return new Enum();
};
export default createEnum;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment