Skip to content

Commit 04336f9

Browse files
committed
Use prettier
1 parent 9836399 commit 04336f9

File tree

13 files changed

+309
-224
lines changed

13 files changed

+309
-224
lines changed

.eslintrc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
"parser": "babel-eslint",
33
"extends": [
44
"airbnb-base",
5-
"plugin:flowtype/recommended"
5+
"plugin:flowtype/recommended",
6+
"plugin:prettier/recommended",
7+
"prettier/flowtype"
68
],
79
"plugins": [
810
"flowtype",
@@ -12,8 +14,6 @@
1214
"jest": true
1315
},
1416
"rules": {
15-
"semi": [2, never],
16-
"comma-dangle": 0,
17-
"flowtype-errors/show-errors": 2
17+
"flowtype-errors/show-errors": "error"
1818
}
1919
}

README.md

Lines changed: 50 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ Useful interpolated functions for [styled-components](https://github.com/styled-
1818
Play with it on [WebpackBin](https://www.webpackbin.com/bins/-Kel3KgddZSrD5oK0fIk)
1919

2020
```jsx
21-
import styled, { css } from 'styled-components'
22-
import { prop, ifProp, switchProp } from 'styled-tools'
21+
import styled, { css } from "styled-components";
22+
import { prop, ifProp, switchProp } from "styled-tools";
2323

2424
const Button = styled.button`
25-
color: ${prop('color', 'red')};
26-
font-size: ${ifProp({ size: 'large' }, '20px', '14px')};
27-
 background-color: ${switchProp('theme', {
28-
dark: 'blue',
29-
darker: 'mediumblue',
30-
darkest: 'darkblue'
25+
color: ${prop("color", "red")};
26+
font-size: ${ifProp({ size: "large" }, "20px", "14px")};
27+
 background-color: ${switchProp("theme", {
28+
dark: "blue",
29+
darker: "mediumblue",
30+
darkest: "darkblue"
3131
})};
32-
`
32+
`;
3333

3434
// renders with color: blue
3535
<Button color="blue" />
@@ -48,31 +48,31 @@ A more complex example:
4848

4949
```jsx
5050
const Button = styled.button`
51-
color: ${prop('theme.colors.white', '#fff')};
52-
 font-size: ${ifProp({ size: 'large' }, prop('theme.sizes.lg', '20px'), prop('theme.sizes.md', '14px'))};
53-
 background-color: ${prop('theme.colors.black', '#000')};
51+
color: ${prop("theme.colors.white", "#fff")};
52+
 font-size: ${ifProp({ size: "large" }, prop("theme.sizes.lg", "20px"), prop("theme.sizes.md", "14px"))};
53+
 background-color: ${prop("theme.colors.black", "#000")};
5454
55-
${switchProp('kind', {
55+
${switchProp("kind", {
5656
dark: css`
57-
background-color: ${prop('theme.colors.blue', 'blue')};
58-
     border: 1px solid ${prop('theme.colors.blue', 'blue')};
57+
background-color: ${prop("theme.colors.blue", "blue")};
58+
     border: 1px solid ${prop("theme.colors.blue", "blue")};
5959
`,
6060
darker: css`
61-
background-color: ${prop('theme.colors.mediumblue', 'mediumblue')};
62-
     border: 1px solid ${prop('theme.colors.mediumblue', 'mediumblue')};
61+
background-color: ${prop("theme.colors.mediumblue", "mediumblue")};
62+
     border: 1px solid ${prop("theme.colors.mediumblue", "mediumblue")};
6363
`,
6464
darkest: css`
65-
background-color: ${prop('theme.colors.darkblue', 'darkblue')};
66-
     border: 1px solid ${prop('theme.colors.darkblue', 'darkblue')};
67-
`,
65+
background-color: ${prop("theme.colors.darkblue", "darkblue")};
66+
     border: 1px solid ${prop("theme.colors.darkblue", "darkblue")};
67+
`
6868
})}
6969
70-
 ${ifProp('disabled', css`
71-
   background-color: ${prop('theme.colors.gray', '#999')};
72-
   border-color: ${prop('theme.colors.gray', '#999')};
70+
 ${ifProp("disabled", css`
71+
   background-color: ${prop("theme.colors.gray", "#999")};
72+
   border-color: ${prop("theme.colors.gray", "#999")};
7373
pointer-events: none;
7474
`)}
75-
`
75+
`;
7676
```
7777

7878
## API
@@ -101,8 +101,8 @@ Returns the value of `props[path]` or `defaultValue`
101101

102102
```javascript
103103
const Button = styled.button`
104-
color: ${prop('color', 'red')};
105-
`
104+
color: ${prop("color", "red")};
105+
`;
106106
```
107107

108108
Returns **any**
@@ -121,15 +121,15 @@ Returns `pass` if prop is truthy. Otherwise returns `fail`
121121

122122
```javascript
123123
// usage with styled-theme
124-
import styled from 'styled-components'
125-
import { ifProp } from 'styled-tools'
126-
import { palette } from 'styled-theme'
124+
import styled from "styled-components";
125+
import { ifProp } from "styled-tools";
126+
import { palette } from "styled-theme";
127127

128128
const Button = styled.button`
129-
background-color: ${ifProp('transparent', 'transparent', palette(0))};
130-
color: ${ifProp(['transparent', 'accent'], palette('secondary', 0))};
131-
font-size: ${ifProp({ size: 'large' }, '20px', ifProp({ size: 'medium' }, '16px', '12px'))};
132-
`
129+
background-color: ${ifProp("transparent", "transparent", palette(0))};
130+
color: ${ifProp(["transparent", "accent"], palette("secondary", 0))};
131+
font-size: ${ifProp({ size: "large" }, "20px", ifProp({ size: "medium" }, "16px", "12px"))};
132+
`;
133133
```
134134

135135
Returns **any**
@@ -147,23 +147,22 @@ Calls a function passing properties values as arguments.
147147

148148
```javascript
149149
// example with polished
150-
import styled from 'styled-components'
151-
import { darken } from 'polished'
152-
import { withProp, prop } from 'styled-tools'
150+
import styled from "styled-components";
151+
import { darken } from "polished";
152+
import { withProp, prop } from "styled-tools";
153153

154154
const Button = styled.button`
155-
border-color: ${withProp(prop('theme.primaryColor', 'blue'), darken(0.5))};
156-
font-size: ${withProp('theme.size', size => `${size + 1}px`)};
157-
background: ${withProp(['foo', 'bar'], (foo, bar) => `${foo}${bar}`)};
158-
`
155+
border-color: ${withProp(prop("theme.primaryColor", "blue"), darken(0.5))};
156+
font-size: ${withProp("theme.size", size => `${size + 1}px`)};
157+
background: ${withProp(["foo", "bar"], (foo, bar) => `${foo}${bar}`)};
158+
`;
159159
```
160160

161161
Returns **any**
162162

163163
### switchProp
164164

165165
Switches on a given prop. Returns the value or function for a given prop value.
166-
Otherwise returns `defaultValue`
167166

168167
**Parameters**
169168

@@ -173,26 +172,26 @@ Otherwise returns `defaultValue`
173172
**Examples**
174173

175174
```javascript
176-
import styled, { css } from 'styled-components'
177-
import { switchProp, prop } from 'styled-tools'
175+
import styled, { css } from "styled-components";
176+
import { switchProp, prop } from "styled-tools";
178177

179178
const Button = styled.button`
180-
font-size: ${switchProp(prop('size', 'medium'), {
181-
small: prop('theme.sizes.sm', '12px'),
182-
medium: prop('theme.sizes.md', '16px'),
183-
large: prop('theme.sizes.lg', '20px'),
179+
font-size: ${switchProp(prop("size", "medium"), {
180+
small: prop("theme.sizes.sm", "12px"),
181+
medium: prop("theme.sizes.md", "16px"),
182+
large: prop("theme.sizes.lg", "20px")
184183
}};
185-
${switchProp('theme.kind', {
184+
${switchProp("theme.kind", {
186185
light: css`
187186
color: LightBlue;
188187
`,
189188
dark: css`
190189
color: DarkBlue;
191-
`,
190+
`
192191
})}
193-
`
192+
`;
194193
195-
<Button size="large" theme={{ kind: 'light' }} />
194+
<Button size="large" theme={{ kind: "light" }} />
196195
```
197196
198197
Returns **any**

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,17 @@
6363
"documentation": "^6.1.0",
6464
"eslint": "^4.15.0",
6565
"eslint-config-airbnb-base": "^12.1.0",
66+
"eslint-config-prettier": "^2.9.0",
6667
"eslint-plugin-flowtype": "^2.29.2",
6768
"eslint-plugin-flowtype-errors": "^3.0.0",
6869
"eslint-plugin-import": "^2.2.0",
70+
"eslint-plugin-prettier": "^2.6.0",
6971
"flow-bin": "^0.69.0",
7072
"flow-typed": "^2.0.0",
7173
"jest-cli": "^22.0.6",
7274
"npm-watch": "^0.3.0",
7375
"opn-cli": "^3.1.0",
76+
"prettier": "^1.11.1",
7477
"rimraf": "^2.6.2"
7578
}
7679
}

src/ifProp.js

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,42 @@
11
// @flow
2-
import get from 'lodash/get'
3-
import at from 'lodash/at'
4-
import values from 'lodash/values'
5-
import difference from 'lodash/difference'
6-
import type { Needle } from '.'
2+
import get from "lodash/get";
3+
import at from "lodash/at";
4+
import values from "lodash/values";
5+
import difference from "lodash/difference";
6+
import type { Needle } from ".";
77

88
/**
99
* Returns `pass` if prop is truthy. Otherwise returns `fail`
1010
* @example
1111
* // usage with styled-theme
12-
* import styled from 'styled-components'
13-
* import { ifProp } from 'styled-tools'
14-
* import { palette } from 'styled-theme'
12+
* import styled from "styled-components";
13+
* import { ifProp } from "styled-tools";
14+
* import { palette } from "styled-theme";
1515
*
1616
* const Button = styled.button`
17-
* background-color: ${ifProp('transparent', 'transparent', palette(0))};
18-
* color: ${ifProp(['transparent', 'accent'], palette('secondary', 0))};
19-
* font-size: ${ifProp({ size: 'large' }, '20px', ifProp({ size: 'medium' }, '16px', '12px'))};
20-
* `
17+
* background-color: ${ifProp("transparent", "transparent", palette(0))};
18+
* color: ${ifProp(["transparent", "accent"], palette("secondary", 0))};
19+
* font-size: ${ifProp({ size: "large" }, "20px", ifProp({ size: "medium" }, "16px", "12px"))};
20+
* `;
2121
*/
22-
const ifProp = (test: Needle | string[] | Object, pass?: any, fail?: any): any => (
23-
(props: Object = {}): any => {
24-
let result
25-
if (Array.isArray(test)) {
26-
result = !at(props, test).filter(value => !value).length
27-
} else if (typeof test === 'function') {
28-
result = test(props)
29-
} else if (typeof test === 'object') {
30-
const testKeys = Object.keys(test)
31-
const testValues = values(test)
32-
result = !difference(at(props, testKeys), testValues).length
33-
} else {
34-
result = get(props, test)
35-
}
36-
return result ? pass : fail
22+
const ifProp = (
23+
test: Needle | string[] | Object,
24+
pass?: any,
25+
fail?: any
26+
): any => (props: Object = {}): any => {
27+
let result;
28+
if (Array.isArray(test)) {
29+
result = !at(props, test).filter(value => !value).length;
30+
} else if (typeof test === "function") {
31+
result = test(props);
32+
} else if (typeof test === "object") {
33+
const testKeys = Object.keys(test);
34+
const testValues = values(test);
35+
result = !difference(at(props, testKeys), testValues).length;
36+
} else {
37+
result = get(props, test);
3738
}
38-
)
39+
return result ? pass : fail;
40+
};
3941

40-
export default ifProp
42+
export default ifProp;

src/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export ifProp from './ifProp'
2-
export prop from './prop'
3-
export switchProp from './switchProp'
4-
export withProp from './withProp'
1+
export ifProp from "./ifProp";
2+
export prop from "./prop";
3+
export switchProp from "./switchProp";
4+
export withProp from "./withProp";

src/prop.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
// @flow
2-
import get from 'lodash/get'
2+
import get from "lodash/get";
33

44
/**
55
* Returns the value of `props[path]` or `defaultValue`
66
* @example
77
* const Button = styled.button`
8-
* color: ${prop('color', 'red')};
9-
* `
8+
* color: ${prop("color", "red")};
9+
* `;
1010
*/
11-
const prop = (path: string, defaultValue?: any): any => (
12-
(props: Object = {}) => get(props, path, defaultValue)
13-
)
11+
const prop = (path: string, defaultValue?: any): any => (props: Object = {}) =>
12+
get(props, path, defaultValue);
1413

15-
export default prop
14+
export default prop;

src/switchProp.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,37 @@
11
// @flow
2-
import get from 'lodash/get'
3-
import type { Needle } from '.'
2+
import get from "lodash/get";
3+
import type { Needle } from ".";
44

55
/**
66
* Switches on a given prop. Returns the value or function for a given prop value.
7-
* Otherwise returns `defaultValue`
87
* @example
9-
* import styled, { css } from 'styled-components'
10-
* import { switchProp, prop } from 'styled-tools'
8+
* import styled, { css } from "styled-components";
9+
* import { switchProp, prop } from "styled-tools";
1110
*
1211
* const Button = styled.button`
13-
* font-size: ${switchProp(prop('size', 'medium'), {
14-
* small: prop('theme.sizes.sm', '12px'),
15-
* medium: prop('theme.sizes.md', '16px'),
16-
* large: prop('theme.sizes.lg', '20px'),
12+
* font-size: ${switchProp(prop("size", "medium"), {
13+
* small: prop("theme.sizes.sm", "12px"),
14+
* medium: prop("theme.sizes.md", "16px"),
15+
* large: prop("theme.sizes.lg", "20px")
1716
* }};
18-
* ${switchProp('theme.kind', {
17+
* ${switchProp("theme.kind", {
1918
* light: css`
2019
* color: LightBlue;
2120
* `,
2221
* dark: css`
2322
* color: DarkBlue;
24-
* `,
23+
* `
2524
* })}
26-
* `
25+
* `;
2726
*
28-
* <Button size="large" theme={{ kind: 'light' }} />
27+
* <Button size="large" theme={{ kind: "light" }} />
2928
*/
30-
const switchProp = (needle: Needle, switches: Object): any => (props: Object = {}): any => {
31-
const value = typeof needle === 'function' ? needle(props) : get(props, needle)
32-
return get(switches, value)
33-
}
29+
const switchProp = (needle: Needle, switches: Object): any => (
30+
props: Object = {}
31+
): any => {
32+
const value =
33+
typeof needle === "function" ? needle(props) : get(props, needle);
34+
return get(switches, value);
35+
};
3436

35-
export default switchProp
37+
export default switchProp;

0 commit comments

Comments
 (0)