From d6fec792ed128387c6c40515dc21a70e8802e872 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Filipek?= Date: Sun, 20 Dec 2015 13:21:01 +0100 Subject: [PATCH 1/2] simple autokey feature --- src/index.js | 9 +++++++- test/index.js | 59 +++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index a4b01a2..914025b 100644 --- a/src/index.js +++ b/src/index.js @@ -4,10 +4,17 @@ const truthy = x => x; export default theme => (key, ...names) => { const styles = names - .map(name => theme[name]) + .map(name => { + return theme[name] + }) .filter(truthy); return typeof styles[0] === 'string' ? { key, className: styles.join(' ') } : { key, style: assign({}, ...styles) }; }; + +export const autokey = (fnc) => { + let autoKey = 1 + return (...names) => fnc(autoKey++, ...names) +} diff --git a/test/index.js b/test/index.js index f367437..d403752 100644 --- a/test/index.js +++ b/test/index.js @@ -1,6 +1,5 @@ -import themeable from '../src'; +import themeable, { autokey } from '../src'; import { expect } from 'chai'; - describe('className', () => { const classes = { foo: 'aaa', bar: 'bbb' }; const classTheme = themeable(classes); @@ -79,3 +78,59 @@ describe('style', () => { }); }); + +describe('autokey', () => { + const classes = { foo: 'aaa', bar: 'bbb' }; + const styles = { + foo: { + color: 'red', + fontSize: '16px' + }, + bar: { + color: 'blue', + fontWeight: 'bold' + } + }; + const classTheme = autokey(themeable(classes)); + const styleTheme = autokey(themeable(styles)); + + it('should return a single class', () => { + expect(classTheme('foo')) + .to.deep.equal({ + key: 1, + className: classes.foo + }); + }); + + it('should return multiple classes', () => { + expect(classTheme('foo', 'bar')) + .to.deep.equal({ + key: 2, + className: `${classes.foo} ${classes.bar}` + }); + }); + + it('should return a single style', () => { + expect(styleTheme('foo')) + .to.deep.equal({ + key: 1, + style: { + color: 'red', + fontSize: '16px' + } + }); + }); + + it('should return multiple styles merged', () => { + expect(styleTheme('foo', 'bar')) + .to.deep.equal({ + key: 2, + style: { + fontSize: '16px', + color: 'blue', + fontWeight: 'bold' + } + }); + }); + +}); From cdfe60a5c5c2b1efe1af4743263821f6973cca40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Filipek?= Date: Sun, 20 Dec 2015 13:22:35 +0100 Subject: [PATCH 2/2] formatting --- src/index.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 914025b..bd7a1d8 100644 --- a/src/index.js +++ b/src/index.js @@ -4,9 +4,7 @@ const truthy = x => x; export default theme => (key, ...names) => { const styles = names - .map(name => { - return theme[name] - }) + .map(name => theme[name]) .filter(truthy); return typeof styles[0] === 'string' ?