Skip to content

Commit

Permalink
Merge pull request styled-system#824 from styled-system/props-util
Browse files Browse the repository at this point in the history
Add Props utility package
  • Loading branch information
jxnblk authored Sep 16, 2019
2 parents 7824f5c + 787f960 commit 5260480
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/packages.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ The Styled System ecosystem also includes these optional packages
| [`@styled-system/prop-types`](https://github.com/styled-system/styled-system/tree/master/packages/prop-types) | Prop type definitions for components built with Styled System |
| [`@styled-system/theme-get`](https://github.com/styled-system/styled-system/tree/master/packages/theme-get) | Utility to safely access values from a theme |
| [`@styled-system/edit`](https://github.com/styled-system/styled-system/tree/master/packages/edit) | Debugging components for live editing Styled System theme objects |
[`@styled-system/props`](https://github.com/styled-system/styled-system/tree/master/packages/props) | Utilities for working with Styled System Props
26 changes: 26 additions & 0 deletions packages/props/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

# @styled-system/props

Utilities for using Styled System props

```sh
npm i @styled-system/props
```

```js
import { pick, omit } from '@styled-system/props'

const attr = omit({
id: 'keep-this',
color: 'primary',
})
// { id: 'keep-this' }

const props = pick({
className: 'hello',
color: 'secondary',
})
// { color: 'secondary' }
```

MIT License
14 changes: 14 additions & 0 deletions packages/props/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "@styled-system/props",
"version": "5.1.2",
"main": "dist/index.js",
"module": "dist/index.esm.js",
"author": "Brent Jackson",
"license": "MIT",
"dependencies": {
"styled-system": "^5.0.0"
},
"publishConfig": {
"access": "public"
}
}
52 changes: 52 additions & 0 deletions packages/props/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import {
compose,
space,
typography,
color,
layout,
flexbox,
border,
background,
position,
grid,
shadow,
buttonStyle,
textStyle,
colorStyle
} from 'styled-system'

const all = compose(
space,
typography,
color,
layout,
flexbox,
border,
background,
position,
grid,
shadow,
buttonStyle,
textStyle,
colorStyle
)

const regex = new RegExp(`^(${all.propNames.join('|')})$`)

export const omit = props => {
const next = {}
for (let key in props) {
if (regex.test(key)) continue
next[key] = props[key]
}
return next
}

export const pick = props => {
const next = {}
for (let key in props) {
if (!regex.test(key)) continue
next[key] = props[key]
}
return next
}
26 changes: 26 additions & 0 deletions packages/props/test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { pick, omit } from '../src'

const props = {
id: 'hi',
className: 'beep',
p: 3,
bg: 'tomato',
color: 'white',
}

test('omits styled-system props', () => {
const attr = omit(props)
expect(attr).toEqual({
id: 'hi',
className: 'beep',
})
})

test('picks styled-system props', () => {
const sx = pick(props)
expect(sx).toEqual({
p: 3,
bg: 'tomato',
color: 'white',
})
})

0 comments on commit 5260480

Please sign in to comment.