-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement getSeparatedStyles to reduce creation of unnecessary static…
… rules classes/rules
- Loading branch information
Showing
6 changed files
with
140 additions
and
40 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
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,84 @@ | ||
import getSeparatedStyles from '../utils/getSeparatedStyles' | ||
|
||
describe('unit tests for utils', () => { | ||
it('should extract separated 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 | ||
} | ||
}, | ||
nested: { | ||
'& a': { | ||
color: 'red' | ||
} | ||
} | ||
} | ||
|
||
expect(getSeparatedStyles(styles)).toEqual({ | ||
dynamicStyle: { | ||
button: { | ||
color, | ||
'@media print': { | ||
color | ||
}, | ||
'& a': { | ||
'& b': { | ||
color | ||
} | ||
} | ||
}, | ||
'@media': { | ||
button: { | ||
color | ||
} | ||
} | ||
}, | ||
staticStyle: { | ||
button: { | ||
float: 'left', | ||
margin: [5, 10], | ||
'@media screen': { | ||
width: null, | ||
}, | ||
'@media print': { | ||
width: undefined, | ||
}, | ||
'& a': { | ||
color: 'red', | ||
} | ||
}, | ||
'@media': { | ||
button: { | ||
width: 2, | ||
} | ||
}, | ||
nested: { | ||
'& a': { | ||
color: 'red' | ||
} | ||
} | ||
} | ||
}) | ||
}) | ||
}) |
File renamed without changes.
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,27 @@ | ||
const isObject = value => | ||
typeof value === 'object' && value !== null && !Array.isArray(value) | ||
|
||
/** | ||
* Extracts static and dynamic styles objects separately | ||
*/ | ||
const getSeparatedStyles = (styles: Object) => { | ||
const result = {} | ||
|
||
for (const key in styles) { | ||
const value = styles[key] | ||
const itemStyles = {} | ||
|
||
if (typeof value === 'function') itemStyles.dynamicStyle = value | ||
else if (isObject(value)) Object.assign(itemStyles, getSeparatedStyles(value)) | ||
else itemStyles.staticStyle = value | ||
|
||
for (const styleType in itemStyles) { | ||
result[styleType] = result[styleType] || {} | ||
result[styleType][key] = itemStyles[styleType] | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
export default getSeparatedStyles |