-
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathomitProps.ts
More file actions
37 lines (32 loc) · 1.07 KB
/
omitProps.ts
File metadata and controls
37 lines (32 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import capitalize from '../../strings/capitalize'
import Layout from '../../../Layout'
import propAliases from '../../../const/propAliases'
import memoizeWith from '../memoizeWith'
const breakpoints = Object.keys(Layout.breakpoints)
const responsiveProps = Object.keys(propAliases)
const allResponsiveProps = responsiveProps.reduce((acc, prop) => {
return acc.concat(
prop,
`${prop}Down`,
`${prop}Only`,
...breakpoints.map((breakpointName) => {
const responsivePropName = `${prop}${capitalize(breakpointName)}`
return [`${responsivePropName}Down`, `${responsivePropName}Only`]
}),
)
}, [])
const regExp = new RegExp(allResponsiveProps.join('|'))
function omitProps<P extends Record<string, any>>(props: P) {
return Object.keys(props).reduce((acc, key) => {
if (!regExp.test(key)) {
acc[key] = props[key]
}
return acc
}, {} as Record<string, any>)
}
const memoizeWithPairs = memoizeWith<typeof omitProps>((props) =>
Object.keys(props)
.map((key) => [key, props[key]])
.join(),
)
export default memoizeWithPairs(omitProps)