Created
July 22, 2019 03:46
-
-
Save vincentriemer/85781f87ceb45f950d84e789bb4c71fd to your computer and use it in GitHub Desktop.
instyle
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 | |
*/ | |
opaque type Ruleset = { [string]: string | number }; | |
type CreateInput = { | |
[styleName: string]: CSSProperties | |
}; | |
type ToRuleset = () => Ruleset; | |
function create<T: CreateInput>(input: T): $ObjMap<T, ToRuleset> { | |
return input; | |
} | |
type InStyle = { | |
(...styles: Array<CSSProperties | Ruleset>): CSSProperties, | |
create: typeof create | |
}; | |
function _instyle(...styles: Array<CSSProperties | Ruleset>): CSSProperties { | |
if (styles.length === 1) { | |
return styles[0]; | |
} | |
const result: CSSProperties = styles.reduce((acc, cur) => { | |
if (cur != null) { | |
for (const key in cur) { | |
if (cur.hasOwnProperty(key)) { | |
acc[key] = cur[key]; | |
} | |
} | |
} | |
return acc; | |
}, ({}: CSSProperties)); | |
return result; | |
} | |
_instyle.create = create; | |
const instyle: InStyle = _instyle; | |
export default instyle; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment