Skip to content

Commit b596706

Browse files
author
rofrischmann
committed
pushed some new utils
1 parent d71f0f7 commit b596706

15 files changed

+354
-19
lines changed

.eslintrc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
"no-mixed-operators": [ 0 ],
2424
"no-underscore-dangle": [ 0 ],
2525
"no-plusplus": [ 0 ],
26+
"no-continue": [ 0 ],
2627
"no-restricted-syntax": [ 0 ],
27-
"consistent-return": [ 0 ]
28+
"consistent-return": [ 0 ],
29+
"guard-for-in": [ 0 ]
2830
}
2931
}

Changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
# Changelog
22

3+
### 1.0.2
4+
* added `resolveArrayValue` and `assignStyle`
5+
6+
### 1.0.1
7+
* added `cssifyDeclaration` and `cssifyObject`
8+
39
### 1.0.0
410
Initial version

README.md

Lines changed: 70 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ By now I have authored and collaborated on many different libraries and found I
1818

1919
## Utilities
2020
* [`camelCaseProperty(property)`](#camelcasepropertyproperty)
21+
* [`cssifyDeclaration(property, value)`](#cssifydeclarationproperty-value)
22+
* [`cssifyObject(object)`](#cssifyobjectproperty)
2123
* [`hyphenateProperty(property)`](#hyphenatepropertyproperty)
2224
* [`isPrefixedProperty(property)`](#isprefixedpropertyproperty)
2325
* [`isPrefixedValue(value)`](#isprefixedvaluevalue)
2426
* [`isUnitlessProperty(property)`](#isunitlessproperty)
2527
* [`normalizeProperty(property)`](#normalizepropertyproperty)
28+
* [`resolveArrayValue(property, value)`](#resolvearrayvalueproperty-value)
2629
* [`unprefixProperty(property)`](#unprefixpropertyproperty)
2730
* [`unprefixValue(value)`](#unprefixvaluevalue)
2831

@@ -34,26 +37,59 @@ Converts the `property` to camelCase.
3437
```javascript
3538
import { camelCaseProperty } from 'css-in-js-utils'
3639

37-
console.log(camelCaseProperty('padding-top'))
40+
camelCaseProperty('padding-top')
3841
// => 'paddingTop'
3942

40-
console.log(camelCaseProperty('-webkit-transition'))
43+
camelCaseProperty('-webkit-transition')
4144
// => 'WebkitTransition'
4245
```
4346

4447
------
4548

49+
### `cssifyDeclaration(property, value)`
50+
Generates a CSS declaration (`property`:`value`) string.
51+
52+
```javascript
53+
import { cssifyDeclaration } from 'css-in-js-utils'
54+
55+
cssifyDeclaration('paddingTop', '400px')
56+
// => 'padding-top:400px'
57+
58+
cssifyDeclaration('WebkitFlex', 3)
59+
// => '-webkit-flex:3'
60+
```
61+
62+
------
63+
64+
### `cssifyObject(object)`
65+
Generates a CSS string using all key-property pairs in `object`.
66+
It automatically removes declarations with value types other than `number` and `string`.
67+
68+
```javascript
69+
import { cssifyObject } from 'css-in-js-utils'
70+
71+
cssifyObject({
72+
paddingTop: '400px',
73+
paddingBottom: undefined,
74+
WebkitFlex: 3,
75+
_anyKey: [1, 2, 4]
76+
})
77+
// => 'padding-top:400px;-webkit-flex:3'
78+
```
79+
80+
------
81+
4682
### `hyphenateProperty(property)`
4783
Converts the `property` to hyphen-case.
4884
> Directly mirrors [hyphenate-style-name](https://github.com/rexxars/hyphenate-style-name).
4985
5086
```javascript
5187
import { hyphenateProperty } from 'css-in-js-utils'
5288

53-
console.log(hyphenateProperty('paddingTop'))
89+
hyphenateProperty('paddingTop')
5490
// => 'padding-top'
5591

56-
console.log(hyphenateProperty('WebkitTransition'))
92+
hyphenateProperty('WebkitTransition')
5793
// => '-webkit-transition'
5894
```
5995

@@ -65,10 +101,10 @@ Checks if a `property` includes a vendor prefix.
65101
```javascript
66102
import { isPrefixedProperty } from 'css-in-js-utils'
67103

68-
console.log(isPrefixedProperty('paddingTop'))
104+
isPrefixedProperty('paddingTop')
69105
// => false
70106

71-
console.log(isPrefixedProperty('WebkitTransition'))
107+
isPrefixedProperty('WebkitTransition')
72108
// => true
73109
```
74110

@@ -79,11 +115,11 @@ Checks if a `value` includes vendor prefixes.
79115
```javascript
80116
import { isPrefixedValue } from 'css-in-js-utils'
81117

82-
console.log(isPrefixedValue('200px'))
83-
console.log(isPrefixedValue(200))
118+
isPrefixedValue('200px')
119+
isPrefixedValue(200)
84120
// => false
85121

86-
console.log(isPrefixedValue('-webkit-calc(100% - 50px)'))
122+
isPrefixedValue('-webkit-calc(100% - 50px)')
87123
// => true
88124
```
89125

@@ -96,11 +132,11 @@ Checks if a `property` accepts unitless values.
96132
```javascript
97133
import { isUnitlessProperty } from 'css-in-js-utils'
98134

99-
console.log(isUnitlessProperty('width'))
135+
isUnitlessProperty('width')
100136
// => false
101137

102-
console.log(isUnitlessProperty('flexGrow'))
103-
console.log(isUnitlessProperty('lineHeight'))
138+
isUnitlessProperty('flexGrow')
139+
isUnitlessProperty('lineHeight')
104140
// => true
105141
```
106142

@@ -113,22 +149,39 @@ Normalizes the `property` by unprefixing **and** camelCasing it.
113149
```javascript
114150
import { normalizeProperty } from 'css-in-js-utils'
115151

116-
console.log(normalizeProperty('-webkit-transition-delay'))
152+
normalizeProperty('-webkit-transition-delay')
117153
// => 'transitionDelay'
118154
```
119155

120156
------
121157

158+
### `resolveArrayValue(property, value)`
159+
Concatenates array values to single CSS value.
160+
> Uses the [`hyphenateProperty`](#hyphenatepropertyproperty)-method.
161+
162+
163+
```javascript
164+
import { resolveArrayValue } from 'css-in-js-utils'
165+
166+
resolveArrayValue('display', [ '-webkit-flex', 'flex' ])
167+
// => '-webkit-flex;display:flex'
168+
169+
resolveArrayValue('paddingTop', [ 'calc(100% - 50px)', '100px' ])
170+
// => 'calc(100% - 50px);padding-top:100px'
171+
```
172+
173+
------
174+
122175
### `unprefixProperty(property)`
123176
Removes the vendor prefix (if set) from the `property`.
124177

125178
```javascript
126179
import { unprefixProperty } from 'css-in-js-utils'
127180

128-
console.log(unprefixProperty('WebkitTransition'))
181+
unprefixProperty('WebkitTransition')
129182
// => 'transition'
130183

131-
console.log(unprefixProperty('transitionDelay'))
184+
unprefixProperty('transitionDelay')
132185
// => 'transitionDelay'
133186
```
134187

@@ -140,10 +193,10 @@ Removes all vendor prefixes (if any) from the `value`.
140193
```javascript
141194
import { unprefixValue } from 'css-in-js-utils'
142195

143-
console.log(unprefixValue('-webkit-calc(-moz-calc(100% - 50px)/2)'))
196+
unprefixValue('-webkit-calc(-moz-calc(100% - 50px)/2)')
144197
// => 'calc(calc(100% - 50px)/2)'
145198

146-
console.log(unprefixValue('100px'))
199+
unprefixValue('100px')
147200
// => '100px'
148201
```
149202

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// flow-typed signature: 891591a45488874951d5117dc307ab7c
2+
// flow-typed version: <<STUB>>/hyphenate-style-name_v^1.0.2/flow_v0.38.0
3+
4+
/**
5+
* This is an autogenerated libdef stub for:
6+
*
7+
* 'hyphenate-style-name'
8+
*
9+
* Fill this stub out by replacing all the `any` types.
10+
*
11+
* Once filled out, we encourage you to share your work with the
12+
* community by sending a pull request to:
13+
* https://github.com/flowtype/flow-typed
14+
*/
15+
16+
declare module 'hyphenate-style-name' {
17+
declare module.exports: any;
18+
}
19+
20+
/**
21+
* We include stubs for each file inside this npm package in case you need to
22+
* require those files directly. Feel free to delete any files that aren't
23+
* needed.
24+
*/
25+
26+
27+
// Filename aliases
28+
declare module 'hyphenate-style-name/index' {
29+
declare module.exports: $Exports<'hyphenate-style-name'>;
30+
}
31+
declare module 'hyphenate-style-name/index.js' {
32+
declare module.exports: $Exports<'hyphenate-style-name'>;
33+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// flow-typed signature: b5cafd4bc97fd336b2d83d1fe546f0b6
2+
// flow-typed version: <<STUB>>/unitless-css-property_v^1.0.2/flow_v0.38.0
3+
4+
/**
5+
* This is an autogenerated libdef stub for:
6+
*
7+
* 'unitless-css-property'
8+
*
9+
* Fill this stub out by replacing all the `any` types.
10+
*
11+
* Once filled out, we encourage you to share your work with the
12+
* community by sending a pull request to:
13+
* https://github.com/flowtype/flow-typed
14+
*/
15+
16+
declare module 'unitless-css-property' {
17+
declare module.exports: any;
18+
}
19+
20+
/**
21+
* We include stubs for each file inside this npm package in case you need to
22+
* require those files directly. Feel free to delete any files that aren't
23+
* needed.
24+
*/
25+
declare module 'unitless-css-property/modules/index' {
26+
declare module.exports: any;
27+
}
28+
29+
declare module 'unitless-css-property/test/index-test' {
30+
declare module.exports: any;
31+
}
32+
33+
// Filename aliases
34+
declare module 'unitless-css-property/modules/index.js' {
35+
declare module.exports: $Exports<'unitless-css-property/modules/index'>;
36+
}
37+
declare module 'unitless-css-property/test/index-test.js' {
38+
declare module.exports: $Exports<'unitless-css-property/test/index-test'>;
39+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import assignStyle from '../assignStyle'
2+
3+
describe('Assinging styles', () => {
4+
it('should merge properties', () => {
5+
expect(assignStyle({ color: 'red' }, { fontSize: 12 }, { lineHeight: 1 })).toEqual({
6+
color: 'red',
7+
fontSize: 12,
8+
lineHeight: 1
9+
})
10+
})
11+
12+
it('should overwrite properties from right to left', () => {
13+
expect(assignStyle({ fontSize: 12 }, { fontSize: 16 }, { fontSize: 11 })).toEqual({ fontSize: 11 })
14+
})
15+
16+
it('should merge nested objects', () => {
17+
expect(
18+
assignStyle(
19+
{
20+
fontSize: 12,
21+
ob2: { color: 'red' },
22+
ob3: { color: 'red' }
23+
},
24+
{
25+
fontSize: 16,
26+
ob2: { fontSize: 12 }
27+
},
28+
{
29+
fontSize: 11,
30+
ob3: { color: 'blue' }
31+
}
32+
)
33+
).toEqual({
34+
fontSize: 11,
35+
ob2: {
36+
color: 'red',
37+
fontSize: 12
38+
},
39+
ob3: { color: 'blue' }
40+
})
41+
})
42+
43+
it('should not overwrite objects other than the first one', () => {
44+
const ob1 = { color: 'red' }
45+
const ob2 = { fontSize: 12 }
46+
47+
const newOb = assignStyle({}, ob1, ob2)
48+
49+
expect(newOb).toEqual({
50+
color: 'red',
51+
fontSize: 12
52+
})
53+
54+
newOb.foo = 'bar'
55+
expect(ob1).toEqual({ color: 'red' })
56+
expect(ob2).toEqual({ fontSize: 12 })
57+
})
58+
59+
it('should use the first object as base', () => {
60+
const ob1 = { color: 'red' }
61+
const ob2 = { fontSize: 12 }
62+
63+
const newOb = assignStyle(ob1, ob2)
64+
65+
expect(newOb).toEqual({
66+
color: 'red',
67+
fontSize: 12
68+
})
69+
expect(ob1).toEqual(newOb)
70+
71+
newOb.foo = 'bar'
72+
expect(ob1).toEqual({
73+
color: 'red',
74+
fontSize: 12,
75+
foo: 'bar'
76+
})
77+
})
78+
})
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import cssifyDeclaration from '../cssifyDeclaration'
2+
3+
describe('Cssifying declarations', () => {
4+
it('should return a valid css declaration', () => {
5+
expect(cssifyDeclaration('width', '300px')).toEqual('width:300px')
6+
expect(cssifyDeclaration('WebkitFlex', '1')).toEqual('-webkit-flex:1')
7+
expect(cssifyDeclaration('msTransitionDuration', '3s')).toEqual('-ms-transition-duration:3s')
8+
})
9+
})
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import cssifyObject from '../cssifyObject'
2+
3+
describe('Cssifying objects', () => {
4+
it('should generate a valid CSS string', () => {
5+
expect(cssifyObject({ color: 'red' })).toEqual('color:red')
6+
})
7+
8+
it('should convert properties to dash case', () => {
9+
expect(cssifyObject({ fontSize: '12px' })).toEqual('font-size:12px')
10+
})
11+
12+
it('should separate declarations with semicolons', () => {
13+
expect(
14+
cssifyObject({
15+
fontSize: '12px',
16+
color: 'red'
17+
})
18+
).toEqual('font-size:12px;color:red')
19+
})
20+
21+
it('should convert vendor prefixes', () => {
22+
expect(
23+
cssifyObject({
24+
WebkitJustifyContent: 'center',
25+
msFlexAlign: 'center'
26+
})
27+
).toEqual('-webkit-justify-content:center;-ms-flex-align:center')
28+
})
29+
})

0 commit comments

Comments
 (0)