-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(charts): customMemo support to compare function
- Loading branch information
1 parent
15ae303
commit 4f9ea1b
Showing
5 changed files
with
130 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { isEqualWithFunction } from '../custom-memo'; | ||
|
||
describe('custom-memo', () => { | ||
it('isEqualWithFunction', () => { | ||
expect( | ||
isEqualWithFunction( | ||
{ | ||
a: 1, | ||
b: 2, | ||
data: [{ x: 1, y: 2 }], | ||
}, | ||
{ | ||
a: 1, | ||
b: 2, | ||
data: [{ x: 1, y: 2 }], | ||
} | ||
) | ||
).toEqual(true); | ||
expect( | ||
isEqualWithFunction( | ||
{ | ||
a: 1, | ||
b: 2, | ||
data: [{ x: 1, y: 2 }], | ||
// arrow function | ||
fn: (x, y) => x + y, | ||
}, | ||
{ | ||
a: 1, | ||
b: 2, | ||
data: [{ x: 1, y: 2 }], | ||
fn: (x, y) => x + y, | ||
} | ||
) | ||
).toEqual(true); | ||
expect( | ||
isEqualWithFunction( | ||
{ | ||
a: 1, | ||
b: 2, | ||
data: [{ x: 1, y: 2 }], | ||
// normal function | ||
fn: function (x, y) { | ||
return x + y; | ||
}, | ||
}, | ||
{ | ||
a: 1, | ||
b: 2, | ||
data: [{ x: 1, y: 2 }], | ||
fn: function (x, y) { | ||
return x + y; | ||
}, | ||
} | ||
) | ||
).toEqual(true); | ||
expect( | ||
isEqualWithFunction( | ||
{ | ||
a: 1, | ||
b: 2, | ||
data: [{ x: 1, y: 2 }], | ||
// normal function abbreviation | ||
fn(x, y) { | ||
return x + y; | ||
}, | ||
}, | ||
{ | ||
a: 1, | ||
b: 2, | ||
data: [{ x: 1, y: 2 }], | ||
fn(x, y) { | ||
return x + y; | ||
}, | ||
} | ||
) | ||
).toEqual(true); | ||
expect( | ||
isEqualWithFunction( | ||
{ | ||
a: 1, | ||
b: 2, | ||
data: [{ x: 1, y: 2 }], | ||
fn: function (x, y) { | ||
return x + y; | ||
}, | ||
}, | ||
{ | ||
a: 1, | ||
b: 2, | ||
data: [{ x: 1, y: 2 }], | ||
fn(x, y) { | ||
return x + y; | ||
}, | ||
} | ||
) | ||
).toEqual(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,28 @@ | ||
import { memo } from 'react'; | ||
import type { FunctionComponent } from 'react'; | ||
import { isEqual } from 'lodash'; | ||
import { isFunction, isEqualWith, isEqual, isPlainObject, maxBy } from 'lodash'; | ||
import type { BaseConfig, AllBaseConfig } from '@ant-design/charts'; | ||
|
||
export function isEqualWithFunction(v1: any, v2: any) { | ||
return isEqualWith(v1, v2, (value1, value2) => { | ||
// function equals | ||
if (isFunction(value1) && isFunction(value2)) { | ||
return value1.toString() === value2.toString(); | ||
} else if (isPlainObject(value1) && isPlainObject(value2)) { | ||
const keys1 = Object.keys(value1); | ||
const keys2 = Object.keys(value2); | ||
const keys = maxBy([keys1, keys2], o => o.length); | ||
return keys.every(key => isEqualWithFunction(value1[key], value2[key])); | ||
} | ||
return isEqual(value1, value2); | ||
}); | ||
} | ||
|
||
export function customMemo<P extends BaseConfig<AllBaseConfig>>( | ||
Component: FunctionComponent<P>, | ||
propsAreEqual?: (prevProps: Readonly<P>, nextProps: Readonly<P>) => boolean | ||
) { | ||
return memo(Component, (prevProps, nextProps) => | ||
propsAreEqual ? propsAreEqual(prevProps, nextProps) : isEqual(prevProps, nextProps) | ||
propsAreEqual ? propsAreEqual(prevProps, nextProps) : isEqualWithFunction(prevProps, nextProps) | ||
); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.