-
-
Notifications
You must be signed in to change notification settings - Fork 399
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const extract = (from) => { | ||
let to | ||
for (const key in from) { | ||
const value = from[key] | ||
const type = typeof value | ||
|
||
if (type === 'function') { | ||
if (!to) to = {} | ||
to[key] = value | ||
} | ||
else if (type === 'object' && value !== null && !Array.isArray(value)) { | ||
if (!to) to = {} | ||
const extracted = extract(value) | ||
if (extracted) to[key] = extracted | ||
} | ||
} | ||
|
||
return to | ||
} | ||
|
||
/** | ||
* Extracts a styles object with only rules that contain function values. | ||
*/ | ||
export default (styles: Object): Object => extract(styles) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import expect from 'expect.js' | ||
import getDynamicStyles from '../../src/utils/getDynamicStyles' | ||
|
||
describe('Unit: jss', () => { | ||
describe('getDynamicStyles', () => { | ||
it('should extract dynamic styles', () => { | ||
const color = data => data.color | ||
const styles = { | ||
button: { | ||
float: 'left', | ||
margin: [5, 10], | ||
color, | ||
'@media screen': { | ||
width: null, | ||
}, | ||
'@media print': { | ||
width: undefined, | ||
color | ||
}, | ||
'& a': { | ||
color: 'red', | ||
'& b': { | ||
color | ||
} | ||
}, | ||
}, | ||
'@media': { | ||
button: { | ||
width: 2, | ||
color | ||
} | ||
} | ||
} | ||
expect(getDynamicStyles(styles)).to.eql({ | ||
button: { | ||
color, | ||
'@media print': { | ||
color | ||
}, | ||
'& a': { | ||
'& b': { | ||
color | ||
} | ||
} | ||
}, | ||
'@media': { | ||
button: { | ||
color | ||
} | ||
} | ||
}) | ||
}) | ||
}) | ||
}) |