Created
March 13, 2017 08:41
-
-
Save alex35mil/a956e6014d5d776c5ed7b984c2a12601 to your computer and use it in GitHub Desktop.
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
/* @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