feat(lib): add css utility to format style values

This commit is contained in:
Eric Liu 2019-12-22 15:32:21 -08:00
commit 9c83fbda69
3 changed files with 26 additions and 0 deletions

View file

@ -0,0 +1,9 @@
import { css } from '../css';
test('css', () => {
expect(css([])).toEqual('');
expect(css(['width: 20px; height: 20px;'])).toEqual('width: 20px; height: 20px');
expect(css(['width: 20px;', ['height', '20px']])).toEqual('width: 20px; height: 20px');
expect(css(['width: 20px', ['height', '20px']])).toEqual('width: 20px; height: 20px');
expect(css([undefined, null, 0, ['height', '20px']])).toEqual('height: 20px');
});

16
src/lib/css.js Normal file
View file

@ -0,0 +1,16 @@
function css(array) {
return array
.map((item, i) => {
if (!item) return false;
return Array.isArray(item)
? item.join(': ')
: item.slice(-1) === ';'
? item.slice(0, -1)
: item;
})
.filter(Boolean)
.join('; ');
}
export { css };

View file

@ -1 +1,2 @@
export { cx } from './cx'; export { cx } from './cx';
export { css } from './css';