Skip to content

Commit

Permalink
Merge pull request #483 from timhudson/merge-max-call-stack
Browse files Browse the repository at this point in the history
Prevent max call stack when merging overlapping keys
  • Loading branch information
jxnblk committed May 8, 2019
2 parents e6b5032 + cc67fbb commit 01d4669
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/index.js
Expand Up @@ -42,7 +42,7 @@ export const merge = (a, b) => {
result[key] = a[key]
}
for (const key in b) {
if (!a[key]) {
if (!a[key] || typeof a[key] !== 'object') {
result[key] = b[key]
} else {
result[key] = merge(a[key], b[key])
Expand Down
31 changes: 29 additions & 2 deletions test/index.js
Expand Up @@ -11,6 +11,7 @@ import {
cloneFunction,
mapProps,
merge,
fontSize,
} from '../src'

const width = style({
Expand Down Expand Up @@ -277,13 +278,39 @@ test('mapProps copies propTypes', t => {

test('merge deeply merges', t => {
const result = merge(
{ hello: { hi: 'beep' } },
{ hello: { hey: 'boop' } },
{ hello: { hi: 'beep', merge: 'me', and: 'me' } },
{ hello: { hey: 'boop', merge: 'me', and: 'all of us' } },
)
t.deepEqual(result, {
hello: {
hi: 'beep',
hey: 'boop',
merge: 'me',
and: 'all of us'
}
})
})

test('variant can be composed', t => {
const system = compose(
variant({ key: 'typography' }),
fontSize,
color
)
const result = system({
theme: {
typography: {
primary: {
fontSize: '32px',
color: '#fff'
},
},
},
variant: 'primary',
color: '#111'
})
t.deepEqual(result, {
fontSize: '32px',
color: '#111'
})
})

0 comments on commit 01d4669

Please sign in to comment.